Multi-dimensional lists, often referred to as multi-dimensional arrays or matrices, allow you to store data in a structured format that can be accessed via multiple indices. This is useful in various applications, such as mathematical computations, graphics, and data representation.
A multi-dimensional array is essentially an array of arrays. The most common type is the two-dimensional array, which can be visualized as a matrix with rows and columns. You can also have three-dimensional arrays and higher dimensions, although they are less common.
Declaration: You can declare a two-dimensional array using the following syntax:
dataType arrayName[rows][columns];
Example:
#include <iostream>
using namespace std;
int main() {
const int rows = 3;
const int columns = 4;
int matrix[rows][columns] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
// Display the matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
Matrix (3x4):
1 2 3 4
5 6 7 8
9 10 11 12
In memory, this would be stored sequentially as:
1 2 3 4 5 6 7 8 9 10 11 12
You can also create three-dimensional arrays, which can be visualized as an array of matrices.
Declaration:
dataType arrayName[depth][rows][columns];
Example:
#include <iostream>
using namespace std;
int main() {
const int depth = 2;
const int rows = 3;
const int columns = 4;
int threeD[depth][rows][columns] = {
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
},
{
{13, 14, 15, 16},
{17, 18, 19, 20},
{21, 22, 23, 24}
}
};
// Display the 3D array
for (int d = 0; d < depth; d++) {
cout << "Layer " << d + 1 << ":" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << threeD[d][i][j] << " ";
}
cout << endl;
}
cout << endl;
}
return 0;
}
Higher-dimensional arrays follow the same principles as two- and three-dimensional arrays, with additional indices for each dimension. However, the complexity increases, and they are less frequently used in practice.
Example of a Four-Dimensional Array:
int fourD[2][3][4][5]; // A 4D array
You can also create dynamic multi-dimensional arrays using pointers and new. This approach provides more flexibility, especially when the dimensions are not known at compile time.
Example:
#include <iostream>
using namespace std;
int main() {
int rows = 3;
int columns = 4;
// Create a dynamic 2D array
int** matrix = new int*[rows];
for (int i = 0; i < rows; i++) {
matrix[i] = new int[columns];
}
// Initialize and display the matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = i * columns + j + 1;
cout << matrix[i][j] << " ";
}
cout << endl;
}
// Cleanup
for (int i = 0; i < rows; i++) {
delete[] matrix[i];
}
delete[] matrix;
return 0;
}
Multi-dimensional lists (arrays) are powerful tools for organizing data in a structured manner. They allow for efficient storage and access, especially in mathematical and graphical applications. By understanding the declaration, memory organization, and both static and dynamic approaches to multi-dimensional arrays, you can effectively utilize them in your C++ programs.
Open this section to load past papers