Programming in C: Unit II (a): Arrays

Storing Values in Arrays

Programming in C

When we declare an array, we are just allocating space for the elements; no values are stored in the array. To store values in the array, there are three ways-first, to initialize the array element at the time of declaration;

STORING VALUES IN ARRAYS

When we declare an array, we are just allocating space for the elements; no values are stored in the array. To store values in the array, there are three ways-first, to initialize the array element at the time of declaration; second, to input value for every individual element at the run time; third to assign values to the individual elements. This is shown in Figure 5.4.

Initializing Arrays during Declaration

Elements of the array can also be initialized at the time of declaration as other variables. When an array is initialized, we need to provide a value for every element in the array.

Programming Tip: By default, the elements of the array are not initialized.They may contain some garbage value, so before using the array you must initialize the array or read some meaningful data into it.

Arrays are initialized by writing,

type array_name [size] = {list of values};

The values are written within curly brackets and every value is separated by a comma. It is a compiler error to specify more number of values than the number of elements in the array. When we write,

int marks [5]={90, 82, 78, 95, 88};

an array with name marks is declared that has enough space to store 5 elements. The first element, i.e., marks [0] is assigned with the value 90. Similarly, the second element of the array, i.e., marks [1] is assigned 82, and so on. While initializing the array at the time of declaration, the programmer may omit the size of the array. For example,

int marks []= {98, 97, 90};

The above statement is absolutely legal. Here, the compiler will allocate enough space for all initialized elements. If the number of values provided is less than the number of elements in the array, the un-assigned elements are filled with zeros. Figure 5.5 illustrates initialization of arrays.


Note

If we have more initializers than the declared size of the array, then a compile time error will be generated. For example, the following statement will result in a compiler error.

int marks [3] {1,2,3,4);

Inputting Values from the Keyboard

An array can be filled by inputting values from the keyboard. In this method, a while/do-while or a for loop is executed to input the value for each element of the array. For example, look at the code shown in Figure 5.6.

In the code, we start with the index i at 0 and input the value for the first element of the array. Since the array can have 10 elements, we must input values for elements whose index varies from 0 to 9. Therefore, in the for loop, we test for condition (i<10) which means the number of elements in the array.

Assigning Values to Individual Elements

The third way is to assign values to individual elements of the array by using the assignment operator. Any value that evaluates to the data type of the array can be assigned to the individual array element. A simple assignment statement can be written as:

marks [3] = 100;

Here, 100 is assigned to the fourth element of the array which is specified as marks [3].

We cannot assign one array to another array, even if the two arrays have the same type and size. To copy an array, you must copy the value of every element of the first array into the element of the second array. Figure 5.7 illustrates the code to copy an array.


In Figure 5.7, the code accesses each element of the first array and simultaneously assigns its value to the cor- responding element of the second array. Finally, the index value i is incremented to access the next element in suc- cession. Therefore, when this code is executed, arr2 [0] = arr1 [0], arr2 [1] = arr1[1], arr2 [2] = arr1 [2] and so on.

We can also use a loop to assign a pattern of values to the array elements. For example, if we want to fill an array with even integers starting (from 0), then we will write the code as shown in Figure 5.8.

In the code, we assign to each element a value equal to twice of its index, where index starts from zero. So after executing this code we will have, arr [0] = 0, arr[1] = 2, arr [2]= 4, and so on. 

Programming in C: Unit II (a): Arrays : Tag: : Programming in C - Storing Values in Arrays