How to use multidimensional Arrays in C / C++
In the previous tutorial, we learned about one-dimensional arrays in C / C++. In this tutorial, we will learn about multidimensional arrays
in C/C++.
More specifically, we will learn about the structure, declaration and initialization of multidimensional arrays
. So, make sure you learn about one-dimensional arrays
first.
arrays
are used to store data of the same type. Data in multidimensional arrays are stored in row-major order. It consists of rows and columns, depending on what we declare.In this tutorial, we only learn about two types of multidimensional arrays
:
- Two-dimensional
arrays
in C/C++. - Three-dimensional
arrays
in C/C++.
Now let's get started with us !!!
Declare multidimensional array in C / C++
General syntax for declaring multidimensional arrays
in C / C++:
data_type array_name[size1][size2]....[sizeN];
Here,
- data_type: This is the data type of the elements that the multidimensional
array
can store. - array_name: This is the name of the multidimensional
array
, it is used to call the multidimensionalarray
later. - [size1] [size2]....[sizeN]: This is the size of the
array
, determined by the number of rows and columns that we declare.
Example 1: Declare a two-dimensional array
in C / C++.
int two_d[10][20];
Example 2: Declare a three-dimensional array
in C / C++.
int three_d[10][20][30];
Size of Multidimensional Arrays
:
The size of a multidimensional array
is calculated by multiplying the dimensions of the row and column.
Example 3: The size of the two-dimensional array
.
The array
int number[3][4] can store up to 3 * 4 = 12 (elements).
Example 4: The size of the three-dimensional array
.
The array
int number[3][4][5] can store up to 3 * 4 * 5 = 60 (elements).
Initialize multidimensional arrays in C / C++
Here, we will learn about how to initialize two-dimensional arrays
and three-dimensional arrays
.
Initialize a two-dimensional array
Suppose we want to declare a two-dimensional array
consisting of elements {1, 2, 3, 4, 5, 6}, as shown below:
Then we have the following ways to initialize.
Method 1:
int number[2][3] = {1, 2, 3, 4, 5, 6};
For this method, it is not optimal. Take a look at the second method.
Method 2:
int number[2][3] = {{1, 2, 3}, {4, 5, 6}};
Initialize a three-dimensional array
Similar to how to initialize a two-dimensional array
, we have an example of three-dimensional array
initialization as follows:
Example: Initialize a multidimensional array
consisting of elements {3, 4, 2, 3, 0, -3, 9, 11, 23, 12, 23, 2, 13, 4, 56, 3, 5, 9, 3, 5, 5, 1, 4, 9}.
Method 1:
int test[2][3][4] = {3, 4, 2, 3, 0, -3, 9, 11, 23, 12, 23, 2, 13, 4, 56, 3, 5, 9, 3, 5, 5, 1, 4, 9};
Method 2:
int test[2][3][4] = { { {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} }, { {13, 4, 56, 3}, {5, 9, 3, 5}, {5, 1, 4, 9} } };
Import and export elements in a multidimensional array in C / C++
Here, we take the program that imports and exports the elements in a two-dimensional array
as an example. Other types of multidimensional arrays
are done similarly.
Example: Use the rand() function to enter random elements in a two-dimensional array
. Then display the elements to the screen.
#include <iostream> using namespace std; #define ROW 100 #define COL 100 void ImportMulArrays(int mt[][COL], int &m, int &n) { srand(time(NULL)); cout << "Enter row number m: "; cin >> m; cout << "Enter col number n: "; cin >> n; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { mt[i][j] = rand() % 100; } } } void ExportMulArrays(int mt[][COL], int m, int n) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cout << mt[i][j] << "\t"; } cout << endl; } } int main() { int mt[ROW][COL]; int nRow, nCol; ImportMulArrays(mt, nRow, nCol); ExportMulArrays(mt, nRow, nCol); cout<<"\n--------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
Output:
Examples of multidimensional array in C / C++
Here, we have basic examples of multidimensional arrays
in C / C++.
Example 1: Find the sum of elements by user-entered row or column in a two-dimensional array.
#include <iostream> using namespace std; #define ROW 100 #define COL 100 void ImportArrays(int mt[][COL], int &m, int &n); void ExportArrays(int mt[][COL], int m, int n); int SumRow(int mt[][COL], int n, int d); int SumCol(int mt[][COL], int m, int c); int main() { int mt[ROW][COL]; int nRow, nCol; ImportArrays(mt, nRow, nCol); ExportArrays(mt, nRow, nCol); int d; cout << "Enter row to find of sum: : "; cin >> d; cout << "Sum of row "<<d<<": " << SumRow(mt, nCol, d) << endl; int c; cout << "Enter col to find of sum: : "; cin >> c; cout << "Sum of col "<<c<<": " << SumCol(mt, nRow, c) << endl; cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; } void ImportArrays(int mt[][COL], int &m, int &n) { srand(time(NULL)); cout << "Enter row number m: "; cin >> m; cout << "Enter col number m: "; cin >> n; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { mt[i][j] = rand() % 100; } } } void ExportArrays(int mt[][COL], int m, int n) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cout << mt[i][j] << "\t"; } cout << endl; } } int SumRow(int mt[][COL], int n, int d) { int sum(0); for (int j = 0; j < n; j++) sum += mt[d][j]; return sum; } int SumCol(int mt[][COL], int m, int c) { int sum(0); for (int i = 0; i < m; i++) sum += mt[i][c]; return sum; }
Output:
Example 2: Find the largest element in a two-dimensional array
#include <iostream> using namespace std; #define ROW 100 #define COL 100 void ImportArrays(int mt[][COL], int &m, int &n); void ExportArrays(int mt[][COL], int m, int n); int FindMax(int mt[][COL], int m, int n); int main() { int mt[ROW][COL]; int nRow, nCol; ImportArrays(mt, nRow, nCol); ExportArrays(mt, nRow, nCol); int max = FindMax(mt, nRow, nCol); cout << "Max = " << max << endl; cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; } void ImportArrays(int mt[][COL], int &m, int &n) { srand(time(NULL)); cout << "Enter row number m: "; cin >> m; cout << "Enter col number m: "; cin >> n; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { mt[i][j] = rand() % 100; } } } void ExportArrays(int mt[][COL], int m, int n) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cout << mt[i][j] << "\t"; } cout << endl; } } int FindMax(int mt[][COL], int m, int n) { int i, j, max; max = mt[0][0]; for (i = 0; i < m; i++) for (j = 0; j < n; j++) if (mt[i][j] > max) max = mt[i][j]; return max; }
Output:
Example 3: Initialize and display elements in a multidimensional array
#include <iostream> using namespace std; int main() { int array[2][3][2] = { { {1, 2}, {3, 4}, {5, 6} }, { {7, 8}, {9, 10}, {11, 12} } }; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 3; ++j) { for (int k = 0; k < 2; ++k) { cout << "array[" << i + 1 << "][" << j + 1 << "][" << k + 1 << "] = " << array[i][j][k] << endl; } } } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
Output: