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..

``````#include<iostream.h>

class matrix
{
int a,b,d,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???

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..

``````#include<iostream.h>

class matrix
{
int a,b,d,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???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.