Two-dimensional arrays can be useful for storing data that has multiple dimensions or that is organized in a table or grid. For example, you might use a two-dimensional array to store the grades for a group of students, with each row representing a different student and each column representing a different assignment or exam.
Declaration of Multi-Dimensional Array in C++
To declare an array of arrays in C++, you need to use the following syntax:
In this syntax, the data_type refers to the data type that is stored in the array. The array_name is the name of the array. The num_rows are the number of rows in the array. And num_cols are the number of columns in the array.
Different Ways to Declare the Multi-Dimensional Arrays in C++
We use an integer-type array so that we can easily understand the working of arrays in C++ and how we put the data in array rows and columns. The program that follows declares an array of integer arrays. It creates a 3×3 array of name grid which is an integer type:
As we learned in the previous syntax, 3×3 means that there are 3 rows and 3 columns in the array. For example: we want to assign the value which is 25 to the element in the second row and third column of the grid array.
This sets the value of the element in the second row and third column which has a zero-based index of 1 and 2, respectively, to 25. You can also initialize an array of arrays when you declare it. For example, the following code declares and initializes a 2×2 grid of characters:
{'O', 'X'}};
This creates a 2×2 array of name “board” which contain the characters with the values “X”, “O”, “O”, and “X”. To iterate over an array of arrays, we shall utilize the loops.
Example 1: Demonstrating a Multidimensional Array of Integer Type
Here is the first simple example of multidimensional arrays in C++. In this example, we first include the basic header files which are useful in getting the input and printing the output on the user console window. Then, we start the main() function so that we can easily demonstrate the working of multidimensional arrays.
First, we initialize an int-type array of the name “grid” which contains 3 rows and 3 columns in it. Then, we store some data in the grid. To get the data from the grid array, we use a for-loop that works iteratively to print all the data of the grid on the output screen.
#include<cmath>
using namespace std;
int main()
{
int grid[3][3] = {{1, 4, 7},
{2, 5, 8},
{3, 6, 9}};
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
cout << grid[i][j] << " ";
}
cout << endl;
} }
This code prints the following output:
Example 2: Demonstrating the Seating Arrangement of a Movie Theater though Multidimensional Arrays in C++
Here is another example of an array of arrays in C++. Suppose we want to store the seating arrangement of a movie theater where each seat can be either empty or occupied. We can use a two-dimensional array to store this data, with each row representing a different row of seats and each column representing a different seat in that row. We declare a two-dimensional array called “seating” with 5 rows and 10 columns.
A separate row of seats is represented by each row, and a different seat within that row is represented by each column. We initialize the array with some sample data where 0 represents an empty seat and 1 represents an occupied seat. To access a specific seat, we can use the row and column index of that seat.
For example, to check if the seat in the third row and the fifth column are empty, we use the if-else statement. For example: Seating[2] represents the third row of the array which contains the seating arrangement for the third row of seats. Seating[2][4] represents the fifth element of the third row which is the seat in the third row and fifth column.
Then, we utilize the for-loop that repeatedly iterates over the array’s items and carry out any necessary operations on each seat.
#include<cmath>
using namespace std;
int main()
{
int seating[5][10] = {{0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 0, 0, 0, 0, 1, 1, 0},
{1, 1, 0, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 0, 0, 0, 0, 0, 0, 1, 1}};
int seatStatus = seating[2][4];
if (seatStatus == 0) {
cout << "Seat is empty" << endl;
} else {
cout << "Seat is occupied" << endl;
}
int occupiedSeats = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 10; j++) {
if (seating[i][j] == 1) {
occupiedSeats++;
}
}
}
cout << "There are " << occupiedSeats << " occupied seats." << endl;
}
In this illustration, the inside loop and the outside loop alternately iterate over each row in the array. The if (seating[i][j] == 1) statement inside the inner loop checks if the current seat is occupied. If so, increments the occupied seat counter. At the end of the loops, the program outputs the total number of occupied seats. Here is the output that is generated by compiling the previous program:
Conclusion
We learned that an array of arrays is a data structure that contains multiple arrays within a single array, allowing for the representation of data in a matrix-like structure. We also learned how to create more complex data structures such as tables or cubes of data. While they can be powerful, working with arrays of arrays can also be more complex and require a good understanding of syntax and methods. That is why we implemented multiple examples so that we can easily understand the concepts.