Aleksandr_Grigoryev

CLASSWORK

Nov 23rd, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int main()
  5. {
  6. int i,j;
  7. ofstream fout("fout.txt");
  8. const int n1=2;
  9. const int m1=2;
  10. const int n2=2;
  11. const int m2=2;
  12. int a[n1][m1];
  13.         fout<<"size A="<<n1<<'*'<<m1<<endl;
  14.     for (i=0;i<n1;i++)
  15. {
  16.     for (j=0;j<m1;j++)
  17.         a[i][j]=rand()%10;
  18. }
  19.     for (i=0;i<n1;i++)
  20. {
  21.     for (j=0;j<m1;j++)
  22.         fout<<a[i][j]<<' ';
  23. fout<<endl;
  24. }
  25. fout<<endl;
  26.         int b[n2][m2];
  27.         fout<<"size B="<<n2<<'*'<<m2<<endl;
  28.     for (i=0;i<n2;i++)
  29. {
  30.     for (j=0;j<m2;j++)
  31.         b[i][j]=rand()%10;
  32. }
  33.     for (i=0;i<n2;i++)
  34. {
  35.     for (j=0;j<m2;j++)
  36.         fout<<b[i][j]<<' ';
  37.         fout<<endl;
  38. }
  39. fout.close();
  40.         ifstream fin("fout.txt");
  41.         for (i=0;i<n1;i++)
  42. {
  43.     for (j=0;j<m1;j++)
  44.         fin>>a[i][j];
  45. }
  46.     for (i=0;i<n2;i++)
  47. {
  48.     for (j=0;j<m2;j++)
  49.         fin>>b[i][j];
  50. }
  51. int c[n1][n2];
  52.     if (n1==n2&&m1==m2)
  53. {
  54.     for (i=0;i<n2;i++)
  55. {
  56.     for (j=0;j<m2;j++)
  57.         c[i][j]=a[i][j]+b[i][j];
  58. }
  59.     for (i=0;i<n2;i++)
  60. {
  61.     for (j=0;j<m2;j++)
  62.         cout<<c[i][j]<<' ';
  63.         cout<<endl;
  64. }
  65. }
  66.     else {
  67.         cout<<"can not be folded";
  68.         cout<<endl;
  69.         }
  70. int d[n1][n2],p,s;
  71.         s=0;
  72.     if (n1==m2)
  73. {
  74.     for (i=0;i<n1;i++)
  75. {
  76.     for (j=0;j<m2;j++)
  77. {
  78.         d[i][j]=0;
  79.     for (int k=0;k<n1;k++)
  80.         d[i][j]+=a[i][k]*b[k][j];
  81. }
  82. }
  83.     for (i=0;i<n2;i++)
  84. {
  85. for (j=0;j<m2;j++)
  86.         cout<<d[i][j]<<' ';
  87.         cout<<endl;
  88. }
  89. }
  90. else cout<<"can not be multiplied";
  91. fin.close();
  92. system("pause");
  93. return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment