Arrays — store multiple items using the same variable.

Variables are used to store values. Each variable is like a box. The box will contain a value. The box can be opened and a value can be stored in it. Then the value can be used whenever it is needed. Just open the box and take the value. It’s very useful

But imagine a scenario where the marks of five subjects should be stored. To do this, five variables can be created: subjectt1, subject2, subject3, subject4, and subject5, and the marks of each subject can be stored in it.

**But what if there is a need to store the names of a hundred students? **Creating a hundred variables and storing values manually will very easily drive everyone crazy. Here come arrays as savior.

Arrays

If variables are like boxes, arrays are like lockers. Every locker in the same compartment store similar items. Each locker can be identified uniquely using the locker number. Each locker is placed sequentially in a single compartment.

Likewise, arrays are placed sequentially.

Declaring an Array

int marks[5];

Declaring array is the same as declaring variables except for the last square braces and the number in it. The square braces indicate that the particular variable is an array. The number 5 tells the computer to allocate space for storing 5 values. This number is known as the size of the array. If arrays were lockers, the number within the braces tells how many lockers are needed. Here 5 lockers will be allocated.

Storing value in the array

marks[0] = 90;
marks[1] = 80;
marks[2] = 45;
marks[3] = 100;
marks[4] = 67;

In the LHS, the index and the array, where the value should be stored are given and on the RHS, the values are given. In order to access a value just use the array name with the index. ‘marks[3]’ will give the value 100, marks[2] will give the value 45, and so on.

All the indices can be assigned together at the time of declaration like this

int marks[] = { 90, 80, 45, 100, 67};

Note: The indices of an array of size 5 are 0, 1, 2, 3, 4. It’s because arrays start at 0. Index 0 will be the first position of the array.

#matrix #arrays #c-programming #programming

Arrays — store multiple items using the same variable.