In the previous articles on arrays in C++ (parts 1, 2, and 3), Mytour introduced you to the declaration and initialization of arrays in C++. In the upcoming article on arrays in C++ (part 4) below, Mytour will further introduce you to 2D arrays (or two-dimensional arrays) in C++.
Article Contents:
1. 2D Arrays in C++
1.1. Definition of 2D Arrays in C++
1.2. Initialization of 2D Arrays in C++
2. Why Use 2D Arrays in C++?
1. Exploring 2D Arrays in C++
In C++, a 2D array, also known as a matrix, consists of rows and columns. Let's dive into how to declare and initialize a 2D array in C++.
1.1 Defining 2D Arrays in C++
Similar to a 1D array, we define a 2D array in C++ using the following syntax:
int a[2][4];
Here, 'a' is a 2D array of type int, consisting of 2 rows and 4 columns.
1.2 Initializing 2D Arrays in C++
Similar to a 1D array, we can assign values to a 2D array in two ways in C++.
In the first approach, simply assign a value to each element of the array. If no value is assigned to any element, the default value is 0.
Suppose we have declared a 2D array a[2][2]. Then, to assign values to it, we need to assign a value to each of its elements.
int a[2][2];
a[0][0]=1;
a[0][1]=2;
a[1][0]=3;
a[1][1]=4;
In the second approach, declare and assign values simultaneously as in a 1D array.
int a[2][3] = { 1, 2, 3, 4, 5, 6 };
Here, the value of a[0][0] is 1, a[0][1] is 2, a[0][2] is 3, a[1][0] is 4, a[1][1] is 5, and a[1][2] is 6.
We can write the code above as follows:
int a[2][3] = {
{1, 2, 3},
{4, 5, 6 }
};
In a 1D array, when assigning values to an array at the declaration time, we do not need to provide the size, but in a 2D array, we need to provide at least the second dimension size.
Consider the following cases to initialize an array:
int a[2][2] = { 1, 2, 3, 4 }; /* valid */
int a[][2] = { 1, 2, 3, 4 }; /* valid */
int a[2][] = { 1, 2, 3, 4 }; /* invalid */
int a[][] = { 1, 2, 3, 4 }; /* invalid */
2. Why Use 2D Arrays in C++?
Suppose there are 3 students, each student studies 2 subjects (subject 1 and subject 2), the requirement is to display the grades of these 3 students for 2 subjects.
#include
using namespace std;
int main(){
float marks[3][2];
int i,j;
for( i=0; i<3; i++)
{
/* input marks from user */
cout < 'Enter marks for student '< (i+1) < ':'<<endl;
for( j=0; j<2; j++)
{
cout < 'Subject '< (j+1) < ':'<<endl;
cin >> marks[i][j];
}
}
/* printing marks of students */
for( i=0; i<3; i++)
{
cout < 'Marks of student '< (i+1) < ':'<<endl;
for( j=0; j<2; j++)
{
cout < 'Subject '< (j+1) < ':' < '=' < marks[i][j] < ' ' < ''<<endl;
}
}
return 0;
}
Output will be like:
Enter marks for student 1
Subject 1
78
Subject 2
94
Enter marks for student 2
Subject 1
87
Subject 2
91
Enter marks for student 3
Subject 1
62
Subject 2
56
Marks of student 1
Subject 1 : 78
Subject 2 : 94
Marks of student 2
Subject 1 : 87
Subject 2 : 91
Marks of student 3
Subject 1 : 62
Subject 2 : 56
In the example above, we first define an array, consisting of 3 rows and 2 columns as float marks[3][2];
Here, the elements of the array contain the marks of 2 subjects for 3 students as follows:
In this example, we retrieve the value of each element of the array by using a for loop inside another for loop.
In the first iteration of the outer for loop, the value of i is 0. With i being 0, when the inner loop iterates for the first time, the value of j is 0, so marks[i][j] is marks[0][0]. By writing cin >> marks[i][j]; to retrieve the value of marks[0][0].
Then, the inner loop iterates again and the value of j is 1, marks[i][j] is marks[0][1], and its value is taken from the user. Next, the outer loop iterates for the second time with i being 1, and the whole process continues.
After assigning values to the array elements, we will print the values of the array elements using a for loop inside another for loop.
Here is another example of a 2D array in C++:
Assuming there are 2 factories and each of them produces 4 different types of products, including some of type 1, some of type 2, ... . Our task is to calculate the total production of each factory, meaning the total of each type of product produced by each factory.
#include
Here, products[0][i] corresponds to the quantity of products of factory 1 and type i, where we take values from 0 to 3 using a for loop and products[1][i] corresponds to the quantity of products of factory 2 of type i. For example, - products[0][2] corresponds to the third type of product of factory 1 and products[1][2] corresponds to the third type of product of factory 2. sum1 is the total of all products of factory 1. Similarly for the total of all products of factory 2.
Initially sum1 is 0. In the first iteration, products[0][i] is products[0][0], corresponding to the quantity of the first product of factory 1. So, sum1 += products[0][i] will be sum1 += products[0][0]. Thus, sum1 will be 2. Similarly in the second iteration, products[0][i] will be products[0][1], corresponding to the second type of product of factory 1. So, sum1 will be 2 + 5 which is 7, and so on.
In the previous article about arrays in C++ (part 4) here, Mytour just introduced you to what 2D arrays (meaning 2-dimensional arrays) in C++ are. In the upcoming articles, Mytour will continue to introduce you to strings in C - a topic that many programmers are interested in, with many issues related to strings in C being detailed in the article.
