In this C++ tutorial, you will learn about Multidimensional arrays, what is Multidimensional array, how is Multidimensional arrays represented in C++, how to access the elements of a Multidimensional array.
What is a Multidimensional Array?
A Multidimensional array is an array of arrays.
How are Multidimensional Arrays represented in C++
Suppose a programmer wants to represent the two-dimensional array Exforsys as an array with three rows and four columns all having integer elements. This would be represented in C++ as:
int Exforsys[3][4];
It is represented internally as:
Exforsys Data Type: intHow to access the elements in the Multidimensional Array
Exforsys Data Type: intBased on the above two-dimensional arrays, it is possible to handle multidimensional arrays of any number of rows and columns in C++ programming language. This is all occupied in memory. Better utilization of memory must also be made.
Multidimensional Array Example:
#include <iostream>
using namespace std;
const int ROW=4;
const int COLUMN =3;
void main()
{
int i,j;
int Exforsys[ROW][COLUMN];
for(i=0;i < ROW;i++) //goes through the ROW elements
for(j=0;j < COLUMN;j++) //goes through the COLUMN elements
{
cout << "Enter value of Row " << i+1;
cout << ",Column " << j+1 << ":";
cin>>Exforsys[i][j];
}
cout << "nnn";
cout << " COLUMNn";
cout << " 1 2 3";
for(i=0;i < ROW;i++)
{
cout << "nROW " << i+1;
for(j=0;j < COLUMN;j++)
cout << Exforsys[i][j];
}
}
}}
At first, it goes through the elements for ROW 1, with all the COLUMNS from 1 to the max value (3), then procedes to the next value of the ROW which is 2, and the COLUMNS from 1 again to the max value. This ends when the ROW reaches the max value of 4, and COLUMNS the max value of 3.
In the above example, the keyword const (specifying constant) precedes the data type that specifies the variable ROW and COLUMN to remain unchanged in value throughout the program. This is used for defining the array Exforsys ROW size and COLUMN size, respectively.