2D Arrays
2D Arrays
2D Arrays
Topics to be covered:
Multidimensional Array
2D Array
2D Vector
In C++, we can create “ array of an array” which are known as a multidimensional array. It stores
homogeneous data in a tabular form. Data in multidimensional arrays are stored in row-major order i.e.
elements are filled in the current row before moving to the next row.
datatype array_name[size1][size2].....[sizeN];
datatype array_name[rows][columns];
where rows imply the number of rows needed for the 2D array and column implies the number of
columns needed.
For example:
int arr[4][5];
Here, arr is a two-dimensional array. It can hold a maximum of 20 elements. Let us understand how.
We can think of this array as a table with 4 rows and each row has 5 columns as shown below.
arr[0][0] arr[0][1] arr[0][2] arr[0][3] arr[0][4]
In this array you can store the values as required. Suppose, in the above array you want to store 10 at
every index, you can do so using the following code:
#include<iostream>
using namespace std;
void main(){
int arr[4][5];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
arr[i][j] = 10;
}
}
}
Method 1
int arr[2][3]={11,22,33,44,55,66};
Method 2
int arr[2][3]={{11,22,33},{44,55,66}};
Now that we are equipped with all the relevant information about 2-D arrays, let us move a step ahead
to understand 3-D arrays. Although these are rarely used in the common problems that we solve but it
is always better to have a slight idea of how the dimensions of an array are scaled up.
double arr[3][2][5];
This array ‘arr’ can be considered as 3 arrays of 2D which has 2 rows and 5 columns.
We can find out the total number of elements in the array simply by multiplying its dimensions:
325=30
For all implementations, we will have to take inputs from user and work on that data.
A 2D array is an array that contains elements in the form of rows and columns. It means we require
both rows and columns to populate a two-dimensional array. Matrix is the best example of a 2D array.
We have already learnt to declare 2D arrays and the way to access each element.
#include<iostream>
using namespace std;
int main(){
int arr[2][3];
int i, j;
cout<<"\n2D Array Input By user:\n";
for(i=0;i<2;i++){
for(j=0;j<3;j++){
cout<<"\ns["<<i<<"]["<<j<<"]= ";
cin>>arr[i][j];
}
}
cout<<"\nThe 2-D Array entered by user is:\n";
for(i=0;i<2;i++){
for(j=0;j<3;j++){
cout<<"\t"<<arr[i][j];
}
cout<<endl;
}
}
OUTPUT :
s[0][0] = 2
s[0][1] = 4
s[0][2] = 3
s[1][0] = 6
s[1][1] = 7
s[1][2] = 9
2 4 3
6 7 9
Explanation : In the above code firstly we are taking the input of the different elements of the array
and after taking input we are printing the elements of the array.
Introduction to 2D Vectors
A 2D vector is a vector of vectors. Like 2D arrays, values can be declared and assigned to a 2D vector.
vector<vector<datatype>> vector_name
int main(){
vector<int>v1(3 , 5); //{5,5,5}
vector<int>v2(5 , 6); //{6,6,6,6,6}
vector<int>v3(1 , 7); //{7}
vector<vector<int>>vec;
vec.push_back(v1);
vec.push_back(v2);
vec.push_back(v3);
for(int i=0;i<vec.size();i++){
for(int j=0;j<vec[i].size();j++){
cout<<vec[i][j]<<" ";
}
cout<<endl;
}
}
Output :
5 5 5
6 6 6 6 6
For example ,
vector<vector<int>>vec(3 , vector<int>(4));