Advertisement
informaticage

Gauss Jordan sustitution

Mar 12th, 2021
1,275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #define SIZE 3
  4. using namespace std;
  5.  
  6. void print_matrix(int M[SIZE][SIZE + 1]) {
  7.   // La stampiamo
  8.   for (int i = 0; i < SIZE; i++) {
  9.     for (int j = 0; j < SIZE + 1; j++) {
  10.       cout << M[i][j] << " ";
  11.     }
  12.     cout << endl;
  13.   }
  14. }
  15.  
  16. double solve_matrix(int M[SIZE][SIZE + 1]) {
  17.   double
  18.       solutions[SIZE]; // l'array delle soluzioni che ho trovato alle incognite
  19.  
  20.   for (int i = 0; i < SIZE; i++) {
  21.     cout << SIZE - i << " , " << SIZE - i + 1 << " : ";
  22.     cout << M[SIZE - i - 1][SIZE - i] << endl;
  23.     for (int j = 0; j < i; j++) {
  24.       cout << M[(SIZE - i)][SIZE - i + 1] << " * " << solutions[j] << " = "
  25.            << M[SIZE - i][SIZE - i + 1] * solutions[j] << endl;
  26.       M[SIZE - i - 1][SIZE - i - 1] =
  27.           M[SIZE - i - 1][SIZE - i - 1] * solutions[j];
  28.     }
  29.     solutions[i] = (double)M[SIZE - i - 1][SIZE - i] /
  30.                    (double)M[SIZE - i - 1][SIZE - i - 1];
  31.   }
  32.  
  33.   cout << "Soluzione della Z: " << solutions[0] << endl;
  34.   return 0;
  35. }
  36.  
  37. int main(void) {
  38.   // Dichiriamo una SIZE x SIZE
  39.   int M[SIZE][SIZE + 1];
  40.  
  41.   // La carichiamo
  42.   for (int i = 0; i < SIZE; i++) {
  43.     for (int j = 0; j < SIZE + 1; j++) {
  44.       //   cout << "M"
  45.       //        << "[" << i << "]"
  46.       //        << "[" << j << "]: ";
  47.       cin >> M[i][j];
  48.     }
  49.   }
  50.  
  51.   print_matrix(M);
  52.  
  53.   cout << endl << endl;
  54.   solve_matrix(M);
  55.  
  56.   cout << endl;
  57.   print_matrix(M);
  58.   return 0;
  59. }
  60.  
  61. /**
  62.  *     [0][1][2][3]
  63.  *      #  #  #  S          [0]
  64.  *      0  #  #  S          [1]
  65.  *      0  0  #  S          [2]
  66.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement