Multiple of two matrix...

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";

}
getch();
return 0;
}

Please read a book on linear algebra how matrices are multiplied. As for me I do not understand the following code

1
2
3
4
5
6
7
8
if(j==3)
 {
 z[i] [j--] +=r;
 }
 // else
 // {
 z[i] [j] +=r;
 //} 
Last edited on
Hi vlad, thank you for your guidance,i understand how to multiple matrices and i solve it by this code:
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 3to4matrix_multi_4to3matrix.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int i,j,p,r=0,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
	
	for(i=0;i<=2;i++)
	{
		for(j=0;j<=2;j++)
		{
			for(p=0;p<=2;p++)
			{
				for(c=0;c<=3;c++)
				{
					r +=x [j] [c] * y [c] [p];
				}
				z [i] [p]=r;
				r=0;
			}
		}
	}
	//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";

	}
	_getch();
	return 0;
}

are you have a new Adea to solve it by difference way?
Just a minor thing:

This makes a little more sense:

1
2
3
4
5
6
7
8
9
10
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

const int ROWMAX = 4;
const int 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.

HTH
Topic archived. No new replies allowed.