hello everyone i want to calculate multiple of two 3 * 4 and 4 * 3 matrix,
when calculate multiple of two matrix it's go to an infinite loop
please help me ....
code:
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int i,j,r,c,x[3] [4],y [4] [3],z[3] [3];
//get the 3 * 4 matrix numbers from the user
cout << "Please enter 3 * 4 matrix numbers :\n";
for(i=0;i<=2;i++)
{
for(j=0;j<=3;j++)
{
cin >> x [i] [j];
}
}
cout << "Please enter 4 * 3 matrix numbers :\n";
//get the 4 * 3 matrix numbers from the user
for(i=0;i<=3;i++)
{
for(j=0;j<=2;j++)
{
cin >> y [i] [j];
}
}
//set 0 value to matrix z
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
z[i] [j] = 0;
}
}
//calculate multiple of two matrix
//these for loop after run going to infinite loop
for(i=0;i<=2;i++)
{
for(j=0;j<=3;j++)
{
r=x[i] [j] * y[j] [i];
if(j==3)
{
z[i] [j--] +=r;
}
// else
// {
z[i] [j] +=r;
//}
}
}
//output the result
cout << "Multiple of two matrix is : \n";
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
cout << z [i] [j];
}
cout<<"\n";
cout << "Please enter 4 * 3 matrix numbers :\n";
//get the 4 * 3 matrix numbers from the user
for(i=0;i < 4;i++)
{
for(j=0;j < 3;j++)
{
cin >> y [i] [j];
}
}
Even better - have const values rather than magic numbers throughout your code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
constint ROWMAX = 4;
constint COLMAX = 3;
cout << "Please enter 4 * 3 matrix numbers :\n";
//get the 4 * 3 matrix numbers from the user
for(i=0;i < ROWMAX;i++)
{
for(j=0;j < COLMAX;j++)
{
cin >> y [i] [j];
}
}
I recommend you get used to this idiom for for loops.
I try to avoid the <= operator in a for loop end condition, because it easily leads to going past the end of an array.
This is what is happening in lines 46 to 52. It doesn't go past the the end of the array, but it is probably not doing what you want. The loops run this many times respectively:
i,j,p 3 times, c 4 times. Is that what you want? Your comment says multiply 2 by 2 matrix.