-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
326 lines (242 loc) · 9.05 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <iostream>
#include <cmath>
#include<cstdlib>
#include <time.h>
#include <iomanip>
using namespace std;
/*********************************************************************************
* QUADRATIC AND CUBIC EQUATION SOLVER PROGRAM *
* This program enable user to solve quadratic equation and cubic equation *
* by using fixed point iteration approach. *
*********************************************************************************/
void createQuadEqn(double,double,double);
void fixedPointQuad(double,double,double);
void createCubicEqn(double,double,double,double);
void fixedPointCube(double,double,double,double);
double f,g,h,j;
double x,y,z;
int main()
{ double h,i,j,k;
double a,b,c;
int userChoice;
cout<<"\n\t\t************** WELCOME TO EQUATION SOLVER **************\n"<<endl;
cout<<"\n\t\tThis program is to solve quadratic equation and cubic equation"<<endl;
cout<<"\t\tby using fixed number iteration."<<endl;
cout<<"\n\t\t>>Please Choose Any Type of Option \n";
cout<<"\n\t\t1.Quadratic Equation\n\n\t\t2.Cubic Equation\n\n\t\t3.Close Application\n";
cout<<"\n\t\tEnter your choice >> ";
cin >> userChoice;
if (userChoice==1)
{
createQuadEqn(a,b,c);
}
else if(userChoice==2)
{
createCubicEqn(h,i,j,k);
}
{
cout<<"\n\t\t==============================================================="<<endl;
cout<<"\n\t\tThank you for using our service! Please come again."<<endl;
cout<<"\n\t\t==============================================================="<<endl;
}
if (userChoice==3)
{
cout<<"\n\t\t==============================================================="<<endl;
cout<<"\n\t\tApplicaton closed....Thank you for using our service.";
cout<<"\n\t\tSee you again."<<endl;
cout<<"\n\t\t==============================================================="<<endl;
}
return 0;
}
/*****************************************************************************
* createQuadEqn *
* This function serve two choice for user to create a random quadratic *
* equation or create their own cubic equation by enter the value of *
* coefficient *
*****************************************************************************/
void createQuadEqn (double a, double b, double c)
{
int choiceNum;
double disc;
cout<<"\n\t\t************** CREATING QUADRATIC EQUATION **************\n"<<endl;
cout<<"\n\t\tNow,we will generate a quadratic equation"<<endl;
cout<<" \n\t\tThe form of the equation is ax^2 + bx + c = 0"<<endl;
cout<<endl;
cout<<"\n\t\tPlease choose any option."<<endl;
cout<<"\n\t\t1.Random Number Generator\n\n\t\t2.My Own Coefficient\n";
cout<<endl;
cout<<"\n\t\tEnter your choice>> ";
cin >> choiceNum;
cout<<endl;
if (choiceNum == 1)
{
srand (time(NULL));
a=rand()%5+1;
b=-1*(rand()%50+1);
c= rand()%5+1;
cout<<"\n\t\tThe quadratic equation is ";
cout<<a<<"x^2 + "<<b<<"x + "<<c<<" = 0"<<endl;
cout<<endl;
disc= pow(b,2)-4*a*c;
cout<<"\t\tDiscriminant value is : "<< disc<<endl;
if (disc >= 0)
{
fixedPointQuad(a,b,c);
}
else
{
cout<<"\t\tThe discriminant must above or equal 0. Thank you"<<endl;
}
}
else if(choiceNum == 2)
{
cout<<"\t\tEnter your first number: ";
cin >> a;
cout<<"\t\tEnter your second number: ";
cin >> b;
cout<<"\t\tEnter your third number: ";
cin >> c;
cout<<"\n\t\tThe quadratic equation is ";
cout<<a<<"x^2 + "<<b<<"x + "<<c<<" = 0"<<endl;
disc= pow(b,2)-4*a*c;
cout<<"\n\t\tDiscriminant value is : "<< disc<<endl;
cout<<endl;
if (disc >= 0)
{
fixedPointQuad(a,b,c);
}
else
{
cout<<"\t\tThe discriminant must above or equal 0. Thank you."<<endl;
}
}
else
{
cout<<"\t\tThank you for using our service."<<endl;
exit(EXIT_FAILURE);
}
}
/*****************************************************************************
* fixedPointQuad *
* This function is to find the root of the quadratic equation *
* using the fixed-point iteration approach *
*****************************************************************************/
void fixedPointQuad(double x, double y, double z)
{
unsigned int Nit;
unsigned int counter=0;
double x1;//initial value
double x2;
cout<<"\n\t\t***************** FINDING THE ROOTS ***********************\n"<<endl;
cout<<"\t\tNow, we will find the roots by using fixed point iteration."<<endl;
cout<<endl;
cout<<"\t\tEnter your initial value: ";
cin >> x1;
cout<<"\t\tEnter number of iteration: ";
cin >> Nit;
cout<<endl;
while(counter <= Nit)
{
x1 = pow((-(y/x*x1)-(z/x)),0.5);
cout<<setw(25)<<"\t\tIteration("<<counter<<") x value: "<<"\t\t"<<x1<<endl;
counter+=1;
}
}
/*****************************************************************************
* createCubicEqn *
* This function serve two choice for user to create a random cubic *
* equation or create their own cubic equation by enter the value of *
* coefficient *
*****************************************************************************/
void createCubicEqn (double m, double n, double o, double p)
{
int choiceNum;
double disc;
cout<<"\n\t\t************** CREATING CUBIC EQUATION **************\n"<<endl;
cout<<"\n\t\tNow,we will generate a cubic equation"<<endl;
cout<<" \n\t\tThe form of the equation is ax^3 + bx2 + cx + d = 0"<<endl;
cout<<endl;
cout<<"\n\t\tPlease choose any option."<<endl;
cout<<"\n\t\t1.Random Number Generator\n\n\t\t2.My Own Coefficient\n";
cout<<endl;
cout<<"\n\t\tEnter your choice>> ";
cin >> choiceNum;
cout<<endl;
if (choiceNum == 1)
{
srand (time(NULL));
m=rand()%5+1;
n=rand()%5+1;
o=-1*(rand()%50+1);
p= rand()%5+1;
cout<<"\t\tThe cubic equation is ";
cout<<m<<"x^3 + "<<n<<"x^2 + "<<o<<"x +" << p <<" = 0"<<endl;
cout<<endl;
disc= (pow(n,2)*pow(o,2))- (4*m*pow (o,3)) - (4*pow(n,3)*p) - (27*pow(m,2)*pow(p,2))+(18*m*n*o*p);
cout<<"\t\tDiscriminant value is : "<< disc<<endl;
cout<<endl;
if (disc >= 0)
{
fixedPointCube(m,n,o,p);
}
else
{
cout<<"\t\tThe discriminant must above or equal 0"<<endl;
}
}
else if(choiceNum == 2)
{
cout<<"\t\tEnter your first number: ";
cin >> m;
cout<<"\t\tEnter your second number: ";
cin >> n;
cout<<"\t\tEnter your third number: ";
cin >> o;
cout<<"\t\tEnter your fourth number: ";
cin >> p;
cout<<endl;
cout<<"\t\tThe cubic equation is ";
cout<<m<<"x^3 + "<<n<<"x^2 + "<<o<<"x +" << p <<" = 0"<<endl;
cout<<endl;
disc = (pow(n,2)*pow(o,2))- (4*m*pow (o,3)) - (4*pow(n,3)*p) - (27*pow(m,2)*pow(p,2))+(18*m*n*o*p);
cout<<"\t\tDiscriminant value is : "<< disc<<endl;
if (disc >= 0)
{
fixedPointCube(m,n,o,p);
}
else
{
cout<<"\t\tThe discriminant must above or equal 0. Thank you."<<endl;
}
}
else
{
cout<<"\t\tThank you for using our service."<<endl;
exit(EXIT_FAILURE);
}
}
/*****************************************************************************
* fixedPointCube *
* This function is to find the root of the cubic equation *
* using the fixed-point iteration approach *
*****************************************************************************/
void fixedPointCube(double f, double g , double h, double j)
{
unsigned int Nit;
unsigned int counter=0;
double x1;//initial value
cout<<"\n\t\t************** FINDING THE ROOTS **************\n"<<endl;
cout<<"\t\tNow, we will find the roots by using fixed point iteration."<<endl;
cout<<endl;
cout<<"\t\tEnter your initial value: ";
cin >> x1;
cout<<"\t\tEnter number of iteration: ";
cin >> Nit;
cout<<endl;
while(counter <= Nit)
{
x1= pow(((-g*x1*x1-h*x1-j)/f), 0.3333);
cout<<setw(25)<<"\t\tIteration("<<counter<<") x value: "<<"\t\t"<<x1<<endl;
counter+=1;
}
}