Advertisement
Jasir

Gauss Jordan

Oct 11th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. double a[105][105];
  2.  
  3. void PrintMatrix(int n){
  4.     for (int i = 0; i < n; i++) {
  5.         for (int j = 0; j <= n; j++)
  6.           cout << a[i][j] << " ";
  7.         cout << endl;
  8.     }
  9. }
  10.  
  11. int PerformOperation(int n){
  12.     int i, j, k = 0, c, flag = 0, m = 0;
  13.     double pro = 0.0;
  14.     for (i = 0; i < n; i++){
  15.         if (a[i][i] == 0.0){
  16.             c = 1;
  17.             while (a[i + c][i] == 0.0 && (i + c) < n)
  18.                 c++;
  19.             if ((i + c) == n) {
  20.                 flag = 1;
  21.                 break;
  22.             }
  23.             for (j = i, k = 0; k <= n; k++)
  24.                 swap(a[j][k], a[j+c][k]);
  25.         }
  26.         for (j = 0; j < n; j++) {
  27.             if (i != j) {
  28.                 double pro = a[j][i] / a[i][i];
  29.                 for (k = 0; k <= n; k++)
  30.                     a[j][k] = a[j][k] - (a[i][k]) * pro;
  31.             }
  32.         }
  33.     }
  34.     return flag;
  35. }
  36.  
  37. int CheckConsistency(int n){
  38.     int i, j;
  39.     double sum;
  40.     int flag = 3;
  41.     for (i = 0; i < n; i++){
  42.         sum = 0;
  43.         for (j = 0; j < n; j++)
  44.             sum = sum + a[i][j];
  45.         if (sum == a[i][j]){
  46.             flag = 2;
  47.             cout << i << endl;
  48.         }
  49.     }
  50.     return flag;
  51. }
  52.  
  53. void solve(int n){
  54.     int flag = PerformOperation(n);
  55.     if(flag==1) flag = CheckConsistency(n);
  56.     //PrintMatrix(a, n);
  57.     if (flag == 2)
  58.       cout << "Infinite Solutions Exists" << endl;
  59.     else if (flag == 3)
  60.       cout << "No Solution Exists" << endl;
  61.     else {
  62. //        for (int i = 0; i < n; i++)
  63. //            cout << a[i][n] / a[i][i] << " ";
  64.         double res = a[0][100]/a[0][0];
  65.         cout << fixed;
  66.         cout << setprecision(10) << res << endl;
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement