In C++, a two-dimensional array is a collection of variables of the same type arranged in a grid
consisting of rows and columns. It can be thought of as an array of arrays.
Declaring and Initializing Two-Dimensional Arrays
To declare a two-dimensional array, specify the type of its elements, followed by the array name
and its dimensions in square brackets. Here's an example:
You can also initialize a two-dimensional array at the time of declaration:
-
int myArray[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
Accessing Array Elements
Array elements are accessed using their row and column indices. For example,
myArray[0][0]
refers to the element in the first row and first column,
myArray[1][2]
to the element in the second row and third column, and so on.
-
#include
using namespace std;
int main() {
int myArray[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
cout << "The element at row 1, column 2 is: " << myArray[1][2] << endl;
cout << "The element at row 0, column 0 is: " << myArray[0][0] << endl;
return 0;
}
Looping Through a Two-Dimensional Array
To process all elements of a two-dimensional array, you can use nested loops. The outer loop
iterates through the rows, while the inner loop iterates through the columns:
-
#include
using namespace std;
int main() {
int myArray[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 4; ++j) {
cout << "Element at row " << i << ", column " << j << " is: " << myArray[i][j] << endl;
}
}
return 0;
}
Modifying Array Elements
Array elements can be modified by assigning new values to them using their row and column
indices:
-
#include
using namespace std;
int main() {
int myArray[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
myArray[1][2] = 20; // Changing the element at row 1, column 2 to 20
cout << "The new value of the element at row 1, column 2 is: " << myArray[1][2] << endl;
return 0;
}
Practice Questions
- What is the difference between a one-dimensional and a two-dimensional
array?
- What are the advantages of using a two-dimensional array?
- Can you store different data types in different elements of a
two-dimensional array in C++?
- How do you initialize a two-dimensional array to zero given the array
declaration
int arr[3][3]
- What happens if you access an element outside the bounds of a
two-dimensional array?
- Can you change the number of rows or columns in a two-dimensional array
after it has been declared in C++? Why or why not?
- How do you calculate the total memory required by a two-dimensional array in
C++?
- What are some common applications of two-dimensional arrays?
-
Matrix Addition: Write a C++ program to input two 3x3 matrices from the user
and perform matrix addition. Display the resulting matrix.
- Declare two 3x3 matrices and a third matrix to store the result.
- Input the elements of both matrices from the user.
- Add the corresponding elements of both matrices and store them in
the result matrix.
- Display the resulting matrix.
-
Transpose of a Matrix: Write a C++ program to find the transpose of a 2x3
matrix entered by the user.
- Declare a 2x3 matrix and a 3x2 matrix for storing the transpose.
- Input the elements of the 2x3 matrix from the user.
- Calculate the transpose of the matrix (switch rows and columns).
- Display the transposed matrix.
-
Matrix Multiplication: Write a C++ program to multiply two matrices. The
first matrix is of size 2x3, and the second matrix is of size 3x2. Display
the resulting 2x2 matrix after multiplication.
- Declare two matrices of size 2x3 and 3x2, respectively.
- Input the elements of both matrices from the user.
- Multiply the matrices and store the result in a 2x2 matrix.
- Display the resulting matrix.
-
Sum of Rows and Columns: Write a C++ program to calculate the sum of all the
elements in each row and each column of a 3x3 matrix entered by the user.
- Declare a 3x3 matrix.
- Input the elements of the matrix.
- Calculate and display the sum of each row and each column.
-
Finding Maximum and Minimum Elements: Write a C++ program to find the
maximum and minimum elements in a 2x3 matrix entered by the user.
- Declare a 2x3 matrix.
- Input the elements of the matrix.
- Traverse the matrix to find the maximum and minimum elements.
- Display the maximum and minimum values.
-
Diagonal Sum of a Square Matrix: Write a C++ program to calculate the sum of
the diagonal elements of a 3x3 square matrix entered by the user.
- Declare a 3x3 matrix.
- Input the elements of the matrix.
- Calculate the sum of the diagonal elements (from top-left to
bottom-right).
- Display the sum.
-
Matrix Subtraction: Write a C++ program to input two 2x2 matrices from the
user and perform matrix subtraction. Display the resulting matrix.
- Declare two 2x2 matrices and a third matrix to store the result.
- Input the elements of both matrices from the user.
- Subtract the elements of the second matrix from the first and store
the result.
- Display the resulting matrix.
-
Check if a Matrix is Symmetric: Write a C++ program to check if a 3x3 matrix
is symmetric. A matrix is symmetric if it is equal to its transpose.
- Declare a 3x3 matrix.
- Input the elements of the matrix.
- Calculate the transpose of the matrix.
- Check if the original matrix is equal to its transpose.
- Display whether the matrix is symmetric or not.
-
Count Positive and Negative Elements: Write a C++ program to count the
number of positive and negative elements in a 3x3 matrix entered by the
user.
- Declare a 3x3 matrix.
- Input the elements of the matrix.
- Use loops to count how many elements are positive and how many are
negative.
- Display the counts.
-
Histogram plotter (Rainfall figures): Write a program that would plot
the rainfall chart.
- Initialize the array with the following sample rainfall figures:
- User is required to enter the day and the program will plot the
chart. Chart contains the number of * based on the rainfall
- Example: if day entered is 3 then the chart is **********
-
Sum elements column by columns: Write a function that returns the sum of all the elements in a specified column in a matrix. Function:
double sumColumn(const double m[][SIZE], int rowSize, int columnIndex);
- Write a test program that reads a 3-by-4 matrix
- Display the sum of each column.
- Here is a sample run: