Do you want to learn how to print 2D arrays in C++? 🤔 Then keep reading this article, 👇, it’s just for you.
An array is a grouping of identically typed items stored in adjacent memory regions and separately referred to using an index to a special identifier. Each element of an array in C++ has a corresponding number. The value is referred to as an array index.
Using the indexes, we can get an array of items. The maximum number of elements that may be stored in an array in C++ is n. Contiguous memory addresses make up all arrays. The first element has the lowest address, while the last element has the highest address. In order to declare an array in C++, the programmer must first provide the type of elements and the number of items that the array needs, i.e., the array name and the array size.
Syntax
int x[2][4] = {{0,1,2,3}, {4,5,6,7}};
In this article, we’ll discuss 2D arrays in C++, how to declare and initialize an array, and how to print a 2D array in C++. So without further ado, let’s dive deep into the topic and see some real examples!
Table of Contents
What Are 2D Arrays in C++?
In C++, a two-dimensional array is one that has multiple rows and multiple columns. Each element in a 2D array is referenced by two indexes. These arrays include elements that are matrices. The first index displays the matrix’s row, while the second index displays the matrix’s column. The following is how a programmer declares an array in C++ by specifying the type of elements and the necessary number of elements.
The syntax of the 2-Dimensional array is as follows;
Datatype Array_name[Rows] [columns];
How to Initialize a 2D Array in C++?
Along with the definition, a 2-dimensional array may also be initialized. Elements of each row are encased between curly braces and separated by commas for the initialization of a two-dimensional array. Curly braces are used to enclose each row.
Code
int A[2][3] = {{33, 53, 12}, {13, 24, 15}, {35, 73, 63}, {73, 12, 90}};
How to Access The Elements of a 2D Array in C++?
The number of rows and columns in a matrix must be provided by the programmer in order to store data in a C++ two-dimensional array. The user must provide the precise number of rows and columns in order to access each unique position in a matrix where values are stored.
Code
#include<iostream> using namespace std; int main() { int arr[3][2] = {{2,4}, {2,9}, {5,1}}; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { cout << "Element at index:[" << i << "][" << j << "]: "; cout << arr[i][j]<<endl; } } return 0; }
Output
Element at index:[0][0]: 2 Element at index:[0][1]: 4 Element at index:[1][0]: 2 Element at index:[1][1]: 9 Element at index:[2][0]: 5 Element at index:[2][1]: 1
In the above example, we have initialized a 2-dimensional array with three rows and two columns. which is already populated with elements. We can access every element through the indexes. The output shows that on the 0 indexes of the row and column, the element is 2.
How to Store Value in 2D Arrays in C++?
The formula below may be used to determine the maximum number of items a 2D array can keep: There may be n1*n2 entries in the array arr[n1][n2]. The array in the following example has sizes 2 and 2. These measurements are referred to as subscripts. As a result, the first subscript value in this array is 2, and the second subscript value is 2. This means that the array arr[2][2] may contain 2*2 = 4 entries.
Two for-loops, one of which is nested, are being used to store the user-inputted items. The inner for loop extends from 0 to the, and the outside loop extends from 0 to the (first subscript -1). (second subscript -1) In this manner, the user would enter the components in the following order: arr[0][0], arr[0][1], arr[0][2], and so on.
Code
#include<stdio.h> int main(){ int arr[2][2]; int i, j; for(i=0; i<2; i++) { for(j=0;j<2;j++) { printf("Enter value for index[%d][%d]:", i, j); scanf("%d", &arr[i][j]); } } return 0; }
Output
Enter value for index[0][0]:1 Enter value for index[0][1]:2 Enter value for index[1][0]:3 Enter value for index[1][1]:4
How to Print 2D Array in C++?
Following are the four different methods to print a 2D array in C++
- Using for-loop.
- Using While-loop
- Using A For-Loop to Take 2D Array Elements As User Input
- Using A While-Loop to Take 2D Array Elements As User Input
Method 1: Using For-loop
In order to traverse all the rows and columns of the provided 2D matrix and print the elements, the typical approach to printing a 2D array using a for loop necessitates the use of two for loops. Let’s see the example below;
Code
#include<iostream> using namespace std; int main() { int arr[4][2] = {{1, 4}, {3, 5}, {2, 6}, {8, 7}}; int i, j; cout<<"Using a for-loop to print the two-dimensional array::n"; for(i=0; i<4; i++) { for(j=0; j<2; j++) cout<<arr[i][j]<<" "; cout<<endl; } cout<<endl; return 0; }
Output
Using a for-loop to print the two-dimensional array: 1 4 3 5 2 6 8 7
In the above example, we are printing a 2D array using a loop. index 0 to row length-1 will be iterated over in the outer loop. It moves through the 2D array row-by-row, so the first row is printed before printing the second row.
Method 2: Using while-loop
Another example is printing a 2D array by using the while loop in C++.
Let’s see an example:
Code
#include<iostream> using namespace std; int main() { int arr[4][2] = {{1, 4}, {3, 5}, {2, 6}, {8, 7}}; int i = 0, j = 0; cout<<"Using a while-loop to print the two-dimensional array::n"; while (i < 3) { j = 0; while (j < 4) { cout << arr[i][j] << " "; j++; } i++; cout << endl; } }
Output
Using a while-loop to print the two-dimensional array: 1 4 3 5 3 5 2 6
In the above example, we can see that it is a 4*2 array 2 represents rows, and 4 represents columns. We must set the j value for each row to 0 since j prints columns, and then j prints to the 4th column as we have set the condition that j should be printed if the value of the matrix is less than 4, where i represents the number of rows.
Method 3: Using A For-Loop to Take 2D Array Elements As User Input
The user can also enter values for 2D arrays in C++. However, since we cannot accomplish this at the time of declaration, we must once again make use of the different loops covered in the earlier parts. Let’s see an example of getting elements for the matrix using the for-loop.
Code
#include <iostream> using namespace std; int main() { int arr[2][2]; cout << "Enter Elements for matrix: "; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { cin >> arr[i][j]; } } cout << endl; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { cout << arr[i][j] << " "; } cout << endl; } return 0; }
Output
Enter Elements for matrix: 0 8 9 2 0 8 9 2
Method 4: Using a While-Loop to Take 2D Array Elements as User Input
The user can also enter values for two-dimensional arrays in C++ using a while-loop. Let’s see an example of getting elements for the matrix using the while-loop.
Code
#include <iostream> using namespace std; int main() { int i = 0; int j = 0; int arr[2][3]; cout << "Enter Elements for matrix:: n"; while (i < 2) { j = 0; while (j < 2) { cin >> arr[i][j]; j++; } i++; } cout << endl; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { cout << arr[i][j] << " "; } cout << endl; } return 0; }
Output
Enter Elements for matrix:: 1 2 3 4 1 2 3 4
Conclusion
To summarize the article, we have discussed what a 2D array is in C++, A 2D array is composed of other arrays. In C++, the 2D array may be seen as a matrix, and the contents of the two-dimensional array may be seen as a table (row-column manner). We often utilize two variables, i and j, to refer to a specific value or to access each place in the 2D array.
Furthermore, in this article, we have talked about how to initialize, store values, access elements in 2D arrays, and print 2D arrays in C++.
We have also talked about the different kinds of loops. We need to utilize two loops in the nested forms in C++ to print a two-dimensional or 2D array. For, while, or any mix of these loops is acceptable. But compared to the while loop, the for loop’s syntax is simpler to understand.
Let’s have a quick recap of the topics discussed in this article.
- How to Print 2D arrays in C++?
- What are 2D arrays in C++?
- How to initialize a 2D array in C++?
- How do you access elements in the 2D array in C++?
- How to store value in 2D arrays in C++?
If you’ve found this article helpful, remember to share and comment below. 👇: Which solutions have helped you solve the problem?