Wednesday, November 2, 2011

A Simple Program Using Array in C programming language

Let us try to write a program to find average marks obtained by a class of 30 students in a test.
main( )
int avg, sum = 0 ;
int i ;
int marks[30] ; /* array declaration */

for ( i = 0 ; i <= 29 ; i++)
{
         printf ( "\nEnter marks " ) ;
         scanf ( "%d", &marks[i] ) ; /* store data in array */
}

for ( i = 0 ; i <= 29 ; i++ ) 
sum = sum + marks[i] ; /* read data from an array*/

avg = sum / 30 ;
printf ( "\nAverage marks = %d", avg ) ;
}

There is a lot of new material in this program, so let us take it apart slowly.
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. In our program we have done this with the statement:
int marks[30] ;
Here, int specifies the type of the variable, just as it does with ordinary variables and the word marks specifies the name of the variable. The [30] however is new. 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
Once an array is declared, let us see how individual elements in the array can be referred. This is done with subscript, the number in the brackets following the array name. This number specifies the element’s position in the array. All the array elements are numbered, starting with 0. Thus, marks[2] is not the second element of the array, but the third. In our program we are using the variable i as a subscript to refer to various elements of the array. This variable can take different values and hence can refer to the different elements in the array in turn. This ability to use variables as subscripts is what makes arrays so useful.

Entering Data into an Array

Here is the section of code that places data into an array:
for ( i = 0 ; i <= 29 ; i++ ) 
printf ( "\nEnter marks " ) ; 
scanf ( "%d", &marks[i] ) ; 
}

The for loop causes the process of asking for and receiving a student’s marks from the user to be repeated 30 times. The first time through the loop, i has a value 0, so the scanf( ) function will cause the value typed to be stored in the array element marks[0], the first element of the array. This process will be repeated untilbecomes 29. This is last time through the loop, which is a good thing, because there is no array element like marks[30].
In scanf( ) function, we have used the “address of” operator (&) on the element marks[i] of the array, just as we have used it earlier on other variables (&rate, for example). In so doing, we are passing the address of this particular array element to the scanf( ) function, rather than its value; which is what scanf( ) requires.

Reading Data from an Array

The balance 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 the 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 number of students, to get the average.
for ( i = 0 ; i <= 29 ; i++ ) 
sum = sum + marks[i] ;

avg = sum / 30 ;
printf ( "\nAverage marks = %d", avg ) ;
To fix our ideas, let us revise whatever we have learnt about arrays:

(a) An array is a collection of similar elements.
(b) The first element in the array is numbered 0, so the last element is 1 less than the size of the array.
(c) An array is also known as a subscripted variable.
(d) Before using an array its type and dimension must be declared.
(e) However big an array its elements are always stored in contiguous memory locations. This is a very important point which we would discuss in more detail later on.

No comments:

Post a Comment