Array
Array is the set of variable of same data type.Suppose you need 3 int type variable in a program. You may declare 3 variable as follows:
int a,b,c;
memory space under a,b,c will be created which can be imagined as follows:
Now imagine you need 100 int type variable. Will you type int a1,a2,a3,a4,…………………, a100; ? It is so much cumbersome and time consuming. Array is a nice way to do this. You can declare 100 int type data by this keyword as follows:
int mark[100];
Here mark is the array name. 100 is the size of array. 100 variable name will be mark[0], mark[1], mark[2],mark[3],…. mark[99]. So memory spaces for
int mark[100];
can be imagined as follows:
Here 0,1,2,3,……..99 are called index number. Index number starts from 0.
General syntax of declaring array:
data-type array-name[size];
Example:
If data type is float and size is 40 and name is score, then array declaration will be as follows:
float score[40];
Problem:
Input 10 integer numbers from keyboard and show the entered numbers.
Solution:
#include<stdio.h> int main() { int n[10],i; printf("Enter 10 integer numbers : "); for(i=0;i<10;i++) { scanf("%d",&n[i]); } printf("Your entered 10 integer numbers are :\n"); for(i=0;i<10;i++) { printf("%d\n",n[i]); } return 0; }
OUTPUT
Enter 10 integer numbers : 5 8 9 4 3 5 10 0 1
Your entered 10 integer numbers are :
5
8
9
4
3
5
10
0
1
Problem:
Write a program to input 10 integer numbers from keyboard and store the numbers in an array. Finally show the highest number.
Solution:
#include<stdio.h> int main() { int n[10],i,max; printf("Enter 10 integer numbers : "); for(i=0;i<10;i++) { scanf("%d",&n[i]); } max=n[0]; for(i=1;i<10;i++) { if(max>n[i]) max=n[i]; } printf("The highest number is : %d",max); return 0; }
OUTPUT
Enter 10 integer numbers : 5 8 9 4 3 5 10 0 1
The highest number is : 10
Next Previous