# Gauss - Seidel Method

Apr 10th, 2021
735
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1.
2. /// 1807062
3.
4. #include<bits/stdc++.h>
5. #include<string.h>
6. using namespace std;
7.
8. #define see(x)  cout<<endl<<#x<<" : "<<(x)<<endl;
9.
10. double a[15];
11.
12. double get_X(double y,double z)
13. {
14.     return (a[4]-a[2]*y-a[3]*z)/a[1];
15. }
16.
17. double get_Y(double x,double z)
18. {
19.     return (a[8]-a[5]*x-a[7]*z)/a[6];
20. }
21.
22. double get_Z(double x,double y)
23. {
24.     return (a[12]-a[9]*x-a[10]*y)/a[11];
25. }
26.
27. int index = 1;
28.
29. double get_Values(string s)
30. {
31.     int i = 0, to = 0;
32.
33.     while(s[i]!='x')i++;
34.
35.     double power = 0, value = 0;
36.     if(s[0]=='-')to = 1;
37.     if(i==0)value = 1;
38.     for(int  j = i-1;j>=to;j--)
39.         value += pow(10.0,power++) * (s[j]-'0');
40.     if(to) value = -value;
41.
42.     a[index++] = value;
43.
44.     i++; /// '+' or '-'
45.     char cy = s[i]; /// sign of the value
46.     while(s[i]!='y')i++;
47.
48.     power = 0,value = 0;
49.     int j = i-1;
50.     if(s[j]==cy)value=1;
51.     while(1)
52.     {
53.         if(s[j]=='+' || s[j]=='-')break;
54.         value += pow(10.0,power++) * (s[j]-'0'),j--;
55.     }
56.     if(cy=='-')value = -value;
57.
58.     a[index++] = value;
59.
60.     i++; /// '+' or '-'
61.     char cz = s[i];
62.     while(s[i]!='z')i++;
63.
64.     power = 0,value = 0;
65.     j = i-1;
66.     if(s[j]==cz)value=1;
67.     while(1)
68.     {
69.         if(s[j]=='+' || s[j]=='-')break;
70.         value += pow(10.0,power++) * (s[j]-'0'),j--;
71.     }
72.     if(cz=='-')value = -value;
73.
74.     a[index++] = value;
75.
76.     i++; /// '='
77.     to = i;
78.     power = 0,value = 0;
79.     if(s[i+1]=='-') to++;
80.     for(int j = s.size()-1;j>to;j--)
81.         value += pow(10.0,power++) * (s[j]-'0');
82.     if(to!=i)value = -value;
83.
84.     a[index++] = value;
85. }
86.
87. double Gauss_Seidel()
88. {
89.     double X = 0, Y = 0, Z = 0;
90.
91.     for(int it = 0; it< 30; it++)
92.     {
93.         printf("Iteration No. %d : ",it+1);
94.         X = get_X(Y,Z);
95.         Y = get_Y(X,Z);
96.         Z = get_Z(X,Y);
97.         printf("X = %lf, Y = %lf, Z = %lf\n",X,Y,Z);
98.     }
99.     printf("\nAnswer : \nX = %lf, Y = %lf, Z = %lf\n",X,Y,Z);
100. }
101.
102. int main()
103. {
104. //    ios::sync_with_stdio(0);
105. //    cin.tie(NULL);
106.
107.     ifstream in("input.txt");
108.
109.     string s;
110.
111.     puts("The three equations are : ");
112.
113.     while(in>>s)
114.     {
115.         get_Values(s);
116.         cout<<s<<endl;
117.     }
118.
119.     puts("The constants are : ");
120.
121.     cout<<"a1 = "<<a[1]<<endl;
122.     cout<<"b1 = "<<a[2]<<endl;
123.     cout<<"c1 = "<<a[3]<<endl;
124.     cout<<"d1 = "<<a[4]<<endl<<endl;
125.
126.     cout<<"a2 = "<<a[5]<<endl;
127.     cout<<"b2 = "<<a[6]<<endl;
128.     cout<<"c2 = "<<a[7]<<endl;
129.     cout<<"d2 = "<<a[8]<<endl<<endl;
130.
131.     cout<<"a3 = "<<a[9]<<endl;
132.     cout<<"b3 = "<<a[10]<<endl;
133.     cout<<"c3 = "<<a[11]<<endl;
134.     cout<<"d3 = "<<a[12]<<endl<<endl;
135.
136.     Gauss_Seidel();
137.
138.     in.close();
139.
140.     return 0;
141. }
142.
143. /*
144. 27x+6y-z=85
145. 6x+15y+z=72
146. x+y+54z=110
147.
148. -7x+5y-3z=16
149. -3x-5y+2z=-8
150. -5x+3y-7z=0
151. */
152.
RAW Paste Data