In C programming, one of the frequently arising problem is to handle similar types of data. For example: If the user want to store marks of 100 students. This can be done by creating 100 variable individually but, this process is rather tedious and impracticable. These type of problem can be handled in C programming using arrays.
An array is a sequence of data item of homogeneous value(same type).
Arrays are of two types:
- One-dimensional arrays
- Multidimensional arrays
Declaration of one-dimensional array
[message_box color=”yellow”]
data_type array_name[array_size];
For example: int age[5]; [/message_box]
Here, the name of array is age. The size of array is 5,i.e., there are 5 items(elements) of array age. All element in an array are of the same type (int, in this case).
Array elements
Size of array defines the number of elements in an array. Each element of array can be accessed and used by user according to the need of program. For example:
[message_box color=”yellow”]
int age[5];
[/message_box]
Note that, the first element is numbered 0 and so on.
Here, the size of array age is 5 times the size of int because there are 5 elements.
Suppose, the starting address of age[0]
is 2120d and the size of int be 4 bytes. Then, the next address (address of a[1]
) will be 2124d, address of a[2]
will be 2128d and so on.
Initialization of one-dimensional array:
Arrays can be initialized at declaration time in this source code as:
[message_box color=”yellow”]
int age[5]={2,4,34,3,4};
[/message_box]
It is not necessary to define the size of arrays during initialization.
[message_box color=”yellow”]
int age[]={2,4,34,3,4};
[/message_box]
In this case, the compiler determines the size of array by calculating the number of elements of an array.
Example of array in C programming
[message_box title=”Program” color=”yellow”]
/* C program to find the sum marks of n students using arrays */
/*Written by Utpal Chaudhary*/
#include <stdio.h>
#include <conio.h>
void main()
{
int marks[10],i,n,sum=0;
clrscr();
printf("Enter number of students: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("Enter marks of student %d: ",i+1);
scanf("%d",&marks[i]);
sum+=marks[i];
}
printf("Sum= %d",sum);
gtech();
}
[/message_box]
[message_box title=”Output” color=”yellow”]
Enter number of students:3
Enter marks of student 1:40
Enter marks of student 2:50
Enter marks of student 3:60
Sum=150
[/message_box]
Multidimensional Array
C programming language allows programmer to create arrays of arrays known as multidimensional arrays. For example:
float a[3][4];
Here, a is an array of two dimension, which is an example of multidimensional array. This array has 2 rows and 6 columns
For better understanding of multidimensional arrays, array elements of above example can be thinked of as below:
In C, multidimensional arrays can be initialized in different number of ways.
[message_box color=”yellow”]
int c[2][3]={{1,3,0}, {-1,5,9}};
OR
int c[][3]={{1,3,0}, {-1,5,9}};
OR
int c[2][3]={1,3,0,-1,5,9};
[/message_box]
Initialization of three-dimensional array
[message_box color="yellow"] double cprogram[3][2][4]={ {{-0.1, 0.22, 0.3, 4.3}, {2.3, 4.7, -0.9, 2}}, {{0.9, 3.6, 4.5, 4}, {1.2, 2.4, 0.22, -1}}, {{8.2, 3.12, 34.2, 0.1}, {2.1, 3.2, 4.3, -2.0}} }; [/message_box]
Suppose there is a multidimensional array arr[i][j][k][m]
. Then this array can hold i*j*k*m numbers of data.
Similarly, the array of any dimension can be initialized in C programming.
Example of Multidimensional Array In C
[message_box title=”Program” color=”yellow”]
/*write a program to find sum of two matrix of order 2*2 using multidimensional array where, element of matrix entered by user*/
/*Written by Utpal Chaudhay*/
#include <stdio.h>
#include <conio.h>
void main()
{
float a[2][2], b[2][2], c[2][2];
int i,j;
clrscr();
printf("Enter the elements of 1st matrix\n");
/* Reading two dimensional Array with the help of two for loop. If there was an array of 'n' dimension, 'n' numbers of loops
are needed for inserting data to array.*/
for(i=0;i<2;++i)
{
for(j=0;j<2;++j)
{
printf("Enter a%d%d: ",i+1,j+1);
scanf("%f",&a[i][j]);
}
}
printf("Enter the elements of 2nd matrix\n");
for(i=0;i<2;++i)
{
for(j=0;j<2;++j)
{
printf("Enter b%d%d: ",i+1,j+1);
scanf("%f",&b[i][j]);
}
}
for(i=0;i<2;++i)
{
for(j=0;j<2;++j)
{
/* Writing the elements of multidimensional array using loop. */
c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two arrays. */
}
}
printf("\nSum Of Matrix:");
for(i=0;i<2;++i)
{
for(j=0;j<2;++j)
{
printf("%.1f\t",c[i][j]);
if(j==1) /* To display matrix sum in order. */
printf("\n");
}
}
getch();
}
[/message_box]
[message_box title=”Output” color=”yellow”]
Enter the elements of 1st matrix
Enter a11: 2;
Enter a12: 0.5;
Enter a21: -1.1;
Enter a22: 2;
Enter the elements of 2nd matrix
Enter b11: 0.2;
Enter b12: 0;
Enter b21: 0.23;
Enter b22: 23;
Sum Of Matrix:
2.2 0.5
-0.9 25.0
[/message_box]