How to Create Array of Arrays in C++ Last Updated : 12 May, 2024 Comments Improve Suggest changes Like Article Like Report Arrays are basic C++ data structures that allow users to store the same data type in memory sequentially. To manage more complicated data structures, you may sometimes need to build an array of arrays, often called a 2D array or a matrix. In this article, we will learn how to create an array of arrays in C++. Create an Array of Arrays in C++To create an array of arrays also known as 2D arrays, you can follow the same approach you follow to create a normal one-dimensional array with some minor changes. Like in normal one-dimensional arrays, you define only the number of columns for the array here you will have to define an additional dimension row along with the number of columns. Here is the syntax to create an array of arrays in C++: Syntax data_type Arr [rows][columns];where: data_type: denotes the type of data you want to store in the array.Arr: is the name of the array of arrays.rows: denotes the number of rows in your array of arrays.columns: denotes the number of columns in your array of arrays.C++ Program to Create Array of Arrays The following program demonstrates how to create array of arrays in C++ C++ // C++ Program to demonstrate how to create array of arrays #include <iostream> using namespace std; int main() { // Declare and initialize a 2D array int arr[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // Print the elements of the array for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { cout << arr[i][j] << " "; } cout << endl; } return 0; } Output1 2 3 4 5 6 7 8 9 Time Complexity:O(N*M) where N denotes the number of rows and M denotes the number of columns.Auxiliary Space:O(N*M) Create Array of Arrays for std::array Container in C++In C++, we have a wrapper container class for arrays named std::array. We can also create arrays of this type of array container f you are using the below syntax: Syntaxstd::array<std::array<dataType, COLS>, ROWS> matrix = {{ {x,y,z} .... }};where: data_type: denotes the type of data you want to store in the array of arrays.Matrix: is the name of the array of arrays.ROWS: denotes the number of rows in your array of arrays.COLS: denotes the number of columns in your array of arrays.C++ Program to Create std::Array of Arrays The following program demonstrates how we can create array of arrays using std::arrays in C++: C++ // C++ Program to demonstrate how we can create array of // arrays using std::arrays #include <array> #include <iostream> using namespace std; // Declare the dimensions for the arrays of arrays const int ROWS = 3; const int COLS = 3; int main() { // Initialize an array of arrays array<array<int, COLS>, ROWS> matrix = { { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } } }; // Print all elements for (int i = 0; i < ROWS; ++i) { for (int j = 0; j < COLS; ++j) { cout << matrix[i][j] << " "; } cout << endl; } return 0; } Output1 2 3 4 5 6 7 8 9 Time Complexity:O(N*M) where N denotes the number of rows and M denotes the number of columns.Auxiliary Space:O(N*M) Comment More infoAdvertise with us Next Article How to Create Array of Arrays in C++ R ravinp1w6 Follow Improve Article Tags : C++ Programs C++ Arrays C++ Array Programs CPP Examples +1 More Practice Tags : CPPArrays Similar Reads C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to 5 min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous 4 min read Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th 5 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Top 50 Array Coding Problems for Interviews Array is one of the most widely used data structure and is frequently asked in coding interviews to the problem solving skills. The following list of 50 array coding problems covers a range of difficulty levels, from easy to hard, to help candidates prepare for interviews.Easy ProblemsSecond Largest 2 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We 9 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read Maximum Subarray Sum - Kadane's Algorithm Given an array arr[], the task is to find the subarray that has the maximum sum and return its sum.Examples:Input: arr[] = {2, 3, -8, 7, -1, 2, 3}Output: 11Explanation: The subarray {7, -1, 2, 3} has the largest sum 11.Input: arr[] = {-2, -4}Output: -2Explanation: The subarray {-2} has the largest s 9 min read Like