Introduction to Two-Dimensional Arrays
Two-dimensional arrays, the most common multidimensional arrays, are used to store information that we normally represent in table form. Two-dimensional arrays, like one-dimensional arrays, are homogeneous. This means that all of the data in a two-dimensional array is of the same type. Examples of applications involving two-dimensional arrays include:
- a seating plan for a room (organized by rows and columns),
- a monthly budget (organized by category and month), and
- a grade book where rows might correspond to individual students and columns to student scores.
Declaration of Two-Dimensional Arrays
Example: The following declarations set aside storage for a two-dimensional array called labScores which contains 40 rows and 14 columns. Rows correspond to a particular student and columns correspond to a particular lab score.
const int JAN_WAN = 10;
const int SOURCECODEERA_WAN = 20;
int SOURCECODE [JAN_WAN][ SOURCECODEERA_WAN];
Example: A sample of a 2-D array initialize to 0
#include<iostream> //Allow program to preform input/output operation
using namespace std; //Allow to group entities like class
int main()
{
//Declaration of 2D array variable
int arr[8][12] = {0}; //Initialize array to 0. Creates a 8*12 matrix or a 2d array
//A nested for loop is use to go through the element within the array
for(int r = 0; r < 8; r++) //(The Row) This loop will go one time while
{
for(int c = 0; c < 12; c++) //(The Column) This loop will go 12 times
{
cout<<arr[r][c]; //Display the element within the array
}
cout<<endl; //New line
}
system("pause");
return 0;
}
Exercise 1:
Show C++ statements to declare the following two-dimensional array:
Declare a two-dimensional array which can be used to store a yearly budget. Each row of the array corresponds to a particular budgeted item like rent, electric, etc. There are at most 15 items to be budgeted. Each column of the array corresponds to a month, January, February, etc. Of course there are 12 columns corresponding to the 12 months of the year. All the data to be placed in the array consists of real numbers.
Manipulation of a two-dimensional array requires the manipulation of two indices. When the two-dimensional array labScores is declared, enough storage is set aside to hold a table containing 40 rows and 14 columns for a total of 40 * 14 = 560 integer values. To access one particular value, we must specify the row and column. The row index ranges from 0 to MAX_STUDENTS-1 (39) and the column index ranges from 0 to MAX_LABS-1 (13). Thus the table can be visualized as:
This two-dimensional array may also be visualized as a one-dimensional array of arrays. An alternative view of this array would be:
This two-dimensional array may be viewed as a one-dimensional array having 40 elements where each element is an array of 14 values.
Accessing a Two-Dimensional Array Element
In our labScores example, suppose we wish to indicate that the second student (corresponding to row 1) made a 90 on lab 10 (corresponding to column 9). We might use the statement:
labScores [1][9] = 90;
Array indices may be integer constants (as in the above example), variables, or expressions. They should be within the bounds of the array.
Two-Dimensional Array Initialization
We can declare and initialize an array A as follows:
//declaration
int A[3][4] = {{8, 2, 6, 5}, //row 0
{6, 3, 1 ,0}, //row 1
{8, 7, 9, 6}}; //row 2
Memory for the array may be visualized as:
J.W. Production