## A Two dimensional array in c++ with solved example

Welcome friend in this article we will learn about a two dimensional array in c++. you can imagine a two dimensional array in c++ as a matrix you study in Mathematics. or you can understand as a grid of row and columns or you can visualize a two dimensional array as a classroom full of students in which student sit in benches these benches is a row, and there are multiple benches in the classroom they form a structure like a two dimensional grid in which student sit in rows and columns.**Note**: If you don't know about an array I recommend you go through our array tutorial. because a one-dimensional array is essential to learn about two dimensional array.

**What is the two dimensional array in c++?**

A two dimensional array can say as an array of an array in which multiple arrays stores together of the same type. The relational database of MySql in the form of a table and tables are two dimensional array.

Here is the pictorial representation of a two dimensional array in c++ or any other programming language two dimensional array are the same.

So you can see that the

**two dimensional array looks like a grid**which consists of rows and columns in some time we will learn about practical uses and how to make two dimensional array coding question. so be with us for some time ðŸ˜€.

**What are some practical use cases of a two dimensional array in c++?**

- Use in a relational database to stores user data in the form of tables.
- To the solved problem of graphs (A non-linear data structure)
- To solve the problem of traveling salesmen in minimum coast
- we can make a map of the country/state with city distance and can store in two dimensional array.

**What are the different ways to initialize a two dimensional array in c++?**

- int arr[3][4];
- int arr[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
- int arr[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};

So now you can easily visualize how we store elements in two dimensional array {1,2,3,4} in first row {5,6,7,8} in second row {9,10,11,12} in third row.

**Write A program to take input of two-dimensional array and print it on screen?**

#include<iostream>

using namespace std;

int main()

{

int row,column;

cout<<"Enter value of row and column ";

cin>>row>>column;

int arr[row][column];

cout<<"Enter value for first matrix "<<endl;

for(int i=0;i<row;i++)

{

for(int j=0;j<column;j++)

{

cin>>arr[i][j];

}

}

cout<<endl<<"Hear is the output"<<endl<<endl;

for(int i=0;i<row;i++)

{

for(int j=0;j<column;j++)

{

cout<<arr[i][j]<<" ";

}

cout<<endl;

}

}

**The output of the code**

**Take a Matrix of 3*3 from the user and print its determinant value?**

**#include<iostream>**

using namespace std;

int main()

{

int row,column;

int arr[3][3];

cout<<"Enter value for first matrix "<<endl;

for(int i=0;i<3;i++)

{

for(int j=0;j<3;j++)

{

cin>>arr[i][j];

}

}

int val1 = arr[0][0]*((arr[1][1]*arr[2][2])-(arr[1][2]*arr[2][1]));

int val2 = arr[0][1]*((arr[1][0]*arr[2][2])-(arr[2][0]*arr[1][2]));

int val3 = arr[0][2]*((arr[1][0]*arr[2][1])-(arr[2][0]*arr[1][1]));

int valOfDeterminant = val1-val2+val3;

cout<<"Hear is the value of above determinant "<<valOfDeterminant;

}

**The output of the code**

**Write A program to take 2 matrices from the user and print the sum of the matrix?**

#include<iostream>

using namespace std;

int main()

{

int row,column;

cout<<"Enter value of row and column ";

cin>>row>>column;

int arr1[row][column];

int arr2[row][column];

cout<<"Enter value for first matrix "<<endl;

for(int i=0;i<row;i++)

{

for(int j=0;j<column;j++)

{

cin>>arr1[i][j];

}

}

cout<<"Enter value for second matrix "<<endl;

for(int i=0;i<row;i++)

{

for(int j=0;j<column;j++)

{

cin>>arr2[i][j];

}

}

cout<<endl<<"Hear is the sum of matrix "<<endl<<endl;

for(int i=0;i<row;i++)

{

for(int j=0;j<column;j++)

{

cout<<arr1[i][j]+arr2[i][j]<<" ";

}

cout<<endl;

}

}

**The output of the code**

**Write A program to take 2 matrices from the user and print the multiplication of the matrix?**

**#include<iostream>**

using namespace std;

int main()

{

int row;

cout<<"Enter value of row: ";

cin>>row;

int arr1[row][row];

int arr2[row][row];

int res[row][row];

cout<<"Enter value for first matrix "<<endl;

for(int i=0;i<row;i++)

{

for(int j=0;j<row;j++)

{

cin>>arr1[i][j];

}

}

cout<<"Enter value for second matrix "<<endl;

for(int i=0;i<row;i++)

{

for(int j=0;j<row;j++)

{

cin>>arr2[i][j];

}

}

for (int i=0; i<row; i++)

{

for (int j=0; j<row; j++)

{

res[i][j] = 0;

for (int k=0; k<row; k++)

{

res[i][j] += arr1[i][k]*arr2[k][j];

}

}

}

cout<<endl<<"Here is matrix multiplication "<<endl<<endl;

for(int i=0;i<row;i++)

{

for(int j=0;j<row;j++)

{

cout<<res[i][j]<<" ";

}

cout<<endl;

}

}

**The output of the code**

**Hope you like our article on a two dimensional array in c++. then please share this article with your coder friends and do follow us on our social or you have any problem or want to give suggestions then, please go on the main page and you find a contact form where you can mention your problem/suggestion in the message box.**

Thank you so much for reading this article and giving your valuable time to us we hope this article two dimensional array in c++ help you to clear your concept and problem regarding this topic.

if you want to write a post on our website then please check this article on How to do a guest post.

## 0 Comments