What are Arrays
Let us suppose we wish to arrange the percentage marks obtained by 100 students in as- cending order. In such a case there are two options to store these marks in memory:
(a) Construct 100 variables to store percentage marks obtained by 100 different students i.e “each variable containing one students marks.
(b) Construct one variable (called array or subscripted variable) capable of storing or hold- ing all the hundred values.
Clearly, the second alternative is better because it would be much easier to handle one array variable than handling 100 different variables
Now we can give a formal definition of array . An array is a collective name given to a group of similar quantities. These similar quantities could be percentage marks of 100 students, or salaries of 300 employee or ages of 50 employees. Thus an array is a collection of similar elements. These similar elements could be all ints, or all floats or all chars etc. Usually, the array of characters is called a ‘string’, where as an array of ints or floats is called simply an array. All elements of any given array must be of the same type i.e we can’t have an array of
10 numbers, of which 5 are ints and 5 are floats.
ARRAY DECLARATION
To begin with, like other variables an array needs to be declared so that the compiler will know what kind of an array and how. large an array we want.
for e.g. int marks [30];
Here int specifies the type of variable, marks specifies the name of the variable. The number
30 tells how many elements of the type int will be in our array. This number is often called the
‘dimension’ of the array. The bracket [ ] tells the compiler that we are dealing with an array.
ACCESSING ELEMENTS OF AN ARRAY
To access an individual element in the array we have to subscript it, that is we have to put the number in the brackets following the array name. All the array elements are numbered start- ing with 0. Thus, marks [2] is not the second element of array but it is actually the third element. Thus marks [i] refers to (i + 1) th element of the array.
Let us take an example of a program using array :-
main ( )
{
float avg, sum=0;
int i;
int marks [30]; /* array declaration*/
for ( i =0; i < = 29; i ++)
{
printf (“\n Enter marks “);
scanf (“%d”, &marks [i]);
}
for ( i = 0; i <= 29; i ++)
sum = sum + marks [i];
avg = sum /30;
printf (“\n Average marks = % f”, avg);
}
ENTERING DATA IN TO THE ARRAY
The section of code which places data in to an array is
for (i=0; i<= 29; i++)
{
printf (“\n Enter marks”) ;
scanf (“%d”, &marks [i]);
}
The above section will read about 30 elements numbered from 0 to 29 in to the marks array. This will take input from the user repeatedly 30 times.
READING DATA FROM ARRAY
for ( i=0; i <= 29; i++);
sum = sum + marks [i];
avg = sum / 30;
printf (“\n Average marks = % f”, avg );
The rest of the program reads the data back out of the array and uses it to calculate the average. The for loop is much the same, but now the body of loop causes each student’s marks to be added to a running total stored in a variable called sum. When all the marks have been added up, the result is divided by 30, the numbers of students to get the average.
Let us summarize the facts about array :-
(a) An array is a collection of similar elements.
(b) The first element in the array is numbered 0, the last element is 1 less than the size of the array.
(c) An array is also known as subscripted variable .
(d) Before using an array its type and dimension must be declared.
(e) However big an array is, its elements are always stored in contiguous memory locations.
ARRAY INITIALISATION
To initialise an array while declaring it.
Following are a few examples which demonstrate this:-
int num [6] = {2, 4, 12, 5, 45, 5};
int n [ ] = {2, 4, 12, 5, 45, 5};
float press [ ] = { 12.3, 34.2, -23.4, - 11.3}
The following points should be noted
(a) Till the array elements are not given any specific values, they are suppose to contain garbage values.
(b) If the array is initialized where it is declared mentioning the dimension of the array is optional as in the 2nd example above.
MULTIDIMENSIONAL ARRAYS
In C one can have arrays of any dimensions. To understand the concept of multidimensional arrays let us consider the following 4 x 5 matrix

Let us assume the name of matrix is x
To access a particular element from the array we have to use two subscripts on for row
number and other for column number the notation is of the form X [i] [j] where i stands for row subscripts and j stands for column subscripts. Below given are some typical two-dimensional array definitions
float table [50] [50];
char line [24] [40];
The first example defines tables as a floating point array having 50 rows and 50 columns. The number of elements will be 2500 (50 X50).
The second declaration example establishes an array line of type character with 24 rows and
40 columns. The number of elements will be (24 X 40) 1920 consider the following two dimen- sional array definition int values [3] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10. 11, 12, };
Thus the array values can be shown pictorially as

Values [0] [0] = 1 Values [0] [1] = 2 Values [0] [2] = 3 Values [0] [3] = 4
Values [1] [0] = 5 Values [1] [1] = 6 Values [1] [2] = 7 Values [1] [3] = 8
Values [2] [0] = 9 Values [2] [1] = 10 Values [2] [2] = 11 Values [2] [3] = 12
Here the first subscript stands for the row number and second one for column number. First
subscript ranges from 0 to 2 and there are altogether 3 rows second one ranges from 0 to 3
and there are altogether 4 columns.
Alternatively the above definition can be defined and initialised as
int values [3] [4] = {
{ 1, 2, 3, 4}
{ 5, 6, 7, 8}
{9, 10, 11, 12}
};
Here the values in first pair of braces are initialised to elements of first row, the values of second pair of inner braces are assigned to second row and so on. Note that outer pair of curly braces is required.
If there are two few values within a pair of braces the remaining elements will be assigned as zeros.
Here is a sample program that stores roll numbers and marks obtained by a student side by side in matrix
main ( )
{
int stud [4] [2];
int i, j;
for (i =0; i < =3; i ++)
{
printf (“\n Enter roll no. and marks”);
scanf (“%d%d”, &stud [i] [0], &stud [i] [1] );
}
for (i = 0; i < = 3; i ++)
printf (“\n %d %d”, stud [i] [0], stud [i] [1]);
}
The above example illustrates how a two dimensional array can be read and how the values stored in the array can be displayed on screen.
Let us suppose we wish to arrange the percentage marks obtained by 100 students in as- cending order. In such a case there are two options to store these marks in memory:
(a) Construct 100 variables to store percentage marks obtained by 100 different students i.e “each variable containing one students marks.
(b) Construct one variable (called array or subscripted variable) capable of storing or hold- ing all the hundred values.
Clearly, the second alternative is better because it would be much easier to handle one array variable than handling 100 different variables
Now we can give a formal definition of array . An array is a collective name given to a group of similar quantities. These similar quantities could be percentage marks of 100 students, or salaries of 300 employee or ages of 50 employees. Thus an array is a collection of similar elements. These similar elements could be all ints, or all floats or all chars etc. Usually, the array of characters is called a ‘string’, where as an array of ints or floats is called simply an array. All elements of any given array must be of the same type i.e we can’t have an array of
10 numbers, of which 5 are ints and 5 are floats.
ARRAY DECLARATION
To begin with, like other variables an array needs to be declared so that the compiler will know what kind of an array and how. large an array we want.
for e.g. int marks [30];
Here int specifies the type of variable, marks specifies the name of the variable. The number
30 tells how many elements of the type int will be in our array. This number is often called the
‘dimension’ of the array. The bracket [ ] tells the compiler that we are dealing with an array.
ACCESSING ELEMENTS OF AN ARRAY
To access an individual element in the array we have to subscript it, that is we have to put the number in the brackets following the array name. All the array elements are numbered start- ing with 0. Thus, marks [2] is not the second element of array but it is actually the third element. Thus marks [i] refers to (i + 1) th element of the array.
Let us take an example of a program using array :-
main ( )
{
float avg, sum=0;
int i;
int marks [30]; /* array declaration*/
for ( i =0; i < = 29; i ++)
{
printf (“\n Enter marks “);
scanf (“%d”, &marks [i]);
}
for ( i = 0; i <= 29; i ++)
sum = sum + marks [i];
avg = sum /30;
printf (“\n Average marks = % f”, avg);
}
ENTERING DATA IN TO THE ARRAY
The section of code which places data in to an array is
for (i=0; i<= 29; i++)
{
printf (“\n Enter marks”) ;
scanf (“%d”, &marks [i]);
}
The above section will read about 30 elements numbered from 0 to 29 in to the marks array. This will take input from the user repeatedly 30 times.
READING DATA FROM ARRAY
for ( i=0; i <= 29; i++);
sum = sum + marks [i];
avg = sum / 30;
printf (“\n Average marks = % f”, avg );
The rest of the program reads the data back out of the array and uses it to calculate the average. The for loop is much the same, but now the body of loop causes each student’s marks to be added to a running total stored in a variable called sum. When all the marks have been added up, the result is divided by 30, the numbers of students to get the average.
Let us summarize the facts about array :-
(a) An array is a collection of similar elements.
(b) The first element in the array is numbered 0, the last element is 1 less than the size of the array.
(c) An array is also known as subscripted variable .
(d) Before using an array its type and dimension must be declared.
(e) However big an array is, its elements are always stored in contiguous memory locations.
ARRAY INITIALISATION
To initialise an array while declaring it.
Following are a few examples which demonstrate this:-
int num [6] = {2, 4, 12, 5, 45, 5};
int n [ ] = {2, 4, 12, 5, 45, 5};
float press [ ] = { 12.3, 34.2, -23.4, - 11.3}
The following points should be noted
(a) Till the array elements are not given any specific values, they are suppose to contain garbage values.
(b) If the array is initialized where it is declared mentioning the dimension of the array is optional as in the 2nd example above.
MULTIDIMENSIONAL ARRAYS
In C one can have arrays of any dimensions. To understand the concept of multidimensional arrays let us consider the following 4 x 5 matrix
Let us assume the name of matrix is x
To access a particular element from the array we have to use two subscripts on for row
number and other for column number the notation is of the form X [i] [j] where i stands for row subscripts and j stands for column subscripts. Below given are some typical two-dimensional array definitions
float table [50] [50];
char line [24] [40];
The first example defines tables as a floating point array having 50 rows and 50 columns. The number of elements will be 2500 (50 X50).
The second declaration example establishes an array line of type character with 24 rows and
40 columns. The number of elements will be (24 X 40) 1920 consider the following two dimen- sional array definition int values [3] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10. 11, 12, };
Thus the array values can be shown pictorially as
Values [0] [0] = 1 Values [0] [1] = 2 Values [0] [2] = 3 Values [0] [3] = 4
Values [1] [0] = 5 Values [1] [1] = 6 Values [1] [2] = 7 Values [1] [3] = 8
Values [2] [0] = 9 Values [2] [1] = 10 Values [2] [2] = 11 Values [2] [3] = 12
Here the first subscript stands for the row number and second one for column number. First
subscript ranges from 0 to 2 and there are altogether 3 rows second one ranges from 0 to 3
and there are altogether 4 columns.
Alternatively the above definition can be defined and initialised as
int values [3] [4] = {
{ 1, 2, 3, 4}
{ 5, 6, 7, 8}
{9, 10, 11, 12}
};
Here the values in first pair of braces are initialised to elements of first row, the values of second pair of inner braces are assigned to second row and so on. Note that outer pair of curly braces is required.
If there are two few values within a pair of braces the remaining elements will be assigned as zeros.
Here is a sample program that stores roll numbers and marks obtained by a student side by side in matrix
main ( )
{
int stud [4] [2];
int i, j;
for (i =0; i < =3; i ++)
{
printf (“\n Enter roll no. and marks”);
scanf (“%d%d”, &stud [i] [0], &stud [i] [1] );
}
for (i = 0; i < = 3; i ++)
printf (“\n %d %d”, stud [i] [0], stud [i] [1]);
}
The above example illustrates how a two dimensional array can be read and how the values stored in the array can be displayed on screen.