Professional Documents
Culture Documents
ch05 (4) - Multi Dim Arrays
ch05 (4) - Multi Dim Arrays
Multi-Dim Arrays
Example:
// a typical matrix
int matrix[2][4];
Notice that C++ does not follow the notation used in other languages:
matrix[10,12] // Not C++
To access an element of the matrix we use the notation:
matrix[1][2] = 10;
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[0][2] = 3;
matrix[0][3] = 4;
matrix[1][0] = 10;
matrix[1][1] = 20;
matrix[1][2] = 30;
matrix[1][3] = 40;
Practical C++ Programming Copyright 2003 O'Reilly and Associates Page3
2-Dimensional Arrays
Sometimes you may want to use similar data that would
normally be stored in a table rather than a list (1-D
array).
You can envision this table as a collection of data stored
in rows and columns.
This data must be all the same data type.
For example, suppose we had a collection of daily
temperature data taken every three hours for a week.
Number of
Data type Array name Number columns
of rows
_W_W_W_W
W_W_W_W_
_W_W_W_W
________
________
B_B_B_B_
_B_B_B_B
B_B_B_B_
2. Use nested for loops to store and display the following 2D array
12345
2 4 6 8 10
4. Let the user enter data into a 2 by 3 array and neatly display data
Practical C++ Programming Copyright 2003 O'Reilly and Associates Page15