Advertisement
a53

MMultiplication

a53
Jan 20th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. #include <fstream>
  2. #define N 1000
  3. using namespace std;
  4. int a[N][N],b[N][N],c[N][N];
  5.  
  6. int main()
  7. {
  8. int n,m;
  9. ifstream f("mmultiplication.in");
  10. f>>n>>m;
  11. for(int i=0;i<n;++i)
  12. for(int j=0;j<m;++j)
  13. f>>a[i][j];
  14. int p,q;
  15. f>>p>>q;
  16. ofstream g("mmultiplication.out");
  17. if(m!=p)
  18. {
  19. g<<-1;
  20. return 0;
  21. }
  22. p=q;
  23. for(int i=0;i<m;++i)
  24. for(int j=0;j<p;++j)
  25. f>>b[i][j];
  26. for(int i=0;i<n;++i)
  27. for(int j=0;j<p;++j)
  28. for(int k=0;k<m;++k)
  29. c[i][j]+=a[i][k]*b[k][j];
  30. for(int i=0;i<n;++i)
  31. {
  32. for(int j=0;j<p;++j)
  33. g<<c[i][j]<<' ';
  34. g<<endl;
  35. }
  36. return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement