12 Jan Two Dimensional Arrays in C++
In the previous lesson, we saw how to work with Arrays in C++. An array can have two or more dimensions. Here we will discuss about two-dimensional arrays. A two dimensional array as the name suggests has two dimensions, not what we saw in one-dimensional arrays.
Before the example, let us see the syntax,
datatype name_of_array[rows_count][columns_count];
Considering the above syntax, let us see an example, with 3 rows and 3 columns in a two-dimensional array,
int cricket[3][3];
Let us now see how rows and columns work in a two-dimensional array,

Now, let us see a complete example to learn more about two-dimensional arrays.
Example
Let us see a program where we will print the rank and points of 5 cricketers. Here, you can see an array with rows and columns i.e. two-dimensional.
We’re displaying the rank and points for 5 teams, but before that you need to input the values. The cin in C++ is used to input values and cout to get the output,
cin: Standard input stream in C++ cout: Standard output stream in C++
Here, cric[0][1] would mean 1st row and 1st column, cric[0][2] is 1st row and 2nd column, etc
#include <iostream.h>
#include <conio.h>
void main () {
int cric[4][2];
int i, j;
for (i=0;i<5;i++) {
cout<<"\n Enter rank and points for 5 teams:";
cin>>cric[i][0];
cin>>cric[i][1];
}
// displaying output
for(i=0;i<5;i++)
cout<<"\n"<<cric[i][0]<<"\t"<<cric[i][1];
getch();
}
The following is the output,

Here, you saw the following two dimensional arrays,

In this lesson we learned how to work with Multi-Dimensional array in C++.
No Comments