List Of Topics

# Arrays

Until now we used variable which could store only one value at a time. Many times we need to work with a group of data. For e.g. - if we want to input 100 number and perform various operation on them. Using the knowledge obtained till now, we can define 100 variable and store the values of 100 numbers in them. The alternative is to use an array.

In this topic, we will see what is an array. How do we declare, initialize and use an array. How to pass arrays as parameters to functions. We will also study 2- dimensional arrays in brief.

What is an Array?

In order to work with 100 number of same type, instead of defining 100 variable we can use an array. An array can be called as a collection of data items. All the data items should necessarily be of the same type.

All the items in an array have the same name. Then how are they identified would be the question. They are identified with subscript enclosed in square brackets. The subscript of an array generally starts with zero.

E. g. If we want to use 100 numbers
Using variables:

`int var1 = 1, var2 = 10 var3 = 14 ,…………., var100 = 9;`

Using arrays:

`int var[100] = {1,10,14,…………..,9};`

Here ‘var’ is the name of the array, which stores 100 integers. i.e. the capacity of the array is 100. All the elements in the array have the same name ‘var’. Each element is referred to as

Each element in array is identified with subscripts 0, 1, 2,……by the position in the array. Position of first element is 0, position of last element is 99. (It is one less than number of elements in the array). The need of array may have become clear now. It is easier to refer to array elements and use them instead of defining number of variables for the same purpose. Let use write a program to find average marks obtained by 30 students in a test.

/*Program to find average marks obtained by 30 students in a test
Demonstrates use of array */

`#include<stdio.h>void main ( ){float avg, sum = 0;int i;int marks[30]; /* array definition *//* store data inarray */for(i=0;i<=29;i++){printf("n Enter marks :");scanf("%d", &marks[i]);}/* Get data from array */for(i=0;i<=29;i++){sum = sum + marks [i];avg = sum/30;printf("n Average marks = %f ", avg);}}`

We will study the program in parts.

Array definition and declaration
An array is defined like any other variable, except here we have a size – specifier which gives the size i.e. the number of elements in the array. The format is Storage-class data-type array-name[size-specifier (expression)]

E.g.

Storage-class : same rule about storage class apply to array as ordinary variables. Storage class is optional.Default storage class is automatic for arrays defined inside a function and external for array defined outside function.

Data-type : All array elements should have the same data-type. Hence we say array of integers, array of floats etc. we cannot have array of 10 elements out of which 5 are integers and remaining 5 are double.

Array-name: It is the name of the array.

Size-specifier expression: It is also called the dimension of an array. It gives how many elements are present in an array. The size specifier must be a positive integer.
The square bracket is an identification that we are dealing with an array. In Example(1) we have defined int marks[30], marks (name of array) is an array of 30 integers. When we define int marks[30], a memory of 30 * 2 = 60 bytes will be allocated to the array; since there are 30 integers and each integer occupies 2 bytes. Array elements occupy continuous memory locations.

If we define an array in one file and use it in other file in a program, we will require to declare the array in the file we are using it. In array declaration, size of array is optional. If specified it must match with array definition. Initial values cannot be present in array declaration.

Using arrays
Now let us see how to access array elements. The array subscript is used to access individual elements of an array. The following loop in example (1) stores data in the array

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

Variable ‘i’ is used for the subscript. ‘i’ is made to run from 0 to 29 (i.e. 30 array elements). Thus marks[i] refer to individual element of an array (i from 0 to 29). Data is requested from the user and stored in the array by accessing each element one by one in for loop. Since scanf requires address of variable, we use &marks[i] in the statement. ‘&’ is the address operator as explained in the chapter on functions. Thus data is entered in the array.

The following block reads each individual data element of the array again by using the subscript of the array.

`for (i = 0; i <=29; i++)sum = sum + marks [i];`

If we want to operate on two array for e.g. if we want to compare two arrays or perform assignment operation, taking entire array as one operand is not allowed. We have to perform operations on element by element basis. Later on we will study string where entire array can be operated on taking it as a single entity.

Initializing arrays
In example(1) we have assigned values to array elements in the program using the for-loop. But arrays can also be initialized as follows

`int num [5] = {1, 2, 3, 4, 5};`

When we initialize an array it is not necessary to specify the size of the array.

`int num [] = {1, 2, 3, 4, 5};`

The size of the array i.e. 5 will be calculated by the compiler. The individual array elements are assigned values in the order in which they appear.
num[0] = 1
num[1] = 2
num[2] = 3
num[3] = 4
num[4] = 5
If not initialized, static and external array elements contain a value of ‘0’. Automatic array elements contain garbage value.

Passing array as parameters to function

• Passing individual elements of array.
• Passing entire array.

Array can be passed to functions, either the whole array or the individual elements of the array.

Passing individual of array
Individual elements are passed to functions and returned from them in the same way as normal variables are passed and returned.
The following example shows how individual elements of array num are passed to the function display().

/* program demonstrate passing of individual array elements to function */

`#include<stdio.h>main( ){int i;void display(int);int num[5] = {1, 2, 3, 4, 5};for(i=0;i< 5;i++)display(num [i]);}void display(int x){printf("n%d ",x);}`

When we pass individual values of array to function, it can be done in similar manner to ordinary variables i.e. they can be passed by value or by reference.

Passing entire array
But when passing the entire array as a parameter there is a lot of difference. While passing an array to function, as an actual argument only the name must be passed; without the square brackets and the subscripts.
The corresponding formal argument is written in the same way but it must be declare inside the function. The formal declaration of the array should not contain the array dimension. In the
declaration of function using arrays as arguments, a pair of empty square brackets must follow the data type of array argument (or name of array if given).

The following example shows us how entire array is passed to function. The example program is just a skeleton.

In the chapter on function we have seen that argument can be passed by reference or by value. Which method is used while passing array? We have seen a brief explanation of address and pointers earlier.

Let us see how array elements are stored on memory. As said previously array elements are stored in contagious memory locations. The name of the array by itself represents the address of the first element of the array. Taking example of the array ‘num’ which an array of 5 integers. The name ’num’ is the address of the first element, which is ‘1’. num[0] is the first element. Using the ampersand notation &num[0]. Hence ‘num’ and &num[0] both address the first array element.

Adding the subscript of the array element to array name gives the address of the next element, i.e. address of second element is (num + 1) and is same as &num[1].
In short, ‘num’ is the address of the first element of the array and (num + i) is the address of the ith element (which is same as &num[i]). num[i] is the ith element

Going back to previous example; when passing entire array to function, we are passing the name of the array (‘list’). Thus passing array is by reference. Any change in any element of the array will be seen throughout the entire scope of the array definition.

 e-University Search   Related Jobs Query failed: connection to 192.168.2.5:9312 failed (errno=111, msg=Connection refused). No Jobs Found.