H. users, please help understand the calculation of the determinant of the matrix by the method of Sarrus , or rather correct the algorithm in the second cycle

#include <iostream> #include <ctime> using std::cout; using std::cin; using std::endl; void main() { setlocale(LC_ALL, "Russian"); srand(time(NULL)); const int col = 5, row = 3; int matrix[col][row] = {}; int det = 0; for (int i = 0; i < row; cout << endl, i++) // инициализация матрицы случайными значениями for (int j = 0; j < col; j++) { if (j < 3) cout << (matrix[i][j] = rand() % 5 + 1) << " "; else matrix[i][j] = matrix[i][j - 3]; } for (int i = 0; i == 0 && i < row; cout << endl, i++) // вычисление определителя for (int j = 0; j < col + 1; j++) (j < 3) ? det += (matrix[i][j] * matrix[i + 1][j + 1] * matrix[i + 2][j + 2]) : det -= (matrix[i][j - 1] * matrix[i + 1][j - 2] * matrix[i + 2][j - 3]); cout << "det = " << det << endl; } 

Actually, the expression det -= (matrix[i][j - 1] * matrix[i + 1][j - 2] * matrix[i + 2][j - 3]) does not work correctly in the last two iterations. Why? How to fix?

  • Thanks to this condition, i == 0 && i <row; the loop will run only once. Due to this condition, j <col + 1 program has undefined behavior. - Vlad from Moscow
  • @Vlad from Moscow condition i == 0 && i < row; it is set this way, because the expression is solved for one iteration of the cycle i , i.e., if the value of i is greater, then there will be a way out of the matrix in rows (see the formula) - Strngr
  • @Vlad from Moscow the condition j < col + 1 can be changed to j <= col in this case it does not play a special role - Strngr
  • if i is fixed, then it does not make sense to write a cycle. As for the internal cycle, the memory is accessed outside the array with j equal to col - Vlad from Moscow
  • @Vlad from Moscow so be it, but then why in the penultimate iteration of the nested loop, when j is not equal to col value no longer corresponds to Sarrus's rule? - Strngr

1 answer 1

You know, this:

 det = matrix[0][0]*matrix[1][1]*matrix[2][2]+matrix[1][0]*matrix[0][2]*matrix[2][1]+ matrix[0][1]*matrix[1][2]*matrix[2][0]-matrix[2][0]*matrix[1][1]*matrix[0][2]- matrix[0][1]*matrix[1][0]*matrix[2][2]-matrix[0][0]*matrix[1][2]*matrix[2][1]; 

hardly longer, and certainly with less overhead than your

 for (int i = 0; i == 0 && i < row; cout << endl, i++) // вычисление определителя for (int j = 0; j < col + 1; j++) (j < 3) ? det += (matrix[i][j] * matrix[i + 1][j + 1] * matrix[i + 2][j + 2]) : det -= (matrix[i][j - 1] * matrix[i + 1][j - 2] * matrix[i + 2][j - 3]); 

For such simple formulas, it makes no sense to come up with complex solutions ...

Update

Through the cycle - so through the cycle ...

 double matrix[3][3]; int main(int argc, const char * argv[]) { srand(time(0)); for(int i = 0; i < 3; ++i) for(int j = 0; j < 3; ++j) matrix[i][j] = rand()%20 - 10; double det = matrix[0][0]*matrix[1][1]*matrix[2][2]+matrix[1][0]*matrix[0][2]*matrix[2][1]+ matrix[0][1]*matrix[1][2]*matrix[2][0]-matrix[2][0]*matrix[1][1]*matrix[0][2]- matrix[0][1]*matrix[1][0]*matrix[2][2]-matrix[0][0]*matrix[1][2]*matrix[2][1]; double d2 = 0.0; for(int i = 0; i < 3; ++i) { for(int j = 0; j < 3; ++j) { if (j == i) continue; int k = 3-ij; double x = matrix[0][i]*matrix[1][j]*matrix[2][k]; x *= ((ij)*(jk)*(ki) > 0) ? 1 : -1; d2 += x; } } printf("%lf\n",det); printf("%lf\n",d2); } 

As you can see, the result is correct :)

  • I understand, it’s so much easier and faster, I would have done it too, but my sensei insisted that the task be decided with the help of a cycle, so, apparently, I’ll have to think it out myself - Strngr
  • Well, see the corrected answer - with cycles :) - Harry
  • 2
    I would write k=3-ij instead of the third cycle. - Mikhailo
  • @Mikhailo Thank you, you are absolutely right. “Like I didn’t touch it myself” (c) - Harry