Multidimensional Arrays
COS1511 - Introduction To Programming I · Learning C++
Multidimensional Arrays
Multidimensional arrays are arrays that contain more than one dimension. The most common type is the two-dimensional array, which can be thought of as a table with rows and columns. Each element in a multidimensional array can be accessed using multiple indices.
Declaring Multidimensional Arrays
To declare a two-dimensional array in C++, you specify the type of the elements, followed by the name of the array and the sizes of each dimension in square brackets. For example:
int myArray[3][4];This declaration creates an array named myArray with 3 rows and 4 columns. The total number of elements in this array is 3 × 4 = 12.
Remember: The first index represents the row, and the second index represents the column.
Initialising Multidimensional Arrays
You can initialise a two-dimensional array at the time of declaration. For example:
int myArray[2][3] = { {1, 2, 3}, {4, 5, 6} };This creates a 2 × 3 array with the first row containing 1, 2, 3 and the second row containing 4, 5, 6.
Accessing Elements in Multidimensional Arrays
To access an element in a multidimensional array, you use the array name followed by the indices in square brackets. For example, to access the element in the second row and third column:
int value = myArray[1][2];This code assigns the value 6 to the variable value, as it corresponds to the element in the second row and third column of myArray.
Watch out: Remember that array indices start at 0. Therefore, myArray[1][2] refers to the third element in the second row.
Iterating Through Multidimensional Arrays
To iterate through a two-dimensional array, you can use nested loops. The outer loop iterates through the rows, and the inner loop iterates through the columns. Here is an example:
for (int i = 0; i < 2; i++) { // Iterate through rows
for (int j = 0; j < 3; j++) { // Iterate through columns
std::cout << myArray[i][j] << " ";
}
std::cout << std::endl; // New line after each row
}This code will print all the elements of myArray in a tabular format.
Multidimensional Arrays with More Than Two Dimensions
While two-dimensional arrays are common, C++ also supports arrays with more than two dimensions. The syntax is similar. For example, to declare a three-dimensional array:
int my3DArray[2][3][4];This creates an array with 2 layers, 3 rows, and 4 columns. You can access an element in a three-dimensional array using three indices:
int value = my3DArray[1][2][3];Example: Using a Multidimensional Array
Let’s create a simple program that uses a two-dimensional array to store and display the marks of students in different subjects:
#include
using namespace std;
int main() {
int marks[3][4] = { {75, 85, 90, 80},
{65, 70, 75, 80},
{85, 90, 95, 100} };
cout << "Marks of Students:\n";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cout << marks[i][j] << " ";
}
cout << endl;
}
return 0;
}This program declares a 3 × 4 array called marks and initialises it with the marks of three students in four subjects. It then uses nested loops to print the marks.
Dynamic Multidimensional Arrays
In C++, you can also create multidimensional arrays dynamically using pointers and the new operator. Here is an example of how to create a dynamic two-dimensional array:
int rows = 3;
int cols = 4;
int **dynamicArray = new int*[rows];
for (int i = 0; i < rows; i++) {
dynamicArray[i] = new int[cols];
}In this code, we first allocate memory for an array of pointers, and then for each pointer, we allocate an array of integers.
Tip: Remember to free the memory allocated for dynamic arrays using the delete operator to avoid memory leaks.
Freeing Dynamic Multidimensional Arrays
To free a dynamic two-dimensional array, you need to delete each row first, and then delete the array of pointers:
for (int i = 0; i < rows; i++) {
delete[] dynamicArray[i];
}
delete[] dynamicArray;Common Mistakes with Multidimensional Arrays
Watch out: A common mistake is to forget that the first index represents the row and the second index represents the column. This can lead to accessing the wrong element.
Watch out: When using dynamic arrays, always ensure that you free the memory you allocate. Failing to do so can lead to memory leaks.
Summary
- Multidimensional arrays allow you to store data in more than one dimension.
- Two-dimensional arrays are the most common type, resembling tables.
- Access elements using multiple indices.
- Use nested loops to iterate through the elements.
- Dynamic multidimensional arrays can be created using pointers.
Check your understanding
- What is the syntax for declaring a two-dimensional array in C++?
- How do you access an element in a two-dimensional array?
- What is a common mistake when working with multidimensional arrays?
- Explain how to free memory allocated for a dynamic multidimensional array.