Multidimensional Arrays

Filter Course


Multidimensional Arrays

Published by: Zaya

Published date: 17 Jun 2021

Multidimensional Arrays Photo

Multidimensional arrays

In C/C++, we can define multidimensional arrays in simple words as an array of arrays. Data in multidimensional arrays are stored in tabular form (in row-major order).

The general form of declaring N-dimensional arrays:

data_type array_name[size1][size2]….[sizeN];

data_type: Type of data to be stored in the array.
Here data_type is valid C/C++ data type
array_name: Name of the array
size1, size2,… , size: Sizes of the dimensions

The above example represents the element present in the third row and second column.

Note: In arrays, if the size of an array is N. Its index will be from 0 to N-1. Therefore, for row index 2 row number is 2+1 = 3.

 

To output all the elements of a Two-Dimensional array we can use nested for loops. We will require two for loops. One to across the rows and another to across columns.

// C++ Program to print the elements of a
// Two-Dimensional array
#include
using namespace std;

int main()
{
// an array with 3 rows and 2 columns.
int x[3][2] = {{0,1}, {2,3}, {4,5}};

// output each array element’s value
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
cout << “Element at x[” << i
<< “][” << j << “]: “;
cout << x[i][j]< }
}

return 0;
}