Page 294 - Computer_Science_F5
P. 294
Output: Chapter Four: Object oriented programming with C++
FOR ONLINE READING ONLY
Figure 4.63: Output of the program
Multidimensional array
One-dimensional array represents linear data structure. In order to represent
n-dimensional structure you are required to use multidimensional array. For example,
we use two dimensional array to describe a matrix or table.
Declaration of two – dimensional array
The syntax to declare two-dimensional array is given as
dataType arrayName[row size][column size];
An example of a two-dimensional array is int matrix[3][3]; The array contains three
elements in a row and four elements in a column. Such an array can be tabulated as
shown in Table 4.16:
Table 4.16: Two-dimensional array
matrix[0][0] matrix[0][1] matrix[0][2]
matrix[1][0] matrix[1][1] matrix[1][2]
matrix[2][0] matrix[2][1] matrix[2][2]
Initialisation of two-dimensional array
Initialise two-dimensional arrays by listing all elements horizontally or arranging
members according to the feature of the array then enclosed them by curl brackets.
Here is the syntax for initialising two-dimensional arrays,
dataType arrayName[row size][column size] = {list of values};
Example: int matrix[1][6] = {0,2, 7, 8, 9, 5}
or
int matrix[3][2]=
{
{0,2},
{7,8},
{9, 5},
};
285
Student’s Book Form Five
Computer Science Form 5.indd 285 23/07/2024 12:34

