so I'm starting to write a program that multiplies two square matrices using dynamic 2D arrays. I'm just learning how dynamic arrays work, so I'm testing to make sure everything is storing properly.
When I run my code, it outputs the two matrices on a single line each, rather than like a matrix with rows and columns. How do I fix this?
#include <iomanip>
#include <iostream>
#include <array>
using namespace std;
int main()
{
int **C, n, m; //pointer, rows, columns for matrix 1;
int **D, p, q; //pointer, rows, columns for matrix 2;
cout << "Enter the dimensions of your matrices: ";
cin >> n >> m;
p = n;
q = m;
cout << endl;
C = new int *[n];
D = new int *[p];
for (int x=0 ; x < n; x++)
{
C[x] = new int [m];
}
for (int x=0 ; x < p; x++)
{
D[x] = new int [q];
}
cout << "Enter the values of your first matrix: ";
for (int I=0 ; I < n; I++ )
{
for (int K=0 ; K < m; K++)
cin >> C[I][K];
}
cout << "Enter the values of your second matrix: ";
for (int L=0 ; L < p; L++ )
{
for (int Z=0 ; Z < q; Z++)
cin >> D[L][Z];
}
for (int I=0 ; I < n; I++ )
{
for (int K=0 ; K < m; K++)
cout << setw(4)<< C[I][K];
}
cout << endl;
for (int L=0 ; L < p; L++ )
{
for (int Z=0 ; Z < q; Z++)
cout << setw(4)<< D[L][Z];
}
cout << endl;
}
Just add one more statement
cout << endl;
in your for loops like
for (int I=0 ; I < n; I++ )
{
for (int K=0 ; K < m; K++)
cout << setw(4)<< C[I][K];
cout << endl;
}
cout << endl;
for (int L=0 ; L < p; L++ )
{
for (int Z=0 ; Z < q; Z++)
cout << setw(4)<< D[L][Z];
cout << endl;
}
cout << endl;