In: Computer Science
C++ How do you make a 2d array with a user input. For example, the row and columns of the 2d array will be the same so the program can create a square matrix.
Code
#include<iostream>
using namespace std;
int main()
{
int **array;
int size;
cout<<"Enter the size of the mateix: ";
cin>>size;
array=new int*[size];
for(int i=0;i<size;i++)
{
array[i]=new int[size];
}
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
cout<<"Enter the ("<<i<<", "<<j<<")
the value in matrix: ";
cin>>array[i][j];
}
}
cout<<"\n\nMatrix is : "<<endl;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
cout<<array[i][j]<<" ";
}
cout<<endl;
}
}
output
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.