Crazyblack

gaus 1.2

Nov 9th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. /* Algorytm eliminacji Gaussa Jordana */
  2.  
  3. #include<iostream>
  4. #include<conio.h>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.    int i, j, k, n;
  11.    float a[10][10] = { 0 }, x;
  12.    cout << " Algorytm eliminacji Gaussa Jordana " << endl <<endl ;
  13.    cout << "Podaj ilosc rownan: ";
  14.    cin >> n;
  15.    cout << endl << "Wpisz macierz: " << endl;
  16.    
  17.     for (i = 1; i <= n; i++)
  18.        for (j = 1; j <= n; j++)
  19.        
  20.    cin >> a[i][j];
  21.  
  22.     for (i = 1; i <= n; i++)
  23.        for (j = 1; j <= 2 * n; j++)
  24.           if (j == (i + n))
  25.                 a[i][j] = 1;
  26.                 cout << endl;              
  27.    
  28.     /* czesciowe odwracanie */
  29.     for (i = n; i > 1; i--)
  30.     {
  31.        if (a[i - 1][1] < a[i][1])
  32.          for (j = 1; j <= n * 2; j++)
  33.          {
  34.             x = a[i][j];
  35.             a[i][j] = a[i - 1][j];
  36.             a[i - 1][j] = x;
  37.          }
  38.     }
  39.    
  40.     cout << "Odwrocone: " << endl;
  41.        for (i = 1; i <= n; i++)
  42.        {
  43.           for (j = 1; j <= n * 2; j++)
  44.               cout << a[i][j] << "    ";
  45.               cout << endl;
  46.     }
  47.    
  48.     /* Redukcja */
  49.     for (i = 1; i <= n; i++)
  50.     {
  51.         for (j = 1; j <= n * 2; j++)
  52.             if (j != i)
  53.             {
  54.                 x = a[j][i] / a[i][i];
  55.                 for (k = 1; k <= n * 2; k++)
  56.                     a[j][k] -= a[i][k] * x;
  57.             }
  58.     }
  59.    
  60.     /* Redukcja */
  61.     for (i = 1; i <= n; i++)
  62.     {
  63.         x = a[i][i];
  64.         for (j = 1; j <= n * 2; j++)
  65.             a[i][j] = a[i][j] / x;
  66.     }
  67.  
  68.     cout << endl <<"Twoj wynik to: " << endl;
  69.     for (i = 1; i <= n; i++)
  70.     {
  71.         for (j = n + 1; j <= n * 2; j++)
  72.             cout << a[i][j] << "  |  ";
  73.             cout << endl;
  74.     }
  75.  
  76.     cout << endl << "Aby zamknac nacisnij Enter";
  77.  
  78.     getch();
  79.     return 0;
  80. }
Add Comment
Please, Sign In to add comment