Advertisement
farhansadaf

SeperateCoefficients & Gauss Seidel iteration

Sep 2nd, 2019
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void seperateCoefficient(string eqn, int a[])
  6. {
  7.     bool isNegative = false;
  8.     int i, sum = 0;
  9.  
  10.     //ax + by + cz
  11.     for(i = 0; eqn[i] != '='; i++){
  12.         if(eqn[i] == '-')
  13.             isNegative = true;
  14.         else if(eqn[i] == '+')
  15.             isNegative = false;
  16.         else if(eqn[i] >= '0' && eqn[i] <= '9'){
  17.             sum *= 10;
  18.             sum += eqn[i] - '0';
  19.         }
  20.         else if(eqn[i] == 'x' || eqn[i] == 'X'){
  21.             if(isNegative)
  22.                 sum = -sum;
  23.             a[0] = sum;
  24.             sum = 0;
  25.         }
  26.         else if(eqn[i] == 'y' || eqn[i] == 'Y'){
  27.             if(isNegative)
  28.                 sum = -sum;
  29.             a[1] = sum;
  30.             sum = 0;
  31.         }
  32.         else if(eqn[i] == 'z' || eqn[i] == 'Z'){
  33.             if(isNegative)
  34.                 sum = -sum;
  35.             a[2] = sum;
  36.             sum = 0;
  37.         }
  38.     }
  39.     // = const
  40.     isNegative = false;
  41.     for(; i < eqn.length(); i++){
  42.         if(eqn[i] == '-')
  43.             isNegative = true;
  44.         else if(eqn[i] >= '0' && eqn[i] <= '9'){
  45.             sum *= 10;
  46.             sum += eqn[i] - '0';
  47.         }
  48.     }
  49.     if(isNegative)
  50.         sum = -sum;
  51.     a[3] = sum;
  52. }
  53.  
  54.  
  55. int main()
  56. {
  57.     int a[3][4];
  58.     string eqn;
  59.  
  60.     for(int i = 0; i < 3; i++){
  61.         getline(cin, eqn);
  62.         seperateCoefficient(eqn, a[i]);
  63.     }
  64.  
  65.     //printing
  66.     cout << endl << "Co-efficients: " << endl;
  67.     for(int i = 0; i < 3; i++){
  68.         for(int j = 0; j < 4; j++){
  69.             cout << a[i][j] << "  ";
  70.         }
  71.         cout << endl;
  72.     }
  73.  
  74.     double x = 0, y = 0, z = 0;
  75.     for(int i = 0; i < 20; i++){
  76.         x = (a[0][3] - a[0][1]*y - a[0][2]*z) / a[0][0];
  77.         y = (a[1][3] - a[1][0]*x - a[1][2]*z) / a[1][1];
  78.         z = (a[2][3] - a[2][0]*x - a[2][1]*y) / a[2][2];
  79.     }
  80.     cout << endl << "Solution:" << endl;
  81.     cout << "x = " << x << endl;
  82.     cout << "y = " << y << endl;
  83.     cout << "z = " << z << endl;
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement