
Multidimensional ArraysThere may be many applications of arrays in daily life. In mathematics, there are many applications of arrays. Let’s talk about vectors. A vector is a set of values which have independent coordinates. There may be twodimensional vector or threedimensional vector. There are dot and cross products of vectors besides many other manipulations. We do all the manipulations using arrays. We manipulate the arrays with loops. Then there is a mathematical structure matrix, which is in rows and columns. These rows and columns are manipulated in twodimensional arrays. To work with rows and columns, C provides a structure i.e. a twodimensional array. A two dimensional array can be declared by putting two sets of brackets [] with the name of array. The first bracket represents the number of rows while the second one depicts the number of columns. So we can declare an array numbers of two rows and three columns as follows. 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] 1 