Arrays — store multiple items using the same variable.

Shafi Sahal
Geek Culture
Published in
5 min readJun 29, 2021

--

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.

The indices are like the locker numbers. Indices are used to identify each array uniquely. The value of an array can be accessed with its index. From the above figure, if index 6 is accessed, the value 89 will be retrieved.

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};

Here, the size of the array do not need to be specified explicitly as the compiler will calculate it automatically form the initialization;

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.

Now, the marks can be stored using a single variable name instead of creating new names for each variable.

Program to store and display any number of marks

Output

Enter the size: 5
Enter mark 1: 78
Enter mark 2: 90.5
Enter mark 3: 76
Enter mark 4: 56
Enter mark 5: 73.5
Here goes the marks...Mark 0 ----------- 78.00
Mark 1 ----------- 90.50
Mark 2 ----------- 76.00
Mark 3 ----------- 56.00
Mark 4 ----------- 73.50

Here, two ‘for loops’ are used: one for storing the array values and the other one for displaying the values. Loops can be used to work with arrays efficiently.

These arrays are 1-dimensional. But there are 2-dimensional arrays too!

2D Arrays

2D arrays are like a 2 x 2 matrix. From the above figure, the number 14 can be accessed using [1][3]. [1] is the row index and [3] is the column index.

Declaring a 2D Array

int x[3][3];

It's the same as declaring a 1D array except for the extra index. The total number of row indices and column indices should be mentioned. Here, an array of 3 rows and 3 columns will be created.

Note: Since arrays start at 0, the three rows will be, row 0, row 1, and row 2. The three columns will be column 0, column 1, and column 2.

A value at row 1 and column 2 can be accessed using x[1][2].

Storing values in a 2D array

It’s simple, 
x[0][0] = 5;
x[0][1] = 7; //and go on till the end
........
........
x[2][2] = 9;

Values can be stored at the time of declaration like this.

int x[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

The matrix format of the above array will be

1 2 3
4 5 6
7 8 9

And it does not stop with 2D, arrays can be 3D, 4D, and more. The principles for using them remain the same.

Make a matrix using a 2D array

Output

Enter number of rows: 3
Enter number of columns: 3
Enter the values in order:
1 2 3
4 5 6
7 8 9
Your matrix...1 2 3
4 5 6
7 8 9

See how a nested for loop is used to enter and display values in a 2D array. The first for loop is used for the row and the second for loop for the column.

Let’s call ‘for loop 1’ as f1 and ‘for loop 2’ as f2.

Variable ‘i’ is incremented in the f1 and variable ‘j’ is incremented in f2. Initially, i = 0 and j = 0. Then after one loop in f2, the values become i = 0 and j = 1 and it goes on in this pattern.

Nested for loops can be used to work with multidimensional arrays.

Previous article => Reapeating Logic in Programming

Next article => String — An array of characters with ‘\0

--

--

Shafi Sahal
Geek Culture

Developer, adventure lover and a truth seeker. Like to write about topics from a unique perspective. Twitter: https://twitter.com/_shafisahal.