Table of contents | |
What is an Array? | |
What is a Two-Dimensional Array? | |
Declaring a Two-Dimensional Array | |
Iterating Over a Two-Dimensional Array | |
Conclusion |
An array is a collection of elements of the same data type, stored in contiguous memory locations. It provides a convenient way to store and access multiple values using a single variable.
A two-dimensional array is an array in which elements are arranged in a two-dimensional grid or matrix. It can be visualized as a table with rows and columns. This data structure is useful when working with grids, matrices, tables, and other similar structures.
To declare a two-dimensional array in C++, you specify the data type of the elements and the dimensions of the array. The syntax for declaring a two-dimensional array is as follows:
dataType arrayName[rowSize][columnSize];
Here, 'dataType' is the type of elements in the array, 'arrayName' is the name of the array, 'rowSize' specifies the number of rows, and 'columnSize' specifies the number of columns.
Example: Declaring a Two-Dimensional Array
Let's declare a two-dimensional integer array named 'matrix' with 3 rows and 4 columns:
int matrix[3][4];
To access an element in a two-dimensional array, you use the row and column indices. The indices start from 0 and go up to the size of the array minus 1.
Example: Accessing Elements in a Two-Dimensional Array
int matrix[3][4];
// Assigning values to elements
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[0][2] = 3;
matrix[0][3] = 4;
matrix[1][0] = 5;
matrix[1][1] = 6;
matrix[1][2] = 7;
matrix[1][3] = 8;
matrix[2][0] = 9;
matrix[2][1] = 10;
matrix[2][2] = 11;
matrix[2][3] = 12;
// Accessing elements
cout << matrix[0][0]; // Output: 1
cout << matrix[1][2]; // Output: 7
You can initialize a two-dimensional array at the time of declaration using an initializer list. The number of elements in the initializer list should match the size of the array.
Example: Initializing a Two-Dimensional Array
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
You can use nested loops to iterate over the elements of a two-dimensional array. The outer loop controls the rows, and the inner loop controls the columns.
Example: Iterating Over a Two-Dimensional Array
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Printing elements
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
1 2 3
4 5 6
7 8 9
Problem 1: Write a C++ program to find the sum of all elements in a given 3x3 matrix.
#include <iostream>
using namespace std;
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int sum = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum += matrix[i][j];
}
}
cout << "Sum of elements: " << sum << endl;
return 0;
}
Sum of elements: 45
Problem 2: Write a C++ program to find the maximum element in a given 4x4 matrix.
#include <iostream>
using namespace std;
int main() {
int matrix[4][4] = {
{3, 7, 2, 9},
{4, 1, 8, 5},
{6, 2, 4, 7},
{9, 3, 5, 1}
};
int maxElement = matrix[0][0];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (matrix[i][j] > maxElement) {
maxElement = matrix[i][j];
}
}
}
cout << "Maximum element: " << maxElement << endl;
return 0;
}
Maximum element: 9
In this article, we covered the basics of two-dimensional arrays in C++. We learned how to declare, access, initialize, and iterate over two-dimensional arrays. Two-dimensional arrays are a powerful tool for handling structured data in programming. By understanding and practicing with them, you'll be well-equipped to work with grids, matrices, and tables in your C++ programs.
70 videos|45 docs|15 tests
|
|
Explore Courses for EmSAT Achieve exam
|