Learn C Language : Array

What are Arrays

For understanding the arrays properly, consider the following program:

main()
{
     int x;
      x = 5;
      x = 10;
      printf(“x = %d”,x);
}

No doubt, this program will print the value of x as 10.Because when a value 10 is assigned to x, the earlier value of x i.e. 5 is lost.Thus, ordinary variables are capable of holding only one value at a time.However, there are situations in which we would want to store more than one value at a time in a single variable.nnFor example, suppose we wish to arrange the percentage marks obtained by 100 students in ascending order.In such a case we have two options to store these marks in memory:

(1) Construct 100 variables to store percentage marks obtained by 100 different students.i.e. each variable containing one student’s marks.
(2) Construct one variable (called array or subscripted variable) capable of storing or holding all the hundred values.

Assume the following group of numbers which represent percentage marks obtained by five students.

per = {48, 88 ,34, 23, 96}

A simple program using array:

main()
{
      float avg, sum = 0;
      int i;

      // Array declaration
      int mark[30];

      for(i=0; i<=29; i++)
      {
            printf(“Enter the marks”);
            scanf(“%d”,marks[i]);
      }
      for(i=0; i<=29; i++)
            sum = sum + marks[i];
      avg = sum / 30;
      printf(“avg = %d”,avg);
}

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 ‘diamension’ 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 ara numbered, starting with 0.thus, marks[2] is not second element of the array but the third.

In our program, we are using the variable i as a subscript to referto 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 sunscripts is whats makes arrays so useful.

Entering Data into an Array

Here is the section of code that places data into the array:

for(i=0; 29>=i; i++)
{
      printf(“Enter marks”);
      scanf(“%d”,&marks[i]);
}

The for loop causes the process of asking for and receiving of 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() statement 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 until i become 29.This is last time through the loop,which is a good thing, because there is no array element like marks[30].

In the scanf() statement we have used the ‘address of’ operatoer on the element marks[i] of the array, just as we have used it earlier on other variables.We are passing the address of this particular array element to the scanf() function, rather than its value.

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 cause each student 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; 29>=i; i++)
{
     sum = sum + marks[i];
      avg = sum / 30;
      printf(“Average marks = %f”,avg);

To fix our ideas, let us revise whatever we have learnt about arrays:

(1) An array is a collection of similar elements.
(2) The first element in the array is numbered 0, so the last element is 1 less than the size of the array.
(3) An array is also known as a subscripted variable.
(4) Before using an array its type and dimension must be declared.
(5) However an array elements are always stored in contiguous memory locations.

Array Initialisation

So far we have used arrays that did not have any value in them to begin with.We managed to store values in them during program execution.Let us now how to initialize an array while declaring it.

Following are a few examples which demonstrate this.

int num[6] = {2, 3, 4, 6, 1, 7};
int n[] = {2, 4, 1, 56, 45, 5};
float press[] = {12.3, 34.2, 23.4, -11.3};

Note the following points carefully:

(1) Till the array elements are not given any specific value, they are supposed to contain garbage values.

(2) If the array is initialized where it is declared mentioning the dimension of the array is optional as in the 2nd example above.

Passing Array Elements to a Function

Array elements can be passed to a function by calling the function by value or by reference.In the call by value we pass values of array elements to the function, whereas in the call by reference we pass address of the array elements to the function.

These two calls are illustrated below:

/* Demonstration of call by value */

main()
{
      int i;
      int mark[] = {55, 65, 75, 56, 78, 78, 90};
      for(i=0; 6>=i; i++)
            display(marks[i]);
}

void display(int m)
{
      printf(“%d”,m);
}

Output
55 65 75 56 78 78 90

Here, we are passing an individual array element at a time to the function display() and getting it printed in the function display().Note that since at a time only one element is being passed, this element is collected in an ordinary integer variable m, in the function display().

Now see the call by reference

/* Demonstration of call by reference */

main()
{
      int i;
      int marks[] = {55, 65, 75, 56, 78, 78, 90};
      for(i=0; 6>=i; i++)
            disp(&marks[i]);
}

void disp(int *n)
{
      printf(“%d”,*n);
}

Output
55 65 75 56 78 78 90

Here, we are passing addresses of individual array element to the function display().Hence, the variable in which this address is collected (n) is declared as a pointer variable.And since n contains the address of array element, to print out the array element we are using the ‘value of address’ operator (*).

Leave a Comment