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?
i == 0 && i < row;it is set this way, because the expression is solved for one iteration of the cyclei, i.e., if the value ofiis greater, then there will be a way out of the matrix in rows (see the formula) - Strngrj < col + 1can be changed toj <= colin this case it does not play a special role - Strngrjis not equal tocolvalue no longer corresponds to Sarrus's rule? - Strngr