Array
It is a collection of similar elements (having same data type). For an array
of 100 elements, the first element’s index is zero ‘0’ and the last index will
be 99. This indexed access makes it very convenient to loop through each
element of array. Array elements occupy contiguous memory locations.
A [0] = a
A [1] = b
A [7] = h
Multi Dimensional Array
In C language, one can have arrays of any dimensions. Let us consider a
3 x 3 matrix.
3 x 3 matrix for multi dimensional array
To access the particular element from the array, we have to use two subscripts; one for row number and other for column number. The notation is of the form a [i] [1], where i starlets for row subscripts and j stands for column subscripts.
We can also define and initialize the array as follows i nt values [3] [4] = (
11, 2, 3, 4,1
( 5 , 6, 7, 81
(9, 10, 11, 121
);
OR int values [3] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; A simple program using array # include <stdio.h> void main ( ) { |
a [ i ]
Subscripted Subscript Variable |
int ave, sum= 0;
int i;
int marks [10]; // array declaration
for (i =0; i <= 9; i + +)
{
printf (“\n Enter marks”);
scanf (“%d”, &marks [1]); // store data in array
}
for (i = 0; i <= 9; i + +)
sum= sum+ marks [I]; avg= sum/30;
printf (“1n avg-marks= %d”, avg);
}