Multi-dimensional arrays can be described as ‘arrays of arrays’, that is, each element of the array is itself an array. A multi-dimensional array of dimension n is a collection of items that are accessed with the help of n subscript values.
Generally, the arrays of three or more dimensions are not used because of their huge memory requirements and the complexities involved in their manipulation.
Two Dimensional Array: A two-dimensional array is the simplest form of a. multi-dimensional array that requires two subscript values to access an array element. two-dimensional arrays are useful when data being processed can be arranged in the form of rows and columns (matrix form). The syntax for declaring a two-dimensional array is
data_type array_name [row_size] [column_size] ;
For example, an array a of type int having three rows and two columns can be declared using this statement.
int a[3][2];
Initialization of Two-Dimensional Array: Like a single-dimensional array, a two-dimensional array can also be declared and initialized at the same time. To understand the concept of two-dimensional array initialization, consider this statement.
int a[3] [2]={{101,51},{l02,67},{l03,76} }
In this statement, an array a of type int having three rows and two columns is declared and initialized. This type of initialization is generally used to increase the readability. However, the array a can also be initialized using this statement.
int a[] [2]={101,51,l02,67,l03,76};
In this statement, the first two elements are stored in the first row, next two in the second row and so on. However, this method is least used and some of the c++ compilers may issue a warning also. Note that while initializing a two-dimensional array, mentioning the row size is optional, however, the column size must be specified.
Accessing Two-Dimensional Array Elements: Once a two-dimensional array is declared and initialized, the value stored in the array elements can be accessed using two subscripts. The syntax for accessing the two dimensional array elements is
array_name [row] [column]
The first subscript value (row) specifies the row number and the second subscript value (column) specifies the column number. Both the subscript values specify the position of the array element within the array.
For example, the elements of array a (declared earlier) are referred to as a [0] [0], a [0] [1], a [1] [0], a [1] [1], a [2] [0] and a [2] [1] respectively.
Generally, two-dimensional arrays are represented with the help of a matrix. However, in actual implementation, two-dimensional arrays are always allocated contiguous blocks of memory. Figure shows the matrix and memory representation of two-dimensional array a.
Manipulation of Two-Dimensional Array Elements: The values stored in a two-dimensional array can be manipulated in many ways. Some of the common operations that can be performed on a two-dimensional array include finding the sum of row elements, column elements and diagonal elements, finding the maximum and minimum values, etc.
To understand the concept of operations on two-dimensional array, consider this example.
Example : A program to calculate sum of two matrices
#include<iostream> #include<iomanip> // for setw() using namespace std; int main () { int mat1 [3][3]={ {3,4,5}, // initializing elements {3,2,7}, // of first matrix {7,8,2} }; int mat2 [3][3]={ {2,4,7}, // initializing elements {1,2,2}, // of second matrix {5,3,1} }; int mat3 [3] [3], i, j ; for (i=0;i<3;i++) //calculating sum of two matrices for (j =0; j <3; j ++) / / and storing in third matrix mat3 [i] [j] =mat1 [i] [j ]+mat2[i] [j] ; cout<<"The resultant matrix is : \n"; for(i=0;i<3;i++) //displaying result matrix { for (j =0; j <3; j ++) cout<<setw(4)<<mat3[i] [j]; cout<<endl; } return 0; }
The output of the program is
The resultant matrix is
5 8 12
4 4 9
12 11 3
In this example, the elements of two matrices mat1 and mat2 are added and stored in a third matrix mat 3.
Array of Strings: An array of strings is simply a two-dimensional array of characters where each row Strings’ represents a string. An array of strings is declared and initialized in the same way as any. Two dimensional array of type int, float, etc. To understand the concept of declaring and initializing an array of strings, consider this statement.
char names [3][8]={“Nikhl“,“Raman”,"Nandita"};
In this statement, an array of strings names is declared and initialized with string constants. Note that, the first subscript (3 in this case) in the array specifies the maximum number of strings in the array and the second subscript (8 in this case) specifies the maximum length of each string including the null character.
Array_name[i];
To access a particular string in an array of strings, only one subscript is required. The syntax for accessing a string in an array of strings is where i denote string number in the array. For example, the second string (that is “Raman”) in array of strings name s is accessed by names [1]. The memory representation of the array of strings names is shown in Figure.
To understand the concept of array of strings, consider this example.
Example : A program to reverse each string in an array of strings
#include<iostream> #include<cstring> // for strrev() function using namespace std; canst int str = 3; //number of strings canst int max=10; // maximum length of each string int main () { char names [str][max]={"akshay","smith",“raman", } ; for(int i=0; i<str; i++) { cout<<"Reverse of "<<names[i]<<"is; cout<<strrev(names[i])<<"\n"; return 0; }
The output of the program is
Reverse of akshay is yahska
Reverse of smith is htims
Reverse of raman is namar
In this example, an array of strings names is declared and initialized with string constants. Each string is accessed using names [i] and the reverse of each string is displayed using the strrev () function.