Two Dimensional Array in C++

Two dimensional array in C++ programming is represented in the form of rows and columns, also known as a matrix. It is also known as array of arrays or list of arrays.

Printing Elements of Two Dimensional Array

Program

#include<iostream.h>
#include<conio.h>
void main()
{      //'stud' is name of Two Dimensional Array and assigning
int stud[4][2] = {{1234, 56}, //elements to it.
                  {1212, 33},
                  {1434, 80},
                  {1312, 78}};
for(int row = 0; row < 4; row++) //loop looping on Rows.
{
for(int col = 0; col < 2; col++) //loop looping on Columns.
{
cout<<stud[row][col]<<" "; //and printing Rows and Columns.
}
cout<<endl;
}
getch();
}
//U also Declare Array like
 //'int stud[4][2] = {1234, 56, 1212, 33, 1434, 80, 1312, 78};' And
 //'int stud[][2] = {1234, 56, 1212, 33, 1434, 80, 1312, 78};' And
/* 'int stud[4][2];
    stud[0][0] = 1234;
    stud[0][1] = 56;
    stud[1][0] = 1212;
    stud[1][1] = 33;
    stud[2][0] = 1434;
    stud[2][1] = 80;
    stud[3][0] = 1312;
    stud[3][1] = 78;' */
Output
1234 56
1212 33
1434 80
1312 78

Program User Enter Values in Two Dimensional Array Printing

Program

#include<iostream.h>
#include<conio.h>
void main()
{
int stud[4][2]; //this is array which have 4 Rows and 2 Columns.
for(int row = 0; row < 4; row++)
{
for(int col = 0; col < 2; col++)
{
cout<<"Enter value for stud: "<<row, col;
cout<<"  ";
cin>>stud[row][col]; //user Enter value storing in 'stud' Array.
}
}
cout<<"The Output of Array is:"<<endl;
for(int r = 0; r < 4; r++)
{
for(int c = 0; c < 2; c++)
{
cout<<"  "<<stud[r][c]; //printing Elements of 'stud' Array.
}
cout<<"\n";
}
getch();
}
Output
Enter value for stud: 0 13
Enter value for stud: 0 07
Enter value for stud: 1 55
Enter value for stud: 1 42
Enter value for stud: 2 17
Enter value for stud: 2 86
Enter value for stud: 3 53
Enter value for stud: 3 89
The Output of Array is:
13  7
55  42
17  86
53  89