Apr 1, 2009 at 2:46pm UTC
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..
Apr 1, 2009 at 2:52pm UTC
Please use [ code][/code] tags when posting code
if (col3=4)
should be if (col3==4)
Apr 1, 2009 at 3:00pm UTC
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 Apr 1, 2009 at 3:03pm UTC
Apr 1, 2009 at 3:11pm UTC
can u help me edit a bit so as to keep track of row3
Apr 1, 2009 at 3:22pm UTC
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 Apr 1, 2009 at 3:22pm UTC
Apr 1, 2009 at 3:24pm UTC
i dun understand wad u mean ...
i tot the int Table3[5][5] alrdy shows the limit
Apr 1, 2009 at 3:40pm UTC
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.