### Matrix multiplication

Now i have a problem to calculate multiplication of matrix that's not the problem i wrote it
but the real problem is that i have to write it in function and i dont know how
could anybody tell me how or give me hint or what should i study to solve it
That's my code

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` `````` #include using namespace std; int main() { int l,m,z,n; int matrixA[10][10]; int matrixB[10][10]; int matrixC[10][10]; cout<<"enter the dimension of the first matrix"<>l>>m; cout<<"enter the dimension of the second matrix"<>z>>n; if(m!=z||z!=m){ cout<<"error in the multiblication enter new dimensions"<>l>>m; cout<<"enter the dimension of the second matrix"<>z>>n; } else{ cout<<"enter the first matrix"<>matrixA[i][j]; } } cout<<"enter the second matrix"<>matrixB[i][j]; } } for(int i=0;i

i need to make this code using functions
Last edited on
closed account (D80DSL3A)
The tutorial at this site on arrays may tell you what you need. Passing multi-dimensional arrays to functions is covered near the end:
http://www.cplusplus.com/doc/tutorial/arrays/
thank u i will read it
This is my new code
the problem is in my matrix multiplication
and is this function as what my teacher want ???

#include <iostream>
using namespace std;
#include <cstdlib>

void fn(int **arr) {

}
void fn2 (int **arr2){

}
void fn3 (int **arr3){

}
int main() {

int r, c ,r2 , c2 , r3 , c3 , m , l;

cout << "Enter size of row 1 : " ;
cin >> r ;
cout << "Enter size of column 1 : " ;
cin >> c ;
cout <<"Enter matrix 1 please " ;

int **arr = new int*[r];
for (int i = 0; i < r; i++)
arr[i] = new int[c];

for (int i=0 ; i<r ; i++){
for (int j=0 ; j<c ; j++)
cin >> arr[i][j] ;
}
for (int i=0 ; i<r ; i++){
for (int j=0 ; j<c ; j++)
cout << arr[i][j]<<" " ;
cout << endl ;
}
cout << "Enter size of row 2 : " ;
cin >> r2 ;
cout << "Enter size of column2 : " ;
cin >> c2 ;
cout << "Enter matrix 2 please " ;
int **arr2 = new int*[r2];
for (int i = 0; i < r2 ; i++)
arr2[i] = new int[c2];

for (int i=0 ; i<r2 ; i++){
for (int j=0 ; j<c2 ; j++)
cin >> arr2[i][j] ;
}
for (int i=0 ; i<r2 ; i++){
for (int j=0 ; j<c2 ; j++)
cout << arr2[i][j] <<" " ;
cout << endl ;
}
int **arr3 = new int*[r3];
for (int i = 0; i < r2 ; i++)
arr3[i] = new int[c];
if (c == r2 ){

for (int i=0 ; i<r2 ; i++){
for (int j=0 ; j<c ; j++){
arr3[i][j]=0;
for(int k=0;k<c;k++)
arr3[i][j]=arr3[i][j]+(arr[i][k] * arr2[k][j]);
}
}
for(int i=0;i<r3;i++){
for(int j=0;j<r3;j++){
cout<<arr3[i][j]<<" ";
}
cout<<endl;
}
}
else
cout << "Error"<< endl ;

fn(arr);
fn2(arr2);
fn3(arr3);
return 0;
}

> the problem is in my matrix multiplication
¿what is the problem?
it doesnt give me the right answer !!!
closed account (D80DSL3A)
You've made some errors in the number of rows and columns of the matrices.
You can't independently specify the rows and columns for all 3 matrices.

Assuming: arr3 = arr*arr2

You can specify r,c and have arr[r][c]. You can specify the number of columns for arr2 (say c2), but not the number of rows - they must equal the number of columns of arr. ie arr2[c][c2]. arr3 will then have dimensions arr3[r][c2].

If you make a drawing of multiplying the matrices you should be able to see it and to straighten out the row and column values throughout your code.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596`` `````` #include using namespace std; #include void fn(int **arr) { } void fn2 (int **arr2){ } void fn3 (int **arr3){ } int main() { int r, c ,r2 , c2 , r3 , c3 , m , l; cout << "Enter size of row 1 : " ; cin >> r ; cout << "Enter size of column 1 : " ; cin >> c ; cout <<"Enter matrix 1 please " ; int **arr = new int*[r]; for (int i = 0; i < r; i++) arr[i] = new int[c]; for (int i=0 ; i> arr[i][j] ; } for (int i=0 ; i> r2 ; cout << "Enter size of column2 : " ; cin >> c2 ; cout << "Enter matrix 2 please " ; int **arr2 = new int*[r2]; for (int i = 0; i < r2 ; i++) arr2[i] = new int[c2]; for (int i=0 ; i> arr2[i][j] ; } for (int i=0 ; i

That is my final code is it right ???
please i need one tell me what i need to do as my teacher asks me to
Students MUST use functions

This is the whole question

Write a program that asks the user for two matrices: matrix A with dimensions LxM, and matrix B with dimensions MxN. The program then prints the matrix C of their multiplication with dimensions LxN. You MUST represent every matrix with a two-dimensional array. The program should ask the user to:
a) Enter the dimensions of the first matrix
b) Enter the dimensions of the second matrix
c) Enter the first matrix.
d) Enter the second matrix
After step b), the program should validate if the entered dimensions are valid for matrix multiplication. If invalid dimensions are entered, the program should print an error message, and ask the user again for new valid dimensions. After step d), the program prints the result matrix

sorry for being boring for somebody here !!!
any help ???
Topic archived. No new replies allowed.