2D array and multi dimensional array
C++ multidimensional arrays
In this article, you learn about the multidimensional array in cpp.you will learn accessing, initialization, and transverse the multidimensional arrays.
The concept behind the array of arrays is just like that representation of array data in the tabular form, similar to the matrix form representation.
Here the 2D array can store 15 elements. The array has the 3 rows and 5 columns. In the example array name is Ar.
Every index of the array has the name just like the data in the variable has the name.
These indexes used for accessing the data store in the array. In the above diagram, there is an array and every block of the array has a name like an array[row][col].
How to initialize the 2D array in cpp?
Initialization of 2D array
int arr[2][3]={1,2,3,4,5,6};
This is not a good way to initialize the 2D array.following is the best way to initialize the multi-dimensional arrays.
int arr[2][3]={{1,2,3} , {4,5,6}};
This array has two rows and three columns which means that each row has the three elements.
How to initialize the 3D array?
Initialization of 3D array
int arr[2][3][2]={1,2,3,4,5,6,7,8,9,11,12};
Again this is not a good practice to initialize the 3D array in this way.The better way to initialize the three-dimensional array is
int arr[2][3][2]={
{{1,2},{3,4},{5,6}} , {{7,8},{9,10},{11,12}}
};
Example 1: Two-dimensional array
#include <iostream>
using namespace std;
int main() {
int ar[3][4];
//input the value from the user in the 2d array
cout<<" enter the number ";
cout<<endl;
for(int i=0;i<2;i++)
{
for(int j=0;j<4;j++)
{
cin>>ar[i][j];
}
}
//display the number store in the array
for(int i=0;i<2;i++)
{
for(int j=0;j<4;j++)
{
cout<<"ar["<<i<<"]"<<"["<<j<<"] = "<<ar[i][j];
cout<<endl;
}
}
return 0;
}
output:
enter the number
1,2,3,4,5,6,7,8
ar[0][0]=1;
ar[0][1]=2;
ar[0][2]=3;
ar[0][3]=4;
ar[1][0]=5;
ar[1][1]=6;
ar[1][2]=7;
ar[1][3]=8;
In the above example, declare a 2D array of name ar with two rows and three columns,for loop use to input the value from the user and then again use for loop to display the elements store in the 2d array.
In the output ,show the value assign to the indexes of the array ar[0][0]=1,ar[0][1]=2,ar[0][2]=3,ar[0][3]=4,ar[1][0]=5,ar[1][1]=6,ar[1][2]=7,ar[1][3]=8;
Example 2: 3D array
Find the sum of data store in 3d array
#include <iostream>
using namespace std;
int main() {
int ar[2][4][2]={{{1,2},{3,4},{5,6},{7,8}},{{9,10},{11,12},{13,14},{15,16}}};
int sum=0;
//display the number store in the array
for(int i=0;i<2;i++)
{
for(int j=0;j<4;j++)
{
for(int z=0;z<2;z++)
{
sum+=ar[i][j][z];
}
}
}
//display the sum of array elements
cout<<"sum is ="<<sum<<endl; //display the sum variable
return 0;
}
output:
sum=136
In the above example, declare and initialize the 3D array as a name ar of the int data type. Use three nested for loop for accessing the values of the array and add it to the sum variable. Access every element of the 3D array and add each element in the sum variable. At the last, display the sum variable.
In this blog I get a great post please also visit my blog I post a great content https://lyricsmintss.com/crown-prince-jazzy-b-bohemia/
ReplyDelete