A double dimension array is a matrix type in which we can store values in matrix form.

Write a program to print a matrix according to user's choice.

The above program has one double dimension array variable of integer data type which can store matrix upto 10 x 10, and four integers variables i,j,r,c.

int matrix[10][10]; //for matrix

int i, j; //for loops

int r,c; //for rows and columns

At first we give the message to the user that enter the values of rows and columns and take the inputs and stored the values in variables r and c.

Now the program has two phases one is input phase in which we take the values from the user and second is output phase in which we display the values to the output screen in matrix form.

Suppose r = c = 2;

Nested looping is used for taking the values the outer loop is initialized with i = 0, this loop is for the rows. Now as the loop works i is initialized with 0 and now it will check the condition (i < r) which is true (0 is less than 2), the control will come inside the loop.

Now the second inner loop j is initialized with 0 (j = 0), it will again checks for the condition (j < c) which is true (0 is less than 2) so the control will come inside the loop and will give the message to the user that

enter row i + 1 (0 + 1) col j + 1 (0 + 1) :

i.e.

enter row 1 col 1:

and will take a value and store it in the matrix[i][j]

The values are currently i = 0, j = 0;

so the value will be stored in matrix[0][0]

Now the j in the inner loop will be incremented by 1 and become 1, again it will checks for the condition which is true (1 is less than 2)

again it will take a value and store it in the matrix [i][j]

the values are currently i = 0; j = 1;

so the value will be stored in matrix[0][1]

Now again the j in the inner loop will be incremented by 1 and become 2, again it will checks for the condition which is false (2 is not less than 2).

Now the condition became false so it will again go to the outer loop.

The i will incremented by 1 and became 1, again the outer loop will checks for the condition which is true (1 is less than 2), the control again come inside loop and will come to the inner loop.

Inner loop will again initialized with 0 and will checks for the condition which is true (0 is less than 2) the control will again come inside the inner loop and takes the value and store it in matrix[i][j]

the values are currently i = 1; j = 0;

so the value will be stored in matrix[1][0]

Now the j in the inner loop will be incremented by 1 and become 1, again it will checks for the condition which is true (1 is less than 2)

again it will take a value and store it in the matrix [i][j]

the values are currently i = 1; j = 1;

so the value will be stored in matrix[1][1]

Now again the j in the inner loop will be incremented by 1 and become 2, again it will checks for the condition which is false (2 is not less than 2).

Now the condition became false so it will again go to the outer loop.

The i will incremented by 1 and became 2, again the outer loop will checks for the condition which is false (2 is not less than 2), now the first phase is completed input is over.

Output is almost same as input same nested looping will be used but just the place of input we have to place an output function (cout) and after printing we have to place a tab ("\t") and when one round of inner loop is completed we need to place a new line (endl) then it will be printed in the matrix format.

**Syntax**`data_type array_name[row_size][column_size];`

The array index starts from zero, same concept is applied here suppose we want to create a 3 x 3 matrix for storing value of name matrix then the syntax`int matrix[3][3];`

The value it will store in the matrix forma[00] a[01] a[02]a[10] a[11] a[12]a[20] a[21] a[22]

**Lets understand it with a program**Write a program to print a matrix according to user's choice.

```
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,r,c;
int matrix[10][10];
cout<<"Enter rows and cols:\t";
cin>>r>>c;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<"Enter row "<<i+1<<" col "<<j+1<<":";
cin>>matrix[i][j];
}
}
cout<<endl<<"The matrix entered:\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<matrix[i][j];
cout<<"\t";
}
cout<<endl;
}
getch();
}
```

**Explanation of the program**The above program has one double dimension array variable of integer data type which can store matrix upto 10 x 10, and four integers variables i,j,r,c.

int matrix[10][10]; //for matrix

int i, j; //for loops

int r,c; //for rows and columns

At first we give the message to the user that enter the values of rows and columns and take the inputs and stored the values in variables r and c.

Now the program has two phases one is input phase in which we take the values from the user and second is output phase in which we display the values to the output screen in matrix form.

Suppose r = c = 2;

**Input**Nested looping is used for taking the values the outer loop is initialized with i = 0, this loop is for the rows. Now as the loop works i is initialized with 0 and now it will check the condition (i < r) which is true (0 is less than 2), the control will come inside the loop.

Now the second inner loop j is initialized with 0 (j = 0), it will again checks for the condition (j < c) which is true (0 is less than 2) so the control will come inside the loop and will give the message to the user that

enter row i + 1 (0 + 1) col j + 1 (0 + 1) :

i.e.

enter row 1 col 1:

and will take a value and store it in the matrix[i][j]

The values are currently i = 0, j = 0;

so the value will be stored in matrix[0][0]

Now the j in the inner loop will be incremented by 1 and become 1, again it will checks for the condition which is true (1 is less than 2)

again it will take a value and store it in the matrix [i][j]

the values are currently i = 0; j = 1;

so the value will be stored in matrix[0][1]

Now again the j in the inner loop will be incremented by 1 and become 2, again it will checks for the condition which is false (2 is not less than 2).

Now the condition became false so it will again go to the outer loop.

The i will incremented by 1 and became 1, again the outer loop will checks for the condition which is true (1 is less than 2), the control again come inside loop and will come to the inner loop.

Inner loop will again initialized with 0 and will checks for the condition which is true (0 is less than 2) the control will again come inside the inner loop and takes the value and store it in matrix[i][j]

the values are currently i = 1; j = 0;

so the value will be stored in matrix[1][0]

Now the j in the inner loop will be incremented by 1 and become 1, again it will checks for the condition which is true (1 is less than 2)

again it will take a value and store it in the matrix [i][j]

the values are currently i = 1; j = 1;

so the value will be stored in matrix[1][1]

Now again the j in the inner loop will be incremented by 1 and become 2, again it will checks for the condition which is false (2 is not less than 2).

Now the condition became false so it will again go to the outer loop.

The i will incremented by 1 and became 2, again the outer loop will checks for the condition which is false (2 is not less than 2), now the first phase is completed input is over.

**Output**Output is almost same as input same nested looping will be used but just the place of input we have to place an output function (cout) and after printing we have to place a tab ("\t") and when one round of inner loop is completed we need to place a new line (endl) then it will be printed in the matrix format.

Output of the Progam |

Share if you liked this Post using your favorite sharing service:

Subscribe Updates, Its FREE!