You are on page 1of 6

Two - Dimensional Array

Two-dimensional arrays are stored in a row-column matrix, where


the left index indicates the row and the right indicates the
column.

Col 0 Col 1 Col 2 Col 3


row 0 [0][0] [0][1] [0][2] [0][3]
row 1 [1][0] [1][1] [1][2] [1][3]
row 2 [2][0] [2][1] [2][2] [2][3]
row 3 [3][0] [3][1] [3][2] [3][3]

IT 105 – Computer Programming 2


Declaring Two-Dimensional Array

A typical declaration of two-dimensional array is:

data type arrayname[rows][columns];

Example:
int num [2][3] ;

num[0][0] num[0][1] num[0][2]


num[1][0] num[1][1] num[1][2]

IT 105 – Computer Programming 2


Initializing Two-Dimensional Array
Two–dimensional array can be initialized as:

int num[2][3] = {{ 10, 20,30},{40,50,60}};

10 20 30

40 50 60

IT 105 – Computer Programming 2


Processing Two- Dimensional Array

A two-dimensional array can be processed in three


ways:

1. Process a particular row of the array, called row


processing.

2. Process a particular column of the array, called


column processing.

3. Process the entire array.

IT 105 – Computer Programming 2


Processing Two- Dimensional Array
#include <iostream> 2D Array Column Processing
using namespace std;
int a[3][5],x;

int main()
{
for( x=0;x<=4;x++)
{
cout<< "Rectangle "<< x +1<<endl;
cout<< "Length : " ;
cin>> a[0][x];
cout<< "Width : ";
cin>> a[1][x];
a[2][x]= a[0][x] * a[1][x];
cout<< " Area : " << a[2][x];
cout<<"\n\n\n";
}
return 0;
}

IT 105 – Computer Programming 2


Processing Two- Dimensional Array
#include <iostream> 2D Array Row Processing
using namespace std;
int a[5][3],x;

int main()
{
for( x=0;x<=4;x++)
{
cout<< "Rectangle "<< x +1<<endl;
cout<< "Length : " ;
cin>> a[x][0];
cout<< "Width : ";
cin>> a[x][1];
a[x][2]= a[x][0] * a[x][1];
cout<< " Area : " << a[x][2];
cout<<"\n\n\n";
}
return 0;
}

IT 105 – Computer Programming 2

You might also like