
Lessons  Lesson 18: Multidimensional Arrays  
int numbers [2] [3] ; Using twodimensional arrays, we can do the addition, multiplication and other manipulations of matrices. A value in a twodimensional array is accessed by using the row number and column number. To put values in a twodimensional array is different from the onedimensional array. In onedimensional array, we use a single 'for loop' to populate the array while nested loops are used to populate the twodimensional array. We can do addition, multiplication and other manipulations of twodimensional arrays. In C language, we can declare arrays of any number of dimensions (i.e. 1, 2, 3 … n ). We declare a ndimensional array by putting n pair of brackets [] after the name of the array. So a threedimensional array with values of dimensions 3, 5 and 7 respectively, will be declared as: int num [3] [5] [7] ; Example Code:Let’s have a matrix (twodimensional 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. SolutionTo solve this problem, we use a twodimensional array of two rows and three columns. First, we will declare the array by writing int matrix [2] [3] ; 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 twodimensional 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] 1Please 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

