Problem in Matrix Multiplication(Using freind function)

Hi folks,

I'm a beginner in c++. The other day was trying to multiply 2 matrices using friend function. The program did run ,but the result showed some garbage values. plz help..

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

#include<iostream.h>

class matrix
{	
	int a[10][10],b[10][10],d[10][10],r,c,l,m;
	public:
		void read (void);
		void display (void);
		friend void multiply1 (matrix m1);
};

void matrix :: read()
{
	int i,j;
	cout<<"enter r and c of 1st mat"<<"\n";
	cin>>r>>c;
	cout<<"enter elements\n";
	for(i=0;i<r;i++)
		for(j=0;j<c;j++)
				cin>>a[i][j];
	
}
void matrix :: display()
{
	int i,j;
	cout<<"the elements are --"<<"\n";
	cout<<"The elements of 1st\n";
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
				cout<<a[i][j]<<" ";
		}
		cout<<"\n";
	}
	cout<<r;    //The value being printed here is right.
}
void  multiply1(matrix m1)
{
	int i,j,k;
	cout<<"\n"<<m1.r; //The value being printed here is always 0 .. :-(
	cout<<"\n Enter the dimension (n*m) for the second matrix:";
	cin>>m1.l>>m1.m;
	
	cout<<"\n Enter "<< m1.l*m1.m <<" element:";
	for(i=0;i<m1.l;i++)
	{
		 for(j=0;j<m1.m;j++)
 		{
 			 cin>>m1.b[i][j];
 		}
	}
	
	cout<<"The elements of 2nd\n";
	for(i=0;i<m1.l;i++)
	{
		for(j=0;j<m1.m;j++)
		{
				cout<<m1.b[i][j]<<" ";
		}
		cout<<"\n";
	}

// Multiplication
if(m1.r==m1.m)
{
	for(i=0;i<m1.l;i++)
	{
 		for(j=0;j<m1.m;j++)
 		{
  			m1.d[i][j]=0;
  			for(k=0;k<m1.m;k++)
  				m1.d[i][j]=m1.a[i][k]*m1.b[k][j]+m1.d[i][j];
		}
	}
cout<<"\n Resultant Matrix:";
	for(i=0;i<m1.l;i++)
	{
 		cout<<"\n";
		for(j=0;j<m1.m;j++)
			cout<<m1.d[i][j]<<"\t";
 	}
}
}

main()
{
	matrix obj,m1,m2;
	obj.read();
	obj.display();
	multiply1(m1);
	
}



The rows of the first matrix ('r' here) is not being passed into the method 'multiply'. The value of r when i print is showing 0 always. Any idea???
When are you setting m1.r to anything other than 0? You aren't even explicitly initializing it to 0. Should you be passing obj to multiply1?
In general multiply the row with col then adding the results. if your left matrix has n cols then your right should have n rows.
Here is a simplified version which I wrote some time ago =)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/** Multiplication
Matrix is a class which basically acts like 2 dimensional vector.
**/
Matrix matrix_multiplication_function(Matrix & matrix_1, Matrix & matrix_2){

        //Check that rows are of the correct dimension
	if(matrix_1.cols() != matrix_2.rows()){
		throw std::invalid_argument ("Invalid Dimension");
	}

        //Create a new matrix with the correct dimension
	Matrix result(matrix_1.rows(), matrix_2.cols());
 
	for(int r = 0; r < result.rows(); ++r){       
		for(int c = 0; c < result.cols(); ++c){       
			for(int i = 0; i < matrix_1.cols; ++i){
				result[r][c] += matrix_1[r][i] * matrix_2[i][c];
			}     
		}       
	}
        //return a copy of result
	return result;
}
Topic archived. No new replies allowed.