Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <time.h>
  4. using namespace std;
  5.  
  6. int main() {
  7.     srand(time(NULL));
  8.     const int X = 20;
  9.     const int Y = 30;
  10.     const int Z = 20;
  11.  
  12.     int matrixA[X][Y];
  13.     int matrixB[Z][X];
  14.  
  15.     //Generowanie macierzy rzadkich
  16.     for(int i=0; i<X; ++i) {
  17.         for(int j=0; j<Y; ++j) {
  18.             matrixA[i][j] = (rand() % 10 == 0) ? rand() % 10 : 0;
  19.         }
  20.         for(int k=0; k<Z; ++k) {
  21.             matrixB[k][i] = (rand() % 10 == 0) ? rand() % 10 : 0;
  22.         }
  23.     }
  24.  
  25.     //Wyświetlanie macierzy
  26.     cout << "MacierzA" << endl;
  27.     for(int i=0; i<X; ++i) {
  28.         for(int j=0; j<Y; ++j) {
  29.             cout << matrixA[i][j] << " ";
  30.         }
  31.         cout << endl;
  32.     }
  33.  
  34.     cout << endl;
  35.  
  36.     //Liczenie ilości wartości w macierzy
  37.     int noValuesA = 0;
  38.  
  39.     for(int i=0; i<X; ++i) {
  40.         for(int j=0; j<Y; ++j) {
  41.             if(matrixA[i][j]!=0) noValuesA++;
  42.         }
  43.     }
  44.  
  45.     //Zamiana macierzy na COO
  46.     int indexA = 0;
  47.     int valuesA[noValuesA], rowA[noValuesA], colA[noValuesA];
  48.  
  49.     for(int i=0; i<X; ++i) {
  50.         for(int j=0; j<Y; ++j) {
  51.             if(matrixA[i][j]!=0) {
  52.                 valuesA[indexA] = matrixA[i][j];
  53.                 rowA[indexA] = i;
  54.                 colA[indexA] = j;
  55.                 indexA++;
  56.             }
  57.         }
  58.     }
  59.  
  60.     //Wyświetlanie macierzy COO
  61.     cout << "Wartości A" << endl;
  62.     for(int i=0; i<noValuesA; ++i) {
  63.         cout << valuesA[i] << " ";
  64.     }
  65.  
  66.     cout << endl;
  67.  
  68.     cout << "Wiersze A" << endl;
  69.     for(int i=0; i<noValuesA; ++i) {
  70.         cout << rowA[i] << " ";
  71.     }
  72.  
  73.     cout << endl;
  74.  
  75.     cout << "Kolumny A" << endl;
  76.     for(int i=0; i<noValuesA; ++i) {
  77.         cout << colA[i] << " ";
  78.     }
  79.  
  80.     cout << endl << endl;
  81.    
  82.  
  83.     //Wyświetlanie macierzy
  84.     cout << "Macierz B" << endl;
  85.     for(int i=0; i<Z; ++i) {
  86.         for(int j=0; j<X; ++j) {
  87.             cout << matrixB[i][j] << " ";
  88.         }
  89.         cout << endl;
  90.     }
  91.  
  92.     cout << endl;
  93.  
  94.     //Liczenie ilości wartości w macierzy
  95.     int noValuesB = 0;
  96.  
  97.     for(int i=0; i<Z; ++i) {
  98.         for(int j=0; j<X; ++j) {
  99.             if(matrixB[i][j]!=0) noValuesB++;
  100.         }
  101.     }
  102.  
  103.     //Zamiana macierzy na COO
  104.     int indexB = 0;
  105.     int valuesB[noValuesB], rowB[noValuesB], colB[noValuesB];
  106.  
  107.     for(int i=0; i<Z; ++i) {
  108.         for(int j=0; j<X; ++j) {
  109.             if(matrixB[i][j]!=0) {
  110.                 valuesB[indexB] = matrixB[i][j];
  111.                 rowB[indexB] = i;
  112.                 colB[indexB] = j;
  113.                 indexB++;
  114.             }
  115.         }
  116.     }
  117.  
  118.     //Wyświetlanie macierzy COO
  119.     cout << "Wartości B" << endl;
  120.     for(int i=0; i<noValuesB; ++i) {
  121.         cout << valuesB[i] << " ";
  122.     }
  123.  
  124.     cout << endl;
  125.  
  126.     cout << "Wiersze B" << endl;
  127.     for(int i=0; i<noValuesB; ++i) {
  128.         cout << rowB[i] << " ";
  129.     }
  130.  
  131.     cout << endl;
  132.  
  133.     cout << "Kolumny B" << endl;
  134.     for(int i=0; i<noValuesB; ++i) {
  135.         cout << colB[i] << " ";
  136.     }
  137.  
  138.     cout <<  endl;
  139.  
  140.     //Wymnażanie
  141.     int matrixC[Y][Z];
  142.  
  143.     for(int i=0; i<Y; ++i) {
  144.         for(int j=0; j<Z; ++j) {
  145.             matrixC[i][j] = 0;
  146.         }
  147.     }
  148.  
  149.     //Wymnażanie COO
  150.     clock_t beginCOO = clock();
  151.  
  152.     for(int i=0; i<noValuesA; ++i) {
  153.         for(int j=0; j<noValuesB; ++j) {
  154.             if(rowA[i] == colB[j])
  155.                 matrixC[colA[i]][rowB[j]] += valuesA[i]*valuesB[j];
  156.         }
  157.     }
  158.  
  159.     clock_t endCOO = clock();
  160.  
  161.     cout << endl;
  162.  
  163.     //Wyświetlanie wyniku COO
  164.     cout << "Macierz wynikowa" << endl;
  165.     for(int i=0; i<Y; ++i) {
  166.         for(int j=0; j<Z; ++j) {
  167.             cout << setw(3) << matrixC[i][j] << " ";
  168.         }
  169.         cout << endl;
  170.     }
  171.  
  172.     //Wymnażanie Regularne
  173.     int matrixD[Y][Z];
  174.     for(int i=0; i<Y; ++i) {
  175.         for(int j=0; j<Z; ++j) {
  176.             matrixD[i][j] = 0;
  177.         }
  178.     }
  179.  
  180.     clock_t beginRegular = clock();
  181.  
  182.     for(int i=0; i<Y; ++i) {
  183.         for(int j=0; j<Z; ++j) {
  184.             int sum = 0;
  185.             for(int k=0; k<X; ++k) sum += matrixA[k][i]*matrixB[j][k];
  186.             matrixD[i][j] = sum;
  187.         }
  188.     }
  189.  
  190.     clock_t endRegular = clock();
  191.  
  192.     //Wyświetlanie wyniku regularnego
  193.     cout << endl;
  194.     for(int i=0; i<Y; ++i) {
  195.         for(int j=0; j<Z; ++j) {
  196.             cout << setw(3) << matrixD[i][j] << " ";
  197.         }
  198.         cout << endl;
  199.     }
  200.  
  201.  
  202.     cout << endl << "Czas COO: " << (double) (endCOO - beginCOO) / CLOCKS_PER_SEC << endl << "Czas reularny: " << (double) (endRegular - beginRegular) / CLOCKS_PER_SEC << endl;
  203.  
  204.     return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement