2.0+Initializing+Array

How to initialize an array?

After declaration, most probably you would want to put in values into your array. The process of 'putting in values' can be refer to as 'assigning' or 'initializing'. Although technically, initializing refers to the very first time a value is allocated to an array element.

For demonstration purpose we will make use of int Count[5]; //An integer array of 5 elements name Count

Currently, there are no values assigned to every element in the array.

If I want to assign 23 to the first element in the array Count. 42 to the second element in the array Count. 10 to the third element in the array Count. 36 to the fourth element in the array Count. And lastly, 8 to the fifth element in the array Count.

We can access each element in the array by using the Array name follow by it index value eg. To access the first element in Count, we use Count[0] To access the second element in Count, we use Count[1] Note that we start counting from 0 when accessing elements in array. Unlike declaration, we start counting from 1.

Lets get down to assigning values to array. Method 1: Assign them 1 at a time using the '=' operator

Step 1: Count[0] = 23; //assigning 23 to first element in array Step 2: Count[1] = 42; //assigning 42 to first element in array Step 3: Count[2] = 10; //assigning 10 to first element in array Step 4: Count[3] = 36; //assigning 36 to first element in array Step 5: Count[4] = 8; //assigning 8 to first element in array

This is a tedious process. If you have a 100 numbers to update in an array with 100 elements, this method is not recommended.

Suggest a more efficient method to update an array.