programe to perform Matrix Multiplication for 3x5 and 5x3 matrix.

can someone tell me wads wrong with my program..


#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
int Table1 [5][3];

for (int row1 = 0; row1<5;row1++)
{
for (int col1 =0; col1<3;col1++)
{
cout<<"Input digit for 5x3 matrix [";
cout<<row1;
cout<<"][";
cout<<col1;
cout<<"]:";
cin>>Table1[row1][col1];
}
}

cout<<endl;

int Table2 [3][5];

for (int row2 = 0; row2<3;row2++)
{
for (int col2 =0; col2<5;col2++)
{
cout<<"Input digit for 3x5 matrix [";
cout<<row2;
cout<<"][";
cout<<col2;
cout<<"]:";
cin>>Table2[row2][col2];
}
}


int Table3 [5][5];
int row3=0;
int col3=0;


for (int col2 = 0; col2<5;col2++)
{
for (int row1 = 0; row1<5;row1++)
{
Table3[row3][col3]=
(Table1[row1][0]*Table2[0][col2])
+(Table1[row1][1]*Table2[1][col2])
+(Table1[row1][2]*Table2[2][col2]);

row3++;
}
col3++;
}


for (row3=0;row3<5;row3++)
{

for (int col3=0;col3<5;col3++)
{ cout<<Table3[row3][col3];
cout<<" ";

if (col3=4)
{
cout<<endl;
}
}
}








cout<<endl;
system("pause");

return 0;
}

i cant output my answers..
Please use [code][/code] tags when posting code

if (col3=4) should be if (col3==4)
lets say if i cancel the if (col3==4)....

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
int Table1 [5][3];

for (int row1 = 0; row1<5;row1++)
{
for (int col1 =0; col1<3;col1++)
{
cout<<"Input digit for 5x3 matrix [";
cout<<row1;
cout<<"][";
cout<<col1;
cout<<"]:";
cin>>Table1[row1][col1];
}
}

cout<<endl;

int Table2 [3][5];

for (int row2 = 0; row2<3;row2++)
{
for (int col2 =0; col2<5;col2++)
{
cout<<"Input digit for 3x5 matrix [";
cout<<row2;
cout<<"][";
cout<<col2;
cout<<"]:";
cin>>Table2[row2][col2];
}
}


int Table3 [5][5];
int row3=0;
int col3=0;


for (int col2 = 0; col2<5;col2++)
{
for (int row1 = 0; row1<5;row1++)
{
Table3[row3][col3]=
(Table1[row1][0]*Table2[0][col2])
+(Table1[row1][1]*Table2[1][col2])
+(Table1[row1][2]*Table2[2][col2]);

row3++;
}
col3++;
}


for (row3=0;row3<5;row3++)
{

for (int col3=0;col3<5;col3++)
{ cout<<Table3[row3][col3];
cout<<" ";
}
}








cout<<endl;
system("pause");

return 0;
}

the ans it gives me is wrong
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int Table3 [5][5];
int row3=0;
int col3=0;


for (int col2 = 0; col2<5;col2++)
{
for (int row1 = 0; row1<5;row1++)
{
Table3[row3][col3]=
(Table1[row1][0]*Table2[0][col2])
+(Table1[row1][1]*Table2[1][col2])
+(Table1[row1][2]*Table2[2][col2]);

row3++;
}
col3++;
}


Step through these loops and keep track of row3. You keep incrementing it well beyond the size of Table3.
can u help me edit a bit so as to keep track of row3
After you increment through an entire row, you need to return back to beginning so you can go through the next
one. Look at your loops and determine where you need to reset row3. It might help to visualize the matrix
(or even sketch it out on a piece of paper) and think about how you are stepping through it.
Last edited on
i dun understand wad u mean ...
i tot the int Table3[5][5] alrdy shows the limit
It shows the size of the array, but you can still try to write to Table[5][5] and cause all sorts of havoc.

Look at it like this:
The first time you enter the for loops, you set Table3[0][0].
Then [1][0].
Then [2][0].
...
Eventually you try to write to Table3[5][1], which is wrong. You want to write to Table[0][1] instead.

Look at it carefully. You can get it.
Look at two for loops
1
2
3
4
for(int i=0;i<5;i++)
 (  for(int j=0;j<5;j++)
   x=j+1;
   cout<<x<<",";}


This will print:

1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,
1,2,3,4,5,

When j gets to 4 i is incremented but the j loop does the work.
This is what jdd is trying to get you to see..I think.
hope this helps
Topic archived. No new replies allowed.