Ask your own question, for FREE!
Computer Science 14 Online
OpenStudy (anonymous):

Write a function called multiply that takes two square matrices of the same size as arguments and returns their element-wise product. The type of the matrices should be integers. Your code needs to handle matrices with multiple rows and columns, and calculate their product as: p[i][j]= a[i][j] * b[i][j] where p is the product to return, a and b are the input argument matrices, and i, j are index variables for the matrices. Print the return matrix in main

OpenStudy (anonymous):

#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 ; arr3 = new int * [r2]; for(int i=0;i<c;i++) { arr2[i] = new int [c2]; arr3[i] = new int [c2]; } if (c != r2) cout << "We cannot solve multiplication " ; else { for (int i = 0; i < r ; i++){ for (int j=0 ; j<c ; j++){ arr3[i][j]=0; for (int k=0 ; k<2 ; k++){ arr3[i][j]=0; arr3[i][j]=arr3[i][j]+(arr[i][k] * arr2[k][j]); } } } cout<<"your matrix is"<<endl; for(int i=0;i<c;i++){ for(int j=0;j<r2;j++) cout<<arr3[i][j]<<" "; cout<<endl; } } fn(arr); fn2(arr2); fn3(arr3); return 0; }

OpenStudy (anonymous):

BR my answer if helped... You can read this article that I read to understand the algorithm... http://code.wikia.com/wiki/Matrix_multiplication http://msdn.microsoft.com/en-us/library/hh873134.aspx

OpenStudy (anonymous):

Thanks! and wait this isnt java is it?

OpenStudy (anonymous):

oops... It is c++

OpenStudy (anonymous):

you didnt mention java did u?

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!