macrofish

matrixes

Oct 20th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <iomanip>
  4. using namespace std;
  5. const int x = 5;
  6. const int y = 6;
  7. const int m = 10;
  8. const int n = 20;
  9. const int k = 30;
  10. const int a = 10;
  11. const int b = 10;
  12. int M3[m][n];
  13. int M6[x][y];
  14. int M8[a][b];
  15.  
  16. void multiply(int M3[m][n],int M1[m][k],int M2[k][n], unsigned int r, unsigned int c, unsigned int l)
  17. {
  18.     for (unsigned int r =0; r < m ; r++ )
  19.     {
  20.         for (unsigned int c = 0 ; c<n; c++)
  21.         {
  22.             for(unsigned int l=0; l<k; l++)
  23.             {
  24.                 M3[r][c] += M1[r][l]*M2[l][c];
  25.             }            
  26.         }
  27.     }
  28. }
  29.  
  30. void printMultiply(int M3[m][n],unsigned int r, unsigned int c)
  31. {
  32.     for (unsigned int r = 0; r<m; r++)
  33.     {
  34.         for(unsigned int c=0;c<n;c++)
  35.         {
  36.             cout<< setw(2);
  37.             cout << M3[r][c];
  38.         }
  39.     cout << endl;
  40.     }
  41. }
  42.  
  43. void printMatrix(int M1[m][k],unsigned int r,unsigned int c)
  44. {
  45.     for (unsigned int r = 0; r<m; r++)
  46.     {
  47.         for(unsigned int c=0;c<k;c++)
  48.         {
  49.             cout << setw(2);
  50.             cout << M1[r][c];
  51.         }
  52.     cout << endl;
  53.     }
  54. }
  55.  
  56. void printMatrix2(int M2[k][n], unsigned int r, unsigned int c)
  57. {
  58.     for (unsigned int r = 0; r<k; r++)
  59.     {
  60.         for(unsigned int c=0;c<n;c++)
  61.         {
  62.             cout<<setw(2);
  63.             cout << M2[r][c];
  64.         }
  65.     cout << endl;
  66.     }
  67. }
  68.  
  69. void printMatrix4(int M4[x][y], unsigned int r, unsigned int c)
  70. {
  71.     for (unsigned int r = 0; r<x; r++)
  72.     {
  73.         for(unsigned int c=0;c<y;c++)
  74.         {
  75.             cout<<setw(2);
  76.             cout << M4[r][c];
  77.         }
  78.     cout << endl;
  79.     }
  80. }
  81.  
  82. void printMatrix5(int M5[x][y], unsigned int r, unsigned int c)
  83. {
  84.     for (unsigned int r = 0; r<x; r++)
  85.     {
  86.         for(unsigned int c=0;c<y;c++)
  87.         {
  88.             cout<<setw(2);
  89.             cout << M5[r][c];
  90.         }
  91.     cout << endl;
  92.     }
  93. }
  94.  
  95. void SOUCET(int M6[x][y],int M4[x][y], int M5[x][y], unsigned int r, unsigned int c)
  96. {
  97.     for (unsigned int r = 0; r<x; r++)
  98.         for(unsigned int c=0; c<y; c++)
  99.         {
  100.             M6[r][c]=M4[r][c]+M5[r][c];
  101.         }
  102. }
  103.  
  104. void printSoucet(int M6[x][y], unsigned int r, unsigned int c)
  105. {
  106.     for (unsigned int r =0; r<x; r++)
  107.     {
  108.         for (unsigned int c=0; c<y; c++)
  109.         {
  110.             cout<<setw(2);
  111.             cout << M6[r][c];
  112.         }
  113.     cout << endl;
  114.     }
  115. }
  116.  
  117. void printMatrix7(int M7[m][m], unsigned int r, unsigned int c)
  118. {
  119.     for (unsigned int r = 0; r<m; r++)
  120.     {
  121.         for (unsigned int c = 0; c<m; c++)
  122.         {
  123.             cout<<setw(2);
  124.             cout << M7[r][c];
  125.         }
  126.     cout << endl;
  127.     }
  128. }
  129.  
  130. void TRANSPOSSED(int M8[a][b], int M7[a][b], unsigned int r, unsigned int c)
  131. {
  132.     for (unsigned int r = 0; r<m; r++)
  133.     {
  134.         for (unsigned int c= 0; c<m; c++)
  135.         {
  136.             M8[r][c]=M7[c][r];
  137.         }
  138.     }
  139. }
  140.  
  141. void printTRANSPOSSED(int M8[a][b], unsigned int r, unsigned int c)
  142. {
  143.     for  (unsigned int r=0; r<a; r++)
  144.     {
  145.         for (unsigned int c=0; c<b; c++)
  146.         {
  147.             cout<<setw(2);
  148.             cout << M8[r][c];
  149.         }
  150.     cout << endl;
  151.     }
  152. }
  153.  
  154. void upperTriang(int M7[a][b], unsigned r, unsigned c)
  155. {
  156.     for (unsigned int r=0; r < a; r++)
  157.     {
  158.         for (unsigned int c=0; c< b ; c++)
  159.         {
  160.             if (c>r)
  161.             {
  162.                 cout<<setw(2)<< M7[r][c];
  163.             }
  164.             else
  165.             {
  166.             cout <<setw(2) << ' ';
  167.             }
  168.         }
  169.         cout << endl;
  170.     }
  171. }
  172.  
  173. void lowerTriang(int M7[a][b], unsigned r, unsigned c)
  174. {
  175.     for (unsigned int r=0; r < a; r++)
  176.     {
  177.         for (unsigned int c=0; c< b ; c++)
  178.        
  179.         {
  180.             if (c<r)
  181.             {
  182.                 cout<<setw(2)<< M7[r][c];
  183.             }
  184.             else
  185.             {
  186.             cout <<setw(2) << ' ';
  187.             }
  188.         }
  189.         cout << endl;
  190.     }
  191. }
  192.  
  193. void rotateBYdiagonal(int M7[a][b], unsigned int r, unsigned int c)
  194. {
  195.     for (unsigned int r=0; r<a; r++)
  196.     {
  197.         for (unsigned int c=0; c<b; c++)
  198.         {
  199.             if (c != r)
  200.             {
  201.                 cout << setw(2) << M7[c][r];
  202.             }
  203.             else
  204.             {
  205.                 cout<< setw(2) << M7[r][c];
  206.             }
  207.         }
  208.         cout << endl;
  209.     }
  210. }
  211.  
  212. void diagonal(int M7[a][b], unsigned r, unsigned c)
  213. {
  214.     for (unsigned int r=0; r < a; r++)
  215.     {
  216.         for (unsigned int c=0; c< b ; c++)
  217.         {
  218.             if (c==r)
  219.             {
  220.                 cout<<setw(2)<< M7[r][c];
  221.             }
  222.             else
  223.             {
  224.                 cout<<setw(2)<< ' ';
  225.             }
  226.         }
  227.         cout << endl;
  228.     }
  229. }
  230.  
  231. int main()
  232. {
  233.     int M1[m][k];
  234.     int M2[k][n];
  235.     int M4[x][y];
  236.     int M5[x][y];
  237.     int M7[m][m];
  238.     for (unsigned int i=0; i < m ; i++)
  239.         for (unsigned int j=0; j < k; j++)
  240.             {
  241.                 M1[i][j]=2;
  242.             }
  243.     for (unsigned int i=0; i < k ; i++)
  244.         for (unsigned int j=0; j < n; j++)
  245.             {
  246.                 M2[i][j]=1;
  247.             }
  248.     for (unsigned int i=0; i < x ; i++)
  249.          for (unsigned int j=0; j < y; j++)
  250.             {
  251.                 M4[i][j]=6;
  252.             }
  253.     for (unsigned int i=0; i < x ; i++)
  254.         for (unsigned int j=0; j < y; j++)
  255.             {
  256.                 M5[i][j]=5;
  257.             }
  258.     for (unsigned int i=0; i < m ; i++)
  259.         for (unsigned int j=0; j < m; j++)
  260.             {
  261.                 M7[i][j] = rand()%10;
  262.             }
  263.     multiply(M3,M1,M2,m,n,k);
  264.     printMatrix(M1,m,k);  
  265.     printMatrix2(M2,k,n);
  266.     printMultiply(M3,m,n);
  267.     printMatrix4(M4,x,y);
  268.     printMatrix5(M5,x,y);
  269.     SOUCET(M6,M4,M5,x,y);
  270.     printSoucet(M6,x,y);
  271.     printMatrix7(M7,a,b);
  272.     cout << endl;
  273.     TRANSPOSSED(M8,M7,a,b);
  274.     printTRANSPOSSED(M8,a,b);
  275.     cout << endl;
  276.     upperTriang(M7,a,b);
  277.     cout << endl;
  278.     lowerTriang(M7,a,b);
  279.     cout << endl;
  280.     diagonal(M7,a,b);
  281.     cout << endl;
  282.     rotateBYdiagonal(M7,a,b);
  283. return 0;
  284. }
Advertisement
Add Comment
Please, Sign In to add comment