ARRAY
INTRODUCTION:An array is a sequence of data in memory, wherein all data are of the same type, and are placed in physically adjacent locations. For example, a sequence of 10 integers stored one after another in memory, represents an array.
Note that a string can be considered as a sequence of characters. The āCā language treats Strings as an array of characters, the last of which is the null character (Character with an ASCII value zero).
USE OF AN ARRAY:
Consider the problem of accepting the salary of 5 employees, printing them out individually and computing the average, without using arrays. Here, it reads the salary of 5 employees, adds them up in to another variable called sum and divide it by 5 to get the average salary. Needless to say, this is a very clumsy way to write a program. If then are a large number of employees; the number of statements also increases proportionately. To avoid this, use a loop. But the problem is that different variables are needed to store the salary of different employees and hence, the loop will have to store and print different variables each time.
A more elegant way is to use an array of integers to store the salary. So an array is defined as a set of homogeneous data items. Note that we can use any data type to declare an array.
Syn :
Data_type array_name[array_size];
Example :
int sal[9];
Array_Element | sal[0] | sal[1] | sal[2] | sal[3] | sal[4] | sal[5] | sal[6] | sal[7] | sal[8] |
Value | 1200 | 2500 | 2750 | 3750 | 5200 | 2350 | 3000 | 3500 | 4200 |
TYPES OF AN ARRAY:
Arrays whose element are specified by one subscript are called single dimensional array. Corresponding arrays whose elements are specified by two and three subscripts are called two-dimensional arrays respectively.
SINGLE-DIMENSIONAL ARRAY:
A list of items can be given a variable name using only one subscript and such variable is called a single dimensional array.
/* EX-22: PRG TO ILLUSTRATING THE SINGLE DIMENSIONAL ARRAY */ |
#include main() { int i; float avg,sal[8],sum=0; clrscr(); printf("\n Enter Basic salary for 8 employees: \n"); for(i=0;i<8;i++) { scanf("%f",&sal[i]); } clrscr(); printf("\n\n\n\n\n\t\t\t\" LIST OF SALARY \"\n"); for(i=0;i<8;i++) { printf("\n\t\t\t\t%-6.2f",sal[i]); sum+=sal[i]; } avg=sum/8; printf("\n\n\t\t\tSum of Salary : %-10.2f", sum); printf("\n\t\t\tAverage Salary: %-6.3f",avg); getch(); } |
/* EX-23: READ A LINE OF LOWER-CASE TEXT AND WRITE IT OUT IN UPPERCASE USING ONE-DIMENSIONAL CHARACTER ARRAY */ |
#include |
Note: The symbolic constant SIZE is assigned a value of 80. This symbolic constant, rather than its value, appears in the array definition and in the two for statements. Remember that the value of the symbolic constant will be substituted for the constant itself during the compilation process. Therefore, in order to alter the program to accommodate a different size array, only the #define statement must be changed.
|
#include<stdio.h> main() { int i,n; float a[50],large,small; clrscr(); printf("size of vector: "); scanf("%d",&n); printf("\n Vector elements: \n"); for(i=0;i<n;i++) scanf("%f",&a[i]); /* Initialization */ large=a[0]; small=a[0]; /* largest and smallest elements */ for(i=1;i<n;i++) { if(a[i]>large) large=a[i]; else if(a[i]<small) small=a[i]; } printf("\n Largest element is vector is %5.2f\n",large); printf("\n Smallest element in vector is %5.2f",small); getch(); } |
MULTI-DIMENSIONAL ARRAY:
If an array having two or more subscripts, then an array is called multidimensional array.
Syn: data_type var[sub1][sub2]ā¦..[subn];
Often, there is a need to store and manipulate two-dimensional data structures such as matrices and tables. Here, the array has two subscripts. One subscript denotes the row, and the other is column. As before, all subscript (row and column) starts with zero.
Subject Code | ||||
R o l l N o |
0 | 1 | 2 | |
0 | 60 | 52 | 81 | |
1 | 39 | 45 | 43 | |
2 | 70 | 68 | 77 | |
3 | 80 | 82 | 73 |
Here, the table has 4 roll nos. and each roll no. has 3 subject code.
Syn: data_type var_name[subscript1] [subscript2];
Ex: int mark[4][3];
/* EX-25: PGM TO ACCEPT 9 NOS AND PRINT IT IN 3 BY 3 MATRIX FORMAT */ |
main() { int n[3][3]; int r,c; clrscr(); printf("\n Enter any 9 nos: \n"); for(r=0;r<3;r++) { for(c=0;c<3;c++) scanf("%d",&n[r][c]); } clrscr(); gotoxy(23,10); printf(" \"OUTPUT IN 3 BY 3 MATRIX\"\n"); for(r=0;r<3;r++) { printf("\n\n\t\t\t"); for(c=0;c<3;c++) printf("%6d",n[r][c]); } getch(); } |
|
#include<stdio.h> main() { int a[10][10],b[10][10],c[10][10]; int i,j,m,n,p,q; clrscr(); printf("Enter row & column of Matrix-A\n"); scanf("%d %d",&m,&n); printf("Enter row & column of Matrix-B\n"); scanf("%d %d",&p,&q); if((m==p) && (n==q)) { printf("Matrix can be added\n"); printf("Input the elements for matrix-A\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("Input the elements for matrix-B\n"); for(i=0;i<p;i++) for(j=0;j<q;j++) scanf("%d",&b[i][j]); /* MATRIX ADDITION */ for(i=0;i<m;i++) for(j=0;j<n;j++) c[i][j] = a[i][j] + b[i][j]; printf("\n\t\t SUM OF TWO MATRICES\n"); for(i=0;i<m;i++) { printf("\n\n\t\t"); for(j=0;j<n;j++) printf("%5d",c[i][j]); } /* End of if statement */ } else printf("\n\t MATRIX CAN NOT BE ADDED"); getch(); } /* End of main */ |