Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #define SIZE 4
  2.  
  3. #include <iostream>
  4. #include <thread>
  5. using namespace std;
  6.  
  7. int x=4;
  8. int y=4;
  9. int matA[SIZE][SIZE];
  10. int matB[SIZE][SIZE];
  11. int matC[SIZE][SIZE];
  12. int matTemp[SIZE][SIZE];
  13. int matRes[SIZE][SIZE];
  14.  
  15. void fun(int i,int j){
  16.     //mnozysz wiersdz*kolumna
  17.     int temp=0;
  18.     for(int k=0;k<SIZE;k++)
  19.     {
  20.         temp+=matA[i][k]*matB[k][j];
  21.     }
  22.     //matTemp trzyma wyniki mnozenia A*B
  23.     matTemp[i][j]=temp;
  24.     //Przemnoz z C (matTemp*C => A*B*C)
  25.     cout<<matTemp<<endl;
  26.     for(int z=0; z<x; z++){
  27.         temp+=matTemp[i][z]*matC[z][j];
  28.     }
  29.     matRes[i][j]=temp;
  30. }
  31.  
  32.  
  33. int main() {
  34.  
  35.     //Wstawienie do macierzy losowych wartosci
  36.     for(int i=0;i<SIZE;i++)
  37.     {
  38.         for(int j=0;j<SIZE;j++)
  39.         {
  40.             matA[i][j] = rand() % 10;
  41.             matB[i][j] = rand() % 10;
  42.             matC[i][j] = rand() % 10;
  43.         }
  44.     }
  45.  
  46.     //Wypisanie macierzy
  47.     for(int i=0;i<SIZE;i++)
  48.         {
  49.             for(int j=0;j<SIZE;j++)
  50.             {
  51.                 cout<<matA[i][j]<<" ";
  52.             }
  53.             cout<<endl;
  54.         }
  55.     cout<<endl;
  56.     for(int i=0;i<SIZE;i++)
  57.         {
  58.             for(int j=0;j<SIZE;j++)
  59.             {
  60.                 cout<<matB[i][j]<<" ";
  61.             }
  62.             cout<<endl;
  63.         }
  64.     cout<<endl;
  65.     for(int i=0;i<SIZE;i++)
  66.         {
  67.             for(int j=0;j<SIZE;j++)
  68.             {
  69.                 cout<<matC[i][j]<<" ";
  70.             }
  71.             cout<<endl;
  72.         }
  73.  
  74.     //jedno pole w macierzy wynikowej = jeden watek
  75.     thread thr[SIZE][SIZE];
  76.     for(int i=0;i<SIZE;i++)
  77.     {
  78.         for(int j=0;j<SIZE;j++)
  79.         {
  80.             thr[i][j]=thread(fun,i,j);
  81.         }
  82.     }
  83.  
  84.     //join
  85.     for(int i=0;i<SIZE;i++)
  86.         {
  87.             for(int j=0;j<SIZE;j++)
  88.             {
  89.                 thr[i][j].join();
  90.             }
  91.         }
  92.  
  93.     cout<<"\nResult: "<<endl;
  94.     for(int i=0;i<SIZE;i++)
  95.             {
  96.                 for(int j=0;j<SIZE;j++)
  97.                 {
  98.                     cout<<matRes[i][j]<<" ";
  99.                 }
  100.                 cout<<endl;
  101.             }
  102.         cout<<endl;
  103.  
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement