You are using a version without Ads of this website. Please, consider donating:

Matrix Multiplication using Class (Without friend)

Hi guys. I am working on a matrix multiplication code. Its working but not correctly :).. I would really appreciate if you guys could help me with this..
Thanks.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485`` ``````#include #include #include using namespace std; const int ROW=5; const int COL=5; class matrix { int a[ROW][COL]; int row; int col; public: matrix () : row(0), col(0) {} void read(int r, int c); void display(); matrix multiply (matrix m1, matrix m2); }; void matrix :: read(int r, int c) { row=r; col=c; for(int i=1; i<=r; i++) for(int j=1; j<=c; j++) { cout<<"A["<>a[i][j]; } } void matrix :: display() { for(int i=1; i<=row; i++) { for (int j=1; j<= col; j++) cout<>r1>>c1; int r2,c2; cout<<"Enter the order of second matrix : "; cin>>r2>>c2; if(c1!=r2) cout<<"Matrix multiplication not Possible\n"; else { cout<<"Enter elements of first matrix:\n"; m1.read(r1,c1); cout<<"Enter elements of second matrix:\n"; m2.read(r2,c2); m4 = m3.multiply (m1,m2); cout<<"Resultant matrix is :\n"; m4.display(); } getche(); return 0; } ``````
I don't have time to look in detail at your code (since I'm at work), but at a glance. Your loops are starting at 1, which would cause an error since arrays start at 0. Also you have them set to <= which could cause to go out of bound. usually should go `x < a.size`
Thank you so much. It solved my problem.
God Bless you.
Topic archived. No new replies allowed.