|Lessons||Lesson 18: Multidimensional Arrays|
int numbers   ;
Using two-dimensional arrays, we can do the addition, multiplication and other manipulations of matrices. A value in a two-dimensional array is accessed by using the row number and column number. To put values in a two-dimensional array is different from the one-dimensional array. In one-dimensional array, we use a single 'for loop' to populate the array while nested loops are used to populate the two-dimensional array. We can do addition, multiplication and other manipulations of two-dimensional arrays. In C language, we can declare arrays of any number of dimensions (i.e. 1, 2, 3 … n ). We declare a n-dimensional array by putting n pair of brackets  after the name of the array. So a three-dimensional array with values of dimensions 3, 5 and 7 respectively, will be declared as:
int num    ;
Let’s have a matrix (two-dimensional array) of two rows and three columns. We want to fill it with values from the user and to display them in two rows and three columns.
To solve this problem, we use a two-dimensional array of two rows and three columns. First, we will declare the array by writing
int matrix   ;
We declare different variables in our program. To put the values in the array, we use two nested for loops, which can be written as under.
for ( row = 0 ; row < maxrows
; row ++ )
The inner for loop totals the elements of the array one row at a time. It fills all the columns of a row. The outer for loop increments the row after each iteration. In the above code segment, the inner loop executes for each iteration of the outer loop. Thus, when the outer loop starts with the value of row 0, the inner loop is executed for a number of iterations equal to the number of columns i.e. 3 in our program. Thus the first row is completed for the three columns with positions [0,0], [0,1] and [0,2]. Then the outer loop increments the row variable to 1 and the inner loop is again executed which completes the second row (i.e. the positions [1,0], [1,1] and [1,2] ). All the values of matrix having two rows and three columns are found. Similarly, to display these values one by one, we again use nested loops. Following is the code of the program.
/*This program takes values from user to fill a two-dimensional array (matrix) having two rows and three columns. And then displays these values in row column format.*/
# include <iostream.h>
A sample output of the program is given below.Please enter a value for position [0,0] 1
Please enter a value for position [0,1] 2
Please enter a value for position [0,2] 3
Please enter a value for position [1,0] 4
Please enter a value for position [1,1] 5
Please enter a value for position [1,2] 6
The values entered for the matrix are
1 2 3
4 5 6
NEXT>>>>>Lesson 19. Pointers