Advertisement
filip710

RSS 134. zad

Feb 3rd, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <omp.h>
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <iomanip>
  6. using namespace std;
  7.  
  8. void matrixPrint(double **matrix, int rows, int columns);
  9. double** newRandomMatrix(int rows, int columns, double low, double high);
  10. double** newMatrix(int rows, int columns);
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.     int i, j, k;
  15.     int dim = 5;
  16.     omp_set_num_threads(4);
  17.  
  18.     double **matrix1 = newMatrix(dim, dim);
  19.     double **matrix2 = newMatrix(dim, dim);
  20.    
  21.     for(i = 0; i < dim; i++) {
  22.         for(j = 0; j < dim; j++) {
  23.             matrix1[i][j] = i + 3;
  24.             matrix2[i][j] = i + 2;
  25.         }
  26.     }
  27.     double **matrix3 = newMatrix(dim, dim);
  28.  
  29.     #pragma omp parallel for private(i,j,k)
  30.     for (i = 0; i < dim; i++)
  31.     {
  32.         for (j = 0; j < dim; j++)
  33.         {
  34.             for (k = 0; k < dim; k++)
  35.             {
  36.                 matrix3[i][j] += matrix1[i][k] * matrix2[k][j];
  37.             }
  38.         }
  39.     }
  40.  
  41.     cout << "Matrica 1:" << endl;
  42.     matrixPrint(matrix1, dim, dim);
  43.     cout << "Matrica 2:" << endl;
  44.     matrixPrint(matrix2, dim, dim);
  45.     cout << "Matrica 3:" << endl;
  46.     matrixPrint(matrix3, dim, dim);
  47.     return 0;
  48. }
  49.  
  50. void matrixPrint(double **matrix, int rows, int columns) {
  51.   int i, j;
  52.   std::cout << std::setprecision(2) << std::fixed;
  53.   std::cout << "Matrix: " << rows << "x" << columns << std::endl;
  54.   if (matrix == NULL)
  55.     std::cout << "Matrix is NULL!";
  56.   else {
  57.     for (i = 0; i < rows; i++) {
  58.       for (j = 0; j < columns; j++) {
  59.         std::cout << matrix[i][j] << "\t";
  60.       }
  61.       std::cout << std::endl;
  62.     }
  63.   }
  64.   std::cout << std::endl;
  65. }
  66.  
  67. double** newMatrix(int rows, int columns) {
  68.   int i, j;
  69.   double **matrix = new double*[rows];
  70.  
  71.   for(i = 0;i < rows; i++)
  72.   {
  73.     matrix[i] = new double[columns];
  74.     for(j = 0; j < columns; j++)
  75.     {
  76.         matrix[i][j] = 0;
  77.     }
  78.   }
  79.  
  80.   return matrix;
  81. }
  82.  
  83. double** newRandomMatrix(int rows, int columns, double low, double high) {
  84.   double **data;
  85.   int i, j;
  86.   data = newMatrix(rows, columns);
  87.   for (i = 0; i < rows; i++) {
  88.     for (j = 0; j < columns; j++) {
  89.       data[i][j] = (high - low)*((double)rand()/RAND_MAX) + low;
  90.     }
  91.   }
  92.   return data;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement