+ 1
Hii everyone, please tell me can I make this code more optimise? If yes, then also tell how.
2 Answers
+ 1
Thanks bro, I will
0
You can reduce one loop. The loop for making all elements of answer matrix as 0 can be eliminated . Instead it can included in the multiplication loop itself.
#include <iostream>
using namespace std;
int main() {
int n1,n2,n3;
cin>>n1>>n2>>n3;
int m1[n1][n2];
int m2[n2][n3];
for(int i=0;i<n1;i++){
for(int j=0; j<n2;j++){
cin>>m1[i][j];
}
}
for(int i=0;i<n2;i++){
for(int j=0; j<n3;j++){
cin>>m2[i][j];
}
}
int ans[n1][n3];
for(int i=0;i<n1;i++){
for(int j=0; j<n3;j++){
ans[i][j] = 0 ;
for(int k=0; k<n2; k++){
ans[i][j] +=m1[i][k]*m2[k][j];
}
}
}
for(int i=0;i<n1;i++){
for(int j=0; j<n3;j++){
cout<<ans[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
You can also use function for getting input and printing output it may also reduce another loop.