tranerius

2. Ввод матрицы 3х3 и подсчет определителя.

Dec 11th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include <iostream>
  2. int main() {
  3.     double matrix[3][3], fs[3], dsm[3][2][2], pdet[3], det[3], determinant; int i, j, m, l; char a;
  4.     for (i = 0; i < 3; i++) {
  5.         for (j = 0; j < 3; j++) {
  6.             std::cout << "Enter " << i + 1 << "," << j + 1 << " element of matrix: ";
  7.             std::cin >> matrix[i][j];
  8.         }
  9.     }
  10.     std::cout << "Do you matrix look like:" << std::endl;
  11.     do {
  12.         for (i = 0; i<3; i++) {
  13.             for (j = 0; j < 3; j++) {
  14.                 std::cout << matrix[i][j] << " ";
  15.             }
  16.             std::cout << std::endl;
  17.         }
  18.         std::cout << "You should enter answer (y/n): ";
  19.         std::cin >> a;
  20.         if (a != 'y' && a != 'n') {
  21.             std::cout << "You didn't answer" << std::endl;
  22.         }
  23.         else if (a == 'n') {
  24.             std::cout << "What element is incorrect?" << std::endl;
  25.             do {
  26.                 std::cout << "Please enter string number: ";
  27.                 std::cin >> i;
  28.                 if (i > 3 || i < 1) {
  29.                     std::cout << "Error. Incorrect number. Try again." << std::endl;
  30.                 }
  31.             } while (i > 3 || i < 1);
  32.             i = i - 1;
  33.             do {
  34.                 std::cout << "Please enter column number: ";
  35.                 std::cin >> j;
  36.                 if (j > 3 || j < 1) {
  37.                     std::cout << "Error. Incorrect number. Try again." << std::endl;
  38.                 }
  39.             } while (j > 3 || j < 1);
  40.             j = j - 1;
  41.             std::cout << "Please enter correct value: ";
  42.             std::cin >> matrix[i][j];
  43.             std::cout << "Now right?" << std::endl;
  44.         }
  45.     } while (a == 'n' || a != 'y');
  46.     for (m = 0; m < 3; m++) {
  47.         for (j = m; j < m + 1; j++) {
  48.             if (m % 2 == 0) {
  49.                 fs[m] = matrix[0][j];
  50.             }
  51.             else {
  52.                 fs[m] = -matrix[0][j];
  53.             }
  54.         }
  55.     }
  56.     i = 0; j = -1; m = -1; l = -1;
  57.     while (true) {
  58.         m = m + 1;
  59.         if (m == 3) { m = -1; break; }
  60.         while (true) {
  61.             i = i + 1;
  62.             if (i == 3) { i = 0; break; }
  63.             while (true) {
  64.                 j = j + 1;
  65.                 if (j == 3) { j = -1; l = -1; break; }
  66.                 else if (j == m) { continue; }
  67.                 l = l + 1;
  68.                 dsm[m][i - 1][l] = matrix[i][j];
  69.             }
  70.         }
  71.     }
  72.     for (m = 0; m < 3; m++) {
  73.         i = 1; j = 1;
  74.         pdet[m] = dsm[m][i - 1][j - 1] * dsm[m][i][j] - dsm[m][i - 1][j] * dsm[m][i][j - 1];
  75.         det[m] = pdet[m] * fs[m];
  76.     }
  77.     determinant = det[0] + det[1] + det[2];
  78.     std::cout << "Determinant is " << determinant << std::endl;
  79.     return 0;
  80. }
Add Comment
Please, Sign In to add comment