Part 7: Arrays

Ayesha Sahar - Dec 16 '21 - - Dev Community

Hey folks, welcome to the 7th part of my series, C++: Explain like I’m five. In the last part, we discussed functions and their types. It is time to move on to Arrays.

So without further ado, let's get started.

Arrays

Sometimes a normal variable is not enough to hold the amount of data we want to store. For example, if we want to store the marks of 100 students, having 100 different variables for this task is not feasible. But what we can do is to define an array with a size 100 that can hold the marks of all students. Therefore we can say that arrays are a special data type that has a name, data type, and size. Moreover, they occupy contiguous areas in memory.

Declaration of Arrays

You can declare arrays in the following ways:

Method 1:

int arr[5];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
arr[4] = 5;
Enter fullscreen mode Exit fullscreen mode

Method 2:

int arr[5] = {10, 29, 3, 40, 5};
Enter fullscreen mode Exit fullscreen mode

Method 3:

int arr[ ] = {1, 2, 3, 4, 5};
Enter fullscreen mode Exit fullscreen mode

Accessing the elements of an array

Array index starts with 0. This means that the first array element is at index 0 so the second one will be at index 1and so on. We can display all the elements of an array by using the index number information but that is not recommended. The use of loops is much more ideal for this task just like in the example below.

Example:

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
    int i;
    int z[10];
    cout<<"Enter elements in array z: "<<endl;
    for(i=0; i<10;i++){
        cin>>z[i];
    }

    cout<<"Entered elements in ascending order are: "<<endl;
    for(i=0; i <= 10;i++){
        cout<<z[i]<<endl;
    }

    return 0;
}

Enter fullscreen mode Exit fullscreen mode

Run this code by yourself and take a look at the output. It's quite useful and interesting, isn't it?

Multidimensional Arrays

In simple words, they are known as arrays of arrays.

Syntax:

Declaring a two-dimensional array:

int arr[4][5];
Enter fullscreen mode Exit fullscreen mode

This array has total 4*5 = 20 elements.

Declaring a three-dimensional array:

int arr[3][3][3];
Enter fullscreen mode Exit fullscreen mode

This array has total 3*3*3 = 27 elements.

Two-dimensional array

We saw how to declare a 2D array above so now, let's look at how to access the elements of the array.

arr[0][0] – 1st element

arr[0][1] – 2nd element

arr[0][2] – 3rd element

arr[1][0] – 4th element

Since we know the basics, to deepen our understanding, let's take a look at an example.

Example:

#include<iostream>
using namespace std;

int main()
{
    int x[3][2] = {{0,1}, {2,3}, {4,5}};

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            cout << "Element at x[" << i
                 << "][" << j << "]: ";
            cout << x[i][j]<<endl;
        }
    }

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:

Element at x[0][0]: 0
Element at x[0][1]: 1
Element at x[1][0]: 2
Element at x[1][1]: 3
Element at x[2][0]: 4
Element at x[2][1]: 5
Enter fullscreen mode Exit fullscreen mode

Three-dimensional arrays

Three-dimensional arrays are just like 2D ones, but as the name suggests have 3 dimensions.

Example:

// C++ program to print elements of Three-Dimensional
// Array
#include<iostream>
using namespace std;

int main()
{
    // initializing the 3-dimensional array
    int x[2][3][2] =
    {
        { {0,1}, {2,3}, {4,5} },
        { {6,7}, {8,9}, {10,11} }
    };

    // output each element's value
    for (int i = 0; i < 2; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            for (int k = 0; k < 2; ++k)
            {
                cout << "Element at x[" << i << "][" << j
                    << "][" << k << "] = " << x[i][j][k]
                    << endl;
            }
        }
    }
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:

Element at x[0][0][0] = 0
Element at x[0][0][1] = 1
Element at x[0][1][0] = 2
Element at x[0][1][1] = 3
Element at x[0][2][0] = 4
Element at x[0][2][1] = 5
Element at x[1][0][0] = 6
Element at x[1][0][1] = 7
Element at x[1][1][0] = 8
Element at x[1][1][1] = 9
Element at x[1][2][0] = 10
Element at x[1][2][1] = 11
Enter fullscreen mode Exit fullscreen mode

Well, that's all for today. Understanding arrays is quite easy. Just remember to practice some array questions like multiplying, adding, or subtracting matrices. In the next part, we will cover strings and structs so stay tuned :)

Note: To see the examples of some basic C++ programs, take a look at my github repo here

Let's connect!

Twitter

Github

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .