We have seen how to create variables – integer, float, or character types, each variable is given a unique name and you can store one item under that name. Often, however, we need several similar items to be grouped under a single name. For example, marks scored by a student in different subjects, the salaries of employees of a particular department, etc. Of course, if a student has 5 subjects you can declare 5 different variables – say sub1,sub2 .. sub5 and operate on them. But it is better if we can give a common name to them so that we can know that they are related to each other. Further, what if there are 100 employees in a dept? Declaring 100 different variables explicitly is quite difficult. In such cases, a data structure called “array” is used. ( Incidentally, a data structure can be approximately thought of as the method of grouping memory location in a particular form )
Suppose we consider individual variables like this
Then we can group them under a common name sub as follows:
Now all of them have a common name called sub and they can be individually addressed by their index numbers 1,2,3,4 & 5. So, the marks scored in subject 1 are stored in sub[1], that in subject 2 is sub[2], etc. In C, you can declare arrays in the beginning just like declaring other variables. For example int sub[0],float salary[50] etc.
This means that in the array called sub, 5 different integer values can be stored, under salary 50 different float values are stored. To access each of them, their specific index number is to be given. However, in C, the first element index is 0, the second one is 1, etc.
For example sub[0] stores the marks of the first subject…. Sub[4] stores the marks of the fifth subject. Just to familiarise ourselves with the way arrays operate, we see the following examples:
To input the numbers between 1 to 10
Main() { Int rating-counters[11],i,response; for (i=1; i<=10;++i) rating_counters [i] =0; printf ("Enter your responses\n"); for (i=1; i<=20; i++) { scanf ("%d",&response); if (response10) else ++rating_counters[response]; printf("\n\nRating Number of Responses\n"); printf("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ \n"); for (i=1; i<=10; ++i) printf("%4d%14d\n",rating_counters[i]); }
OUTPUT:
Enter your responses 6 5 8 3 9 6 5 7 15 Bad response: 15 5 5 1 7 4 10 5 5 6 8 9 6 Rating Number of Responses - - - - - - - - - - - - - - - - - - - 1 1 2 0 3 1 4 1 5 6 6 4 7 2 8 2 9 2 10 1
Program to generate the first 15 fibonacci numbers
main() { int fibonacci[15],i; fibonacci[0] = 0; /* by definition */ fibonacci[1] =1; /* - " - */ for (i=2; i<15; ++i) fibonacci[i] = fibonacci [i-2] + fibonacci [i-1]; for (i=0; i<15; ++i) printf("%d\n",fibonacci[i]); }
OUTPUT:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Write a program to compute all prime numbers between 2 and 50
main() { int p,is_prime,i,primes[50],prime_index = 2; primes[0] =2; primes[1] =3; for (p=5; p=primes[i]; ++i) for (p=5; p<=50; p=p+2) is_prime =1; for (i=1; is_prime && p/primes[1] >=primes[i]; ++i) if (p%primes[i] == 0) is_prime = 0; if (is_prime) { primes [prime_index] =p; ++prime_index; } } for (i=0; i<prime_index; ++i) printf("%d",primes[i]); printf("\n"); }
OUTPUT:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
It is also possible to initialize the array elements at the time of declaration itself
write a program to print array values
Main() { static int array_values[10] = { 0,1,4,9,16}; int i; for (i=5; i<10; ++i) array_values[i] = i * i; for(i=0; i<10; ++i) printf("array_values[%d]=%d\n",i,array_values[i]); }
OUTPUT:
Array_values[0] = 0 Array_values[1] = 1 Array_values[2] = 4 Array_values[3] = 9 Array_values[4] = 16 Array_values[5] = 25 Array_values[6] = 36 Array_values[7] = 49 Array_values[8] = 64 Array_values[9] = 81
Arrays need not be only single dimensional. They can be 2,3,… dimensional also Consider the very popular concept of matrices A matrix can be thought as a set of numbers as follows
FIGURE
This is a 4 * 5 matrix, with 4 rows and 5 columns. This can be stored in a integer variables M as follows.
Int M[4] [5]
Because M has 4 rows (0,1,2,3) and 5 columns(0,1,2,3,4). As before, the array can also be initialised at declaration time itself.
Int M[4][5]= { { 10, 5, 3, 7, 9}, { 9, 6, 4, 7, 9}, { 5, 4, -8 6, 7}, { 4, 1, 7, 6, -5} };
Ofcourse, in all intilisations, not all the elements need to be initialised, you can only intialise those elements that you need and leave out other elements. They will be initialised to 0. We now see several other programs using arrays.
WRITE A PROGRAM TO FIND LARGEST, SMALLEST, SUM & AVERAGE OF FIVE NUMBERS
#include<stdio.h> main() { int x[5]; int i, large, small, sum=0; float ave; for (i=0; i<5; i+) { printf (“\n enter number%d:”,i+1); scanf(“%f”,&x[i]; } /* To print largest and smallest number */ large=small=x[0]; for (i=0; i<5; i++) { if(x[i]>large) large=x[i]; of(x[i]>small) small=x[i]; } printf(“\n The largest no is %d”,large); printf(“\nThe smallest no. is %d”,small); /*To find the sum and average*/ for (i=0; i<5; i++) sum+=x[i]; ave=sum/5.0 printf(“\nthe sumis %d”,sum); printf(“\nthe average is:%5.2f”,ave); }
OUTPUT:
Enter number1:50 Enter number2:30 Enter number3:40 Enter number4:10 Enter number5:20 The largest No. is 50 The smallest No. is 10 The sum is 150 The average is 30.00
WRITE A PROGRAM TO SORT NUMBERS IN ASCENDING ORDER USING BUBBLE-SORT
#include<stdio.h> main() { int x[10], i, j, n,temp; printf(“\n ENTER NUMBER OF ITEMS:”); scanf(%d”,&n); /*GET THE ARRAY FROM THE USER*/ printf(“\n Enter the numbers:\n); for (i=0;i<n;i++) scanf(“%d”,&x[i]); /*SORT THE ARRAY */ for (i=0;i<n-1-i;j++) { for (j=0;j<n-1-i; j++) { if(x[i]>x[j+1]) { temp=x[j]; x[j]=x(j+1); x[j+1] = temp; } } }
TO PRINT THE ARRAY
printf(“\n The Numbers in ascending order:\n”); for(i=0;i<n;i++) printf(“%d\n”,x[i]); }
OUTPUT :
Enter the numbers: 5 4 3 2 1 The Numbers in ascending order: 1 2 3 4 5
WRITE A PROGRAM TO ADD TWO MATRICES
#include<stdio.h> main() { int a[5][5],b[5][5],c[5][5]; int i,j,m,n; /*GET THE MATRIX FROM THE USER*/ printf(“enter matrix A:\n”); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(“%d”,&a[i][j]; printf(“enter matrix B:\n”); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(“%d”,&b[i][j]; /*ADD THE MATRICES*/ for(i=0;i<m;i++) for(j=0;j<n;j++) c[i][j]=a[i][j] + b[i][j]; /*TO PRINT THE SUM*/ printf(“\n THE SUM OF MATRICES:\n”); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf(“%d\t”,c[i][j]; printf(“/n/”); } }
OUTPUT :
Enter matrix A: 1 1 1 1 1 1 1 1 1 Enter matrix B: 2 2 2 2 2 2 2 2 2 THE SUM OF MATRICES: 3 3 3 3 3 3 3 3 3
WRITE A PROGRAM FOR THE SUBTRACTION OF MATRICES
#include<stdio.h> main() { int a[5][5],b[5][5],c[5][5]; int i,j,m,n; /*GET THE MATRIX FROM THE USER*/ printf(“enter matrix A:\n”); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(“%d”,&a[i][j]; printf(“enter matrix B:\n”); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(“%d”,&b[i][j]; /*SUBTRACTION THE MATRICES*/ for(i=0;i<m;i++) for(j=0;j<n;j++) c[i][j]=a[i][j] - b[i][j]; /*TO PRINT THE DIFFERENCE*/ printf(“\n THE DIFFERENCE OF MATRICES:\n”); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf(“%d\t”,c[i][j]; printf(“/n/”); } }
OUTPUT:
Enter matrix A: 3 3 3 3 3 3 3 3 3 Enter matrix B: 2 2 2 2 2 2 2 2 2 THE DIFFERNCE OF MATRICES: 1 1 1 1 1 1 1 1 1
WRITE A PROGRAM FOR FINDING THE PRODUCT OF MATRICES
#include<stdio.h> main() { int a[5][5],b[5][5],c[5][5]; int i,j,k,m,n,p,q; printf(“\n ENTER ORDER OF MATRIX A:”); scanf(%d%d”,&m,&n); printf(“\n ENTER ORDER OF MATRIX B:”); scanf(%d%d”,&p,&q); if (n!=p) { printf(“\n\tCANNOT MULTIPLY”); exit(); } /*GET THE MATRIX FROM THE USER*/ printf(“enter matrix A:\n”); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(“%d”,&a[i][j]; printf(“enter matrix B:\n”); for(j=0;j<p;j++) for(k=0;k<q;k++) scanf(“%d”,&b[j][k]; /*PRODUCT OF MATRICES*/ for(i=0;i<m;i++) { for(k=0;k<q;k++) { c[i][k]=0; for(j=0;j<n;j++) c[i][k]+=a[i][j] - b[j][k]; } } /*TO PRINT THE PRODUCT OF MATRICES*/ printf(“\n THE PRODUCT OF MATRICES:\n”); for(i=0;i<m;i++) { for(k=0;k<q;k++) printf(“%d\t”,c[i][k]; printf(“\n”); } }
OUTPUT:
Enter matrix A: 2 2 2 2 2 2 Enter matrix B: 3 3 3 3 3 3 THE PRODUCT OF MATRICES: 18 18 18 18
WRITE A PROGRAM TO READ A LINE OF TEXT AND COUNT THE NUMBER OF VOWELS, CONSONENTS, DIGITS AND BLANK SPACES
#include<stdio.h> main() { char line[50],c; int v=0,con=0,d=0,ws=0,count=0; printf(“\n Enter a line of text:\n”); scanf(“%[^\n]”,line); while((c=toupper(line[count] ) )!= ‘\0’) { if(c== ‘A’ || c==‘E’ || c==‘I’ || c== ‘U’) v++; else if(c>=‘A’ && c<= ‘Z’) con++; else if(c>= ‘0’ && c<= ‘9’) d++; elseif(c== ‘ ’ || c==‘\t’) ws++; ++count; } printf(“\n Number of Vowels:%d”,v) printf(“\n Number of Consonants: %d”,con); printf(“\n Number of Digits:%d”,d); printf(“\n Number of Blank spaces:%d”,ws); }
OUTPUT:
Enter a line of text: 1 This is to test a line of text. Number of Vowels: 9 Number of consonants: 14 Number of Digits:1 Number of Blank spaces:8
WRITE A PROGRAM TO FIND THE TRANSPOSE OF A MATRIX
#include<stdio.h> main() { int a[5][5]; int i,j,m,n; printf(“\n ENTER ORDER OF MATRIX”); scanf(“%d%d”,&m,&n); /*GET THE MATRIX FROM THE USER */ printf(“Enter the matrix:\n); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(“%d”,&a[i][j]; /*TRANSPOSE OF MATRIX*/ printf(“\n TRANSPOSE OF MATRIX IS:\n”); for(i=0;i<n;i++) { for(j=0;j<m;j++) printf(“%d\t”,a[i][j]; printf(“/n”); } }
OUTPUT:
Enter matrix : 1 2 3 4 5 6 7 8 9 TRANSPOSE OF MATRIX IS: 1 4 7 2 5 8 3 6 9
WRITE A PROGRAM TO FIND WHETHER THE MATRIX IS SYMMETRICAL
#include<stdio.h> main() { int a[5][5]; int i,j,k,m,n; printf(“\n ENTER ORDER OF MATRIX :”); scanf(%d%d”,&m,&n); if (m!=n) { printf(“\n\**WARNING***\n ENTER SQUARE MATRIX ONLY”); exit(); } /*GET THE MATRIX FROM THE USER*/ printf(“enter matrix :\n”); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(“%d”,&a[i][j]; /*CHECK FOR SUMMETRY*/ for(i=0;i<n;i++) for(j=0;j<m;j++) if(a[i][j]!=a[j][i] { printf(“\n MATRIX IS NOT SYMMETRIC\n”); exit(0; } printf(“\NMATRIX IS SYMMETRIC\n); }
OUTPUT:
Enter matrix : 1 2 3 2 1 4 3 4 1 MATRIX IS SYMMETRIC
WRITE A PROGRAM TO FIND THE LARGEST ELEMENT IN AN ARRAY AND POSITION OF IT’S OCCURRENCE
main() { int A[10]; int largest,position; int num,i; printf(“\nEnter the number of elements in the array\n”); scanf(“%d”,&num); printf(“\nEnter the array elements\n); for (i=0; i<num; i++) scanf(“%d”,%A[i]); largest = A[0]; for(i=1; i<num; i++) { if (largest < A[i]) { largest = A[i]; position = i; } } printf(“The largest element of the array is %d”,largest); printf(“and it is in position %d”,position); }
OUTPUT:
Enter the number of elements in the array 6 Enter the array elements 1 6 7 3 9 7 The largest element of the array is 9 and it is in position 4
WRITE APROGRAM FOR LINEAR SEARCH
main() { int A[100],item, num,i; /* read in number of elements*/ printf(“\nEnter the number of elements \n”); scanf(“%d”,&num); /*read in array elements*/ printf(“\nEnter the array elements\n); for (i=0; i<num; i++) scanf(“%d”,%A[i] ); /*read in item to be searched */ printf(“\Enter the item to be searched\n”); scanf(%d”,&item); /*check for the existence of the item in the array */ for(i=1; i<num; i++) if (item== A[i] ) { printf(“\n Item %d is found at position%d”,item,I); exit(); /* quit the program if search is successful*/ } printf(“\n Item %dnot found in array\n”,item); }
OUTPUT:
Enter the number of elements 7 Enter the array elements 4 9 2 8 3 7 1 Enter the item to be searched 8 Item 8 is found at position 3
WRITE A PROGRAM FOR BINARY SEARCH
main() { int low,high,middle,i,item,n,a[20]; /*read in the element to be searched */ printf(“\n The element to be searched =”); scanf(%d”,&item); /*read in the number of elements in the array*/ printf(“Enter the number of elements in the array(<=20):”); scanf(“%d”,&n); /*read in the array elements */ printf(“\nPlease enter the array element (sorted one) :\n); for (i=1; i<=n; i++) scanf(“%d”,&a[i] ) low =1; high = n; /*initializing low and high index values */ do { middle = (low + high)/2; /* initializing middle index*/ if(item < a[middle] ) high = middle –1; else if(item > a[middle] ) low = middle + 1; } while(item != a[middle] && (low >=high)); /*print the result*/ if(item == a[middle] ) printf(“%d is found at position %d,item,middle); else printf(“Item not found in the given array\n”); }
OUTPUT:
The element to be searched =6 Enter the number of elements in the array (<=20):8 Please enter the array elements (sorted one): 3 6 8 10 11 12 13 60 6 is found at position 2