Advertisement
HyperSensualNarwhal

Determinant by Rule of Sarrus

Jan 2nd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. // Determinant by Rule of Sarrus
  2.  
  3. #include <iostream>
  4. #include <ctime>
  5.  
  6. using std::cout;
  7. using std::cin;
  8. using std::endl;
  9.  
  10. void main()
  11. {
  12.  
  13.     srand(time(0));
  14.  
  15.     double matrix[3][3];
  16.  
  17.     for (int i = 0; i < 3; ++i)
  18.     for (int j = 0; j < 3; ++j)
  19.         matrix[i][j] = rand() % 20 - 10;
  20.  
  21.  
  22.     double det = matrix[0][0] * matrix[1][1] * matrix[2][2] + matrix[1][0] * matrix[0][2] * matrix[2][1] +
  23.         matrix[0][1] * matrix[1][2] * matrix[2][0] - matrix[2][0] * matrix[1][1] * matrix[0][2] -
  24.         matrix[0][1] * matrix[1][0] * matrix[2][2] - matrix[0][0] * matrix[1][2] * matrix[2][1];
  25.  
  26.  
  27.     double det2 = 0.0;
  28.  
  29.     for (int i = 0; i < 3; i++)
  30.     {
  31.         for (int j = 0; j < 3; j++)
  32.         {
  33.             if (j == i) continue;
  34.             int k = 3 - i - j;
  35.             double x = matrix[0][i] * matrix[1][j] * matrix[2][k];
  36.             x *= ((i - j) * (j - k) *( k - i) > 0) ? 1 : -1;
  37.             det2 += x;
  38.         }
  39.     }
  40.  
  41.     cout << det << endl;
  42.     cout << det2 << endl;
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement