Loading....
Coupon Accepted Successfully!

 

Two-Dimensional Arrays

Two-dimensional arrays can be considered a rectangular display of elements with rows and columns, and this is also known as a matrix. Consider the following example int x[3][3]. The two-dimensional array can be declared as shown in Figure-Two-dimensional array.
 

Fig: Two-dimensional array

 
Table: Arrangement of two-dimensional array elements
 
 

Column 0

Column 1

Column 2

Row 0

x[0][0]

x[0][1]

x[0][2]

Row 1

x[1][0]

x[1][1]

x[1][2]

Row 2

x[2][0]

x[2][1]

x[2][2]


The arrangement of array elements shown in Table is only for the sake of understanding. Actually, the elements are stored in continuous memory locations. The two-dimensional array is a collection of two one-dimensional arrays. The meaning of the first argument is in x[3][3] and means the number of rows; that is, the number of one-dimensional arrays, and the second argument indicates the number of elements. The x[0][0] means the first element of the first row and column. In one row, the row number remains the same but the column number changes. The number of rows and columns is called the
range of the array. A two-dimensional array clearly shows the difference between logical assumptions and the physical representation of data. The computer memory is linear and any type of array may one, two- or multi-dimensional array it is stored in continuous memory location Figure-Storage of two dimensional array.
 

Fig: Storage of two dimensional array

 

12.11 Program to demonstrate two dimensional array.

#include<iostream.h>

#include<conio.h>

int main()

{

int i,j;

int a[3][3]={1,2,3,4,5,6,7,8,9};

clrscr();

cout<<“\n Array elements and address ”;

cout<<“\n \t Col-0 Col-1 Col-2”;

cout<<“\n \t ====== ====== ======”;

cout<<“\nRow0”;

for (i=0;i<3;i++)

{

for (j=0;j<3;j++)

cout<<“\t ”<< a[i][j];

if(i==2)

break;

cout<<“\nRow”<<i+1;

}

return 0;

}

OUTPUT
Array elements and address


Row0
Row1
Row2
Col-0
======

1
4
7
Col-1
======
2
5
8
Col-2
======
3
6
9
 

Explanation
: From the above program’s output, we can conclude that the one-dimensional array can be accessed using a single loop. However, for the two-dimensional array, two loops are required for rows and columns. The inner loop helps access the row-wise elements, and the outer loop changes the column number.
 

12.12 Program to read marks and percentage of students using two dimensional array.

#include<iostream.h>

#include<conio.h>

int main()

{

int student[5][2],i,j;

clrscr();

for(i=0;i<5;i++)

{

cout<<“\n Enter the Roll no and percentage of the student:”;

cin>>student[i][0]>>student[i][1];

}

cout<<“\n Roll_no \t percentage ”;

for(i=0;i<5;i++)

{

cout<<“\n”;

cout<<student[i][0]<<“\t”<<(student[i][1]);

}

return 0;

}

OUTPUT
Enter the Roll no and percentage of the student: 1 89
Enter the Roll no and percentage of the student: 2 78
Enter the Roll no and percentage of the student: 3 76
Enter the Roll no and percentage of the student: 4 56
Enter the Roll no and percentage of the student: 5 90
Roll_no
1
2
3
4
5
percentage
89

78
76
56
90

Explanation:
 In the above program, student [5][2] is declared as an array. The array student [5][2] contains 5 rows and 2 columns. Roll number and percentage scored by the students are read through the keyboard and displayed.





Test Your Skills Now!
Take a Quiz now
Reviewer Name