Taking inputs from the user, storing them in a (1D-2D-3D) array and printing them on the screen in c++


Hey there. In this lesson we're going to explain (1D - 2D - 3D) arrays in some depth. First, we're going to take a snapshot on single dimensional arrays, inputting and outputting values to/from them. Next, we're going to move on to 2D and 3D arrays , do the same operations on them and finally take examples on each type.

First of all, what in the world is an array? let's take an idea about it.

What is an array in programming?

Away of mathematical concepts, let's assume that we're asked to store a student age. Really easy, right? Just make an integer variable, name it "age", assign the student age value to it, and the world is happy.

int age = value;

But what if we get to store more than one "age" value like 10 records. Creating 10 integers right? and what if they are 100. Creating a hundred integer variable, one after another, would be kind of boring and wasting time. So what is the solution.. Here are arrays came to make our life easer!
With arrays we can store as much as the memory of a computer can bear, all that using a single line of code!

What is the difference between single dimensional arrays and multidimensional ones?

With (2D, 3D, nD) arrays, we can store data in more than one dimension. A good way to visualize that is considering array dimensions as table columns - yes there are better ways to store table data than arrays in C++ but just follow along - when using 2D arrays, the first dimension specifies the number of rows while the second one is specialized for the number of columns.

int array[ROWS][COLUMNS]

Simply, 2D arrays are used when we need to store a huge number of values. 

Side note: In 2D arrays, we can calculate the number of elements it contains by multiplying the number of rows by the number of columns. 

Using 3D arrays provides a much huger number of elements.
More than three-dimensional arrays are harder to manage.

What is the difference between arrays and structs?

Let's get back to the student age example. We said that arrays help us storing 100 age values using a single variable declaration, but students don't have just age values. They actually have names, levels, id numbers and more. From here the idea of structs comes.
 
Let's take a quick example about it, assuming we're asked to store student names, ages, genders and id numbers. We can notice that names are string values, ages and id numbers are integers, and gender can be stored in a char data type variable with two possible values {M, F}.
If we use arrays, we would create four of them.

string name[100];
int age[100];
int id_number[100];
char gender[100];

Notice that they are four distinct values with three data types, and all of them belong to the same entity, which is student. So to make our code more elegant and makes much sense, we can group our four arrays into variables in a struct called "Student" and then create a single array of type "Student":

struct Student
{
    string name;
    int age;
    int id_number;
    char gender;
};

Student newStudent[100];

Now our "newStudent[100]" array is able to store four distinct data for a hundred student.
Our topic for today is not STRUCTS, so we're not going to deep dive in them too much.

How to store user input in a single dimensional array?

As we all know, the program cannot take multiple input from a user at a time. At the same time, arrays usually should get a set of elements not just one, otherwise, they would be all useless. The basic solution you might think of is using each array index in a single cin statement:

int age[3];
cin >> age[0];
cin >> age[1];
cin >> age[2];

We can find that we're getting back to our previous problem, which is that what if age values are 10 or 100. It is unbelievable to use 100 lines of code just to assign age values. We need something that iterates through each index of the array and assigns an entered value to it. The awesome solution is using loops.
Any kind of loops may be sufficient but I usually rather using for loops when iterating through arrays. So let's create one for assigning values to a 10-element array.

int age[10];
for (int i = 0; i < 10; i++)
{
     cin >> age[i];
}

When using loops, we're getting advantage of the initial value to put it as an index of the array. Once an increment happens, we move to the next index of the array. 

That was assigning values to array elements. To print those values out, we just exchange the cin statement with a cout one.

for (int i = 0; i < 10; i++)
{
     cout >> age[i];
}

So far extremely good, we just learned how to implement input and output on single dimensional arrays! 
Before taking some examples, let's move onto multidimensional arrays

How to store user input in a multidimensional array?

You might think that I'm about to tell you something crazy but actually I'm not. Dealing with multidimensional arrays is as simple as dealing with single dimensional ones. Let's take 2D and 3D arrays for instance. To input/output values into/from 2D arrays, we use 2 nested loops.

int twoValues[2][3];

for ( int i = 0; i < 2; i++ )
{
    for (int j = 0; j < 3; j++)
    {
        cin >> twoValues[i][j];
    }
}

To output the elements of the array, just exchange the "cin" statement with "cout".

When inputting/outputting values into/from nD arrays, we use n levels of loop nesting. Here is a 3D array for instance:

int threeValues[2][3][2];

for ( int i = 0; i < 2; i++ )
{
    for (int j = 0; j < 3; j++)
    {
        for (int k = 0; k < 2; k++)
        {
            cin >> threeValues[i][j][k];
        }
    }
}

for ( int i = 0; i < 2; i++ )
{
    for (int j = 0; j < 3; j++)
    {
        for (int k = 0; k < 2; k++)
        {
            cout << threeValues[i][j][k];
        }
    }
}

Congratulations! You're now a master in arrays. All you have to do is to practice what you learned. here are some solved examples: (Note: make sure you try to solve the question yourself before looking at the solution)


Comments

Popular posts from this blog

Finding (positive - negative - odd - even - zero) numbers in an array

Two methods for reversing a text string in c++