I need to make two matrices and then multiply them and output. I can input fine but somewhere in the multiplication process it makes the program crash. I don't get an error message and I can't seem to figure out why it doesn't work. Any help is greatly appreciated. Thanks!
#include <iostream>
usingnamespace std;
int main() {
int n=0,n2=0,n3=0,**x, **y,**z;
cout<<"How many rows do you want in your first matrix? ";
cin>>n;
cout<<"How many columns do you want in your first matrix? ";
cin>>n2;
cout<<"How many columns do you want in your second matrix? ";
cin>>n3;
x = newint*[n];
y = newint*[n2];
z = newint*[n];
if(x == NULL) {
cout<< "Could not allocate memory. Exiting program.";
return 1;
}
if(y == NULL) {
cout<< "Could not allocate memory. Exiting program.";
return 1;
}
//User input for x
cout<<"Array 1"<<endl<<endl;
for (int i=0 ; i < n; i++)
x[i] = newint [n2];
for (int i=0 ; i < n; i++ )
{
for (int j=0 ; j < n2; j++)
{
cout<<"Please input a number for row "<<i<<" and column "<<j<<endl;
cin>>x[i][j];
}
}
cout<<endl<<endl;
//User input for y
cout<<"Array 2"<<endl<<endl;
for (int i=0 ; i < n2; i++)
y[i] = newint [n3];
for (int i=0 ; i < n2; i++ )
{
for (int j=0 ; j < n3; j++)
{
cout<<"Please input a number for row "<<i<<" and column "<<j<<endl;
cin>>y[i][j];
}
}
cout<<endl<<endl;
//Multiplying x*y
for (int row = 0; row < n; row++)
{
for (int col = 0; col < n3; col++)
{
for(int k = 0; k <n; k++)
{
z[row][col] += x[row][k] * y[k][col];
}
cout<<z[row][col] << " ";
}
cout<< "\n";
}
return 0;
}