C++ Program to Print a 2D Array Last Updated : 19 Jul, 2022 Comments Improve Suggest changes Like Article Like Report Here we will see how to print a 2D array using a C++ program. Below are the examples: Input: {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}Output: 1 2 3 4 5 6 7 8 9 Input: {{11, 12, 13}, {14, 15, 16}}Output: 11 12 13 14 15 16 There are 2 ways to print a 2D array in C++: Using for loop.Using range-based for loop. Let's start discussing each of these methods in detail. 1. Using for loop The general method to print a 2D array using for loop requires two for loops to traverse all the rows and columns of the given 2D matrix and print the elements. The outer loop will loop from index 0 to row_length-1.It traverses the 2D array row-wise, therefore the first row is printed then it goes to print the second row.An Example array Below is the C++ program to print a 2D array using for loop: C++ // C++ program to print // 2D array using for loop #include <iostream> using namespace std; // Driver code int main() { const int i = 3, j = 3; // Declaring array int arr[i][j] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; for(int a = 0; a < 3; a++) { for(int b = 0; b < 3; b++) { cout << arr[a][b] << " "; } cout << endl; } return 0; } Output1 2 3 4 5 6 7 8 9 Time Complexity: O(n*m) where n and m are dimensions of the array.Auxiliary Space: O(1) 2. Using a Range-Based for loop Instead of using for loop, this method will use a range-based for loop. Below is the C++ program to print a 2D array using a range-based for loop: C++ // C++ program to print 2D // array using range-based for loop #include <iostream> using namespace std; // Driver code int main() { const int i = 3, j = 3; int matrix[i][j] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; for (auto &row : matrix) { for (auto &column : row) { cout << column << " "; } cout << endl; } } Output1 2 3 4 5 6 7 8 9 Time Complexity: O(n*m) where n and m are dimensions of the array.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article C++ Program to Print a 2D Array B bskapardi2002 Follow Improve Article Tags : C++ Programs C++ Practice Tags : CPP Similar Reads C++ Program to Print the Pattern 'G" In this article, we will learn how to print the pattern G using stars and white spaces. Given a number n, we will write a program to print the pattern G over n lines or rows.Examples: Input : 7 Output : *** * * * *** * * * * *** Input : 9 Output : ***** * * * * *** * * * * * * ***** In this program, 2 min read Array C/C++ Programs C Program to find sum of elements in a given arrayC program to find largest element in an arrayC program to multiply two matricesC/C++ Program for Given an array A[] and a number x, check for pair in A[] with sum as xC/C++ Program for Majority ElementC/C++ Program for Find the Number Occurring Odd N 6 min read C++ Program To Print Character Pattern Here we will build a C++ Program To Print Character patterns using 2 Approaches i.e. Using for loopUsing while loop Printing 1 character pattern in C++ using different approaches. 1. Using for loop Input: rows = 5 Output: A B B C C C D D D D E E E E E Approach 1: Assign any character to one variable 5 min read C++ Program For Row Wise Sorting in 2D Array Given a 2D array, sort each row of this array and print the result.Examples: Input: 77 11 22 3 11 89 1 12 32 11 56 7 11 22 44 33 Output: 3 11 22 77 1 11 12 89 7 11 32 56 11 22 33 44 Input: 8 6 4 5 3 5 2 1 9 7 4 2 7 8 9 5 Output: 4 5 6 8 1 2 3 5 2 4 7 9 5 7 8 9 Method 1 (Using Bubble Sort): Start ite 3 min read C++ Program To Print Number Pattern Here, we will see a C++ program to print the 3 different number patterns. There are 3 number patterns covered using for loop and while loop with their respective explanation.3 Different Number Patterns: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 1 12 123 1234 12345Pattern 1:In 6 min read How to Print an Array in C++? In C++, an array is a fixed-size linear data structure that stores a collection of elements of the same type in contiguous memory locations. In this article, we will learn how to print an array in C++. For Example, Input: array = {10, 20, 30, 40, 50}Output: Array Elements: 10 20 30 40 50Printing Arr 2 min read How to Loop Over an Array in C++? In C++, an array is a data structure that stores elements of similar type in contiguous memory locations. We can access the elements of an array using array indexing. In this article, we will learn how to loop over an array in C++. Example: Input: int arr[] = [1, 2, 3, 4, 5] Output: Array Elements: 2 min read C++ Program to Access Elements of an Array Using Pointer Prerequisites: Pointers in C++Array in C++ A Pointer is a variable that stores the memory location or address of an object or variable. In other words, pointers reference a memory location, and obtaining the value stored at that memory location is known as dereferencing the pointer. An Array is the 2 min read C++ Program To Print Triangle Pattern Here we will see how to print triangle patterns using a C++ program. There are 4 patterns discussed here: Right Triangle.Inverted Right Triangle.Equilateral Triangle.Inverted Equilateral Triangle.Inverted Mirrored Right Triangle. Let's start discussing each of these in detail. 1. Right Triangle Belo 6 min read How to Deallocate a 2D Array in C++? In C++, an array of arrays is called a 2D array, or two-dimensional array. Deallocating a 2D array means freeing the allocated memory to prevent any memory leaks, especially when working with dynamically allocated memory. In this article, we will learn how to deallocate a 2D array in C++. Deallocati 3 min read Like