How to Create a 2D Array of a Specific Size and Value in C++? Last Updated : 12 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a 2D array is an array of arrays that stores data in the form of a grid with rows and columns. In this article, we will learn how to create a 2D array of a specific size and fill it with some value in C++. Example Input: rows = 3; columns = 3; value = 1; Output: 2D Array: { {1, 1, 1}, {1, 1, 1}, {1, 1, 1} }Create a 2D Array of a Specific Size and Value in C++We can create a 2D array of the given size by specifying the size of the rows as the first dimension size and the column size as second dimension size as shown below: Syntax to Create 2D ArrayThe syntax for creating and initializing a 2D array in C++ is as follows: datatype array_name[row_size][column_size];datatype: Specifies the type of elements in the array.array_name: The name of 2D array.row_size: The number of rows in 2D array.column_size: The number of columns in each row of 2D array.We can then use nested loops to fill the values in a 2D array. C++ Program to Create a 2D Array of a Specific Size and ValueThe below example demonstrates how we can create a two dimensional array and fill it with values in C++. C++ // C++ Program to illustrate how to create a 2D array of a // specific size and fill it with values #include <iostream> using namespace std; int main() { // Specify the size of the 2D array const int rows = 3; const int cols = 3; // Initialize a 2D array int array[rows][cols]; // Fill the 2D array with values for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { array[i][j] = 1; } } // Print the 2D array for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { cout << array[i][j] << " "; } cout << endl; } return 0; } Output1 1 1 1 1 1 1 1 1 Time Complexity: O(n*m) , here n and m are rows and columns respectivelyAuxilliary Space: O(n*m) Comment More infoAdvertise with us Next Article How to Create a 2D Array of a Specific Size and Value in C++? M maha123 Follow Improve Article Tags : C++ Programs C++ cpp-array CPP Examples Practice Tags : CPP Similar Reads How to Create a Set of Arrays in C++? In C++, the set container represents a collection of unique, sorted elements, and an array is a collection of items stored at contiguous memory locations. In this article, we will learn about how to create a set of arrays in C++. Set of Arrays in C++A set of arrays refers to a collection of arrays w 2 min read How to Create an Array of Structs in C++? In C++, a struct is a user-defined data type that allows us to combine data of different types and an array of structs is an array in which each element is of the struct type. In this article, we will learn how to create an array of structs in C++. Creating an Array of Structs in C++To create an arr 2 min read How to Create a Stack of Arrays in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a stack of an array in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; ar 2 min read How to Calculate the Size of a Static Array in C++? In C++, static arrays are the type of arrays whose size is fixed, and memory for them is allocated during the compile time. In this article, we will learn how to calculate the size of a static array in C++. Example: Input:myArray = {1, 2, 3, 4, 5}Output:The size of the array is 5.Size of a Static Ar 2 min read How to Create a Vector of Arrays in C++? In C++, an array is a collection of elements of a single type while vectors are dynamic arrays as they can change their size during the insertion and deletion of elements. In this article, we will learn how to create a vector of arrays in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; arr3 2 min read How to Find the Range of Values in a 2D Array in C++? In C++, 2D arrays are also known as a matrix, and the range of numbers in a 2D array means the maximum and the minimum value in which the numbers lie in the given 2D array. In this article, we will learn how to find the range of numbers in a 2D array in C++. For Example, Input: my2DArray= {{80, 90, 2 min read How to Create Array of Arrays in C++ 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 arra 3 min read How to Resize an Array of Strings in C++? In C++, the array of strings is useful for storing many strings in the same container. Sometimes, we need to change the size of this array. In this article, we will look at how to resize the array of strings in C++. Resize String Array in C++There is no way to directly resize the previously allocate 2 min read How to Convert an Array to a Vector in C++? In this article, we will learn how to convert the given array into vector in C++.The simplest method to convert an array to vector is by using range constructor of vector. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 3, 6, 2, 9}; 3 min read How to Create a Map of Arrays in C++? In C++, the std::map is a container that stores elements in a key-value pair, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a map of arrays in C++. Example: Input: arr1 = {1, 2, 3};arr2 = {4, 5, 6};arr3 = {7, 8, 9}; 2 min read Like