TWO-DIMENSIONAL ARRAY
A common use of two-dimensional array is to represent tables of values consisting of information arranged in rows and columns. The figure below shows the two-dimensional array named a. The array contains four rows and three columns, so it is said to be a 4-by-3 ( 4x3) array. The array has a total of 12 elements. In general, an array with m rows and n columns is called an m-by-n array. Two-dimensional array requires two subscripts to identify a particular element: the first (by convention) identifies the element's row, and the second (by convention) identifies the element's columns.
a[0][0] | a[0][1] | a[0][2] |
a[1][0] | a[1][1] | a[1][2] |
a[2][0] | a[2][1] | a[2][2] |
a[3][0] | a[3][1] | a[3][2] |
Every element in an array idenfied by an element name fo the form a[i][j]; a is the name of the array, and i and j are the subscripts that uniquely identify each element in a.
DECLARING A TWO-DIMENSIONAL ARRAY
Syntax Data type Array-name[ROW-SIZE][COLUMN-SIZE];
Example |
|
Note: Declare an array without specifying its array size will cause a syntax error.
Example: int test [ ][ ]; //Illegal array declaration
INITIALIZING A TWO-DIMENSIONAL ARRAY
A two-dimensionla array is initialized row by row.
Syntax Data type Array-name[ROW-SIZE][COLUMN-SIZE] = { {values of row1},{ values of row2}, ........};
or Data type Array-name[ ][ ] = { {values of row1},{ values of row2}, ........};
Example | |||||||||||||
|
//Declare and initialize a two-dimensional array named grades on the //left
int grades[3][4] = { {10,20,30,40}, {35,25,15,5},{100,10,68,75}}; |
INPUT/OUTPUT A TWO-DIMENSIONAL ARRAY
You use two nested for loop to input values for all elements in an array or to display the values of the array's elements. The input is done row by row.
Example | Output |
const int ROWSIZE = 4; const int COLSIZE=3; void main( ) { int grades[ROWSIZE][COLSIZE]; cout<<"Enter input:\n"; for ( int i=0 ; i < ROWSIZE ; i++) for (int j = 0; j<COLSIZE;j++) cin >> grades[i][j]; cout<<"The values of the grades array's elements\n"; for ( i=0 ; i < ROWSIZE ; i++) { for ( j = 0; j<COLSIZE;j++) cout << grade[i][j]<< " "; cout<<endl; } } |
Enter input: 10 20 30 40 35 25 15 5 100 10 68 75 The values of the grades array's elements 10 20 30 40 35 25 15 5 100 10 68 75 |
ACCESSING A TWO-DIMENSIONAL ARRAY ELEMENTS
Example | Comments |
const int ROWSIZE = 2; const int COLSIZE = 2; void main( ) { int test[ROWSIZE][COLSIZE]; for ( int i=0 ; i < ROWSIZE ; i++) for (int j = 0; j<COLSIZE;j++) { test[i] = 10; } } |
In this example, you use a nestedfor loop to assign a value of 10 to each element of the array test. test[0][0]contains 10. test[0][1] contains 10. test[1][0] contains 10. test[1][1] contains 10. |
Example |
const int ROWSIZE = 3; const int COLSIZE = 3; void main( ) {int num[ROWSIZE][COLSIZE]={ {1,5,2}, { 12,10,30}, {4,8,25}}; int test[ROWSIZE][COLSIZE]; //Copy values from num array to test array for ( int i=0; i < ROWSIZE ; i++) for(int j=0; j<COLSIZE;j++) test[i][j] == num[i][j]; // Add 5 to the element num[2][1] num[2][1] = num[2][1] + 5 ; //num[2][1]= 8 + 5 = 13 // Compare num[2][0] and num[1][1] if ( num[2][0] > num[1][1] ) cout << " num[2][0] is greater than num[1][1]"<<endl; //Display test[2][2] and test[3][1] cout<<test[2][2]<< ' '<<test[3][1]<<endl;} |
Example |
const int ROWSIZE = 3; const int COLSIZE = 3; int num[ROWSIZE][COLSIZE]={ {1,5,2}, { 12,10,30}, {4,8,25}}; num[3][2] = 12; //Overrun an array element. The array num has only three rows: row 0, row 1 and row 2, not row 3. |
PASSING A TWO-DIMENSIONAL ARRAY TO A FUNCTION
Function declaration Return data type function-name( data type [ ][ ] );
Function definition Return data type function-name( data type array-name[ ]{COLSZIZE] ,ROWSIZE,COLSIZE)
{ Function body;
}
Function calling function-name(array-name)
Example |
#include<iostream.h> #include<iomanip.h> const int ROWSIZE = 3; const int COLSIZE = 3; void addFive(int [ ][ ],int, int); void main( ) {int num[ROWSIZE][COLSIZE]={ {1,5,2}, { 4 ,6 ,8}, { 9, 3, 7}}; addFive(num, ROWSIZE,COLSIZE); // passing array name for (int i =0; i < ROWSIZE ; i++ ) { for(int j = 0; j < COLSIZE; j++) cout << setw(4) << num[ i ][ j ]; cout<<endl; } } void addFive( int x[ ][COLSIZE], int rows, int cols) //. { for (int i =0; i < rows ; i++ ) for(int j = 0; j < cols; j++) x[ i ][j] = x[ i ][ j ] + 5; } |
Output |
6 10 7 9 11 13 14 8 12 |
The next example, you pass an item of the array to a function.
Example |
#include<iostream.h> #include<iomanip.h> const int ROWSIZE = 3; const int COLSIZE = 3; void addFive(int); void main( ) {int num[ROWSIZE][COLSIZE]={ {1,5,2}, { 4 ,6 ,8}, { 9, 3, 7}}; addFive(num[2][1]); //pass a value of 3 to the function addFive(num[1][0]); //pass a value of 4 to the function cout<<"Display the items in the array num"<<endl; for (int i =0; i < ROWSIZE ; i++ ) { for(int j = 0; j < COLSIZE; j++) cout << setw(2) << num[ i ][ j ]; cout<<endl; } } void addFive( int value) { value = value + 5; cout<< value <<endl; } |
Output |
8 9 Display the items in the array num 1 5 2 4 6 8 9 3 7 |
COPYING TWO-DIMENSIONAL ARRAY
To copy values from one array to another array, you must use two nested for loop.
Example |
int num[ROWSIZE][COLSIZE]={ {1,5,2}, { 12,10,30}, {4,8,25}}; int test[ROWSIZE][COLSIZE]; //Copy values from num array to test array for ( int i=0; i < ROWSIZE ; i++) for(int j=0; j<COLSIZE;j++) test[i][j] == num[i][j]; |