0

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;
  
  }
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
jmizza
  • 19
  • 2
  • *How do I fix this?* -- You need to state when to start a new line of output. It isn't automatic. Also, your program has memory leaks -- a tool like valgrind will shout at you about it. – PaulMcKenzie Sep 15 '22 at 14:26
  • 2D dynamic array? You want `std::vector`. Forget that C style arrays exist in the language and stay far away from manual memory management. Don't write code like it is still the 1990s. Your program is leaking like a sieve. – Jesper Juhl Sep 15 '22 at 14:26
  • `#include ` -- Your code does not use `std::array` anywhere, so why was this header included? – PaulMcKenzie Sep 15 '22 at 14:28
  • Also; [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Sep 15 '22 at 14:30
  • @JesperJuhl : I completely agree with you, I am writing my code in the way that my professor is teaching it and expects us to write it. Unfortunately, the university I am attending's CS program is incredibly outdated and they have the same professors teaching the same thing they have for decades now, and I am in the process of transferring due to this, as it is not how they said it would be at all. If you have any suggestions on how I can improve/learn a more industry standard (or just more functional in general) coding style on my own that would help a ton. – jmizza Sep 16 '22 at 14:09
  • @PaulMcKenzie : See my comment addressing Jesper. If you have any suggestions as well, it would be deeply appreciated. Thank you. – jmizza Sep 16 '22 at 14:10
  • @PaulMcKenzie regarding the inclusion in the header, I was testing something that utilizes that header in a previous version of my code and forgot to remove it. Thank you for catching that haha. – jmizza Sep 16 '22 at 14:11
  • @jmizza Read [some good up-to-date books about modern C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and maybe join/contribute to some open source projects that use modern C++. – Jesper Juhl Sep 16 '22 at 14:27

1 Answers1

0

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;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335