Introduction
An array is the collection of similar objects stored in a contiguous memory location. The dimension of an array determines the number of arrays present in an array. Multi-dimensional arrays store data in tabular form.
A single-dimensional array has just a single kind of multiple values that can be imagined to be linearly stored. In contrast, in a multi-dimensional Array, the data arranged is in the form of rows and columns or many rows and columns.
The size of the array determines the dimension of an array. For example, a two-dimensional array would include two sizes that would be arranged in the form of a table. Each element in an array has a unique index that determines its position within the array.
Two-dimensional arrays, three-dimensional arrays, four-dimensional arrays are all multi-dimensional arrays. In fact, a two-dimensional array is the simplest multi-dimensional array. Let us start our discussion with multi-dimensional arrays.
Let us now discuss the initialization of a multi-dimensional array:
Data_type name_of_array [size1] [size2] [size3].... [size n];
Also see, Literals in C.
Two Dimensional Array
As stated earlier, a two-dimensional array is the simplest type of multi-dimensional array. It can be visualized as a table with n number of rows and columns.
Initialization of a two-dimensional array is:
data_type array_name [size1] [size2];
An example for this is:
int rank [4] [4];
Number of elements= 4 *4= 16.
Initialization of a two dimensional Array:
One method of initialization is to assign all the values all at once at the time of declaration of the array.
For example:
int rank [4] [4] = { 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11, 12,13,14,15,16};
Another better way to initialize the array is to club together different elements of a particular row.
int rank [4][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16}}
Accessing Elements in a Two dimensional Array
Let the above table represent the above-defined array. The first element in the array is generally referenced as rank [0][0], the next element in the same row is referenced as rank [0][1]. The elements of the next row can be accessed as rank[1] [0], rank [1][1].
Hence, to access a particular element, we reference it using the index. The syntax for it can be defined as:
array_name [number of row] [number of column];
Example:
rank [3] [4]; // gives us the value of the third row and fourth column
Let us now look at a code segment that helps us understand how are elements of a two-dimensional matrix accessed.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i, j; // intializing variables i, j
// intializing the value of row and column of a matrix
int row = 6;
int column = 4;
int rank[row][column];
// for very column i we go through every row
for (i = 0; i < column; i++) // loop helps us traverse through columns in the array
{
for (j = 0; j < row; j++) // loop helps us traverse through every row in the array
{
rank[i][j] = i + j + 1; // initialising the value of a particular element
}
}
for (i = 0; i < column; i++) // loop helps us traverse through columns in the array
{
for (j = 0; j < row; j++) // loop helps us traverse through every row in the array
{
cout << rank[i][j] << " "; // printing the value of a particular element
}
cout << endl;
}
return 0;
}
Output:
1 2 3 4 2 3
2 3 4 5 3 4
3 4 5 6 4 5
4 5 6 7 8 9
Try and compile with online c++ compiler.