Matrix Multiplication

Hi, I'm supposed to make a program that can multiplicate 2 matrices with fixed dimensions, the first one is [7][5], the second one is [5][4]. I was able to find the result, but the output is only 1 column (every result appears under the one before it, instead of it being in 4 columns).
Here is my code until now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;


int main()
{
	int a[7][5];
	int b[5][4];

cout<<"Enter first matrix"<<endl;
for (int i=0; i<7; i++)
for (int j=0; j<5; j++)
{
	cin>>a[i][j];
}
cout<<"Enter second matrix"<<endl;
for (int i=0; i<5; i++)
for (int j=0; j<4; j++)
{
	cin>>b[i][j];
}

for (int q=0; q<4; q++)
{ 
	for (int i=0; i<7; i++)
{ int sum=0;
	for (int j=0; j<5; j++)
{
{ sum=sum+a[i][j]*b[j][q];
}

}
	cout<<sum<<endl;
}
	
}
} 

You cannot tell what the problem is because you haven't any consistent indentation. Fix your indentation and it will make more sense.

The problem is on line 33. It is doing two things - printing a number and it is printing a newline. The newline should be on line 35.

You will also have to add some space between the numbers you print out. I recommend #including <iomanip> and using the setw() manipulator.

Finally, I would print something useful for the person who runs the program on line 22, like cout<<"The product is"<<endl;

Hope this helps.
Ok I solved it myself but thanks anyway.
What I did is I switched the loops in line 17 with the one in 23 and put an endl in line 35, something like that, a bit of trial and error.
Topic archived. No new replies allowed.