-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
233 lines (179 loc) · 5.56 KB
/
main.cpp
File metadata and controls
233 lines (179 loc) · 5.56 KB
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
/*
INVESTIGATING QUADRATIC FUNCTIONS
by Anna Cristina Karingal
CSCI 135 Assignment 1
Takes a quadratic equation given by the user and:
1. Evaluates the equation at any given point.
2. Finds the root(s) of the equation, if any.
3. Finds the point(s) of intersection between the equation and another quadratic equation.
4. Finds the minimum/maximum of the equation, if any.
Last edited: February 27, 2014
*/
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
//Initialization and command functions
void set(float &a, float &b, float &c);
string command ();
//Math Functions
float eval(float a, float b, float c, float x);
float discrim(float a, float b, float c);
void roots(float a, float b, float c, float &r1, float &r2);
void intersect (float a, float b, float c);
void extreme (float a, float b, float c);
/* MAIN FUNCTION */
int main(){
float a, b, c; //coefficients of f(x)
set(a, b, c); //initial setting of coefficients
string cmd;
cmd = command(); //receives initial command from user
do {
if (cmd =="set") {
set(a, b, c);
cmd = command();
}
else if (cmd =="eval"){
float x;
cout << "Please enter the value of x: ";
cin >> x;
cout << "The value of the function at x = " << x << " is " << eval (a,b,c,x) << ".\n";
cmd = command();
}
else if (cmd =="roots"){
float root1, root2;
roots(a,b,c, root1, root2);
float d = discrim(a,b,c);
if (a==0){
cout << "Your function is linear. Its x-intercept is at x = " << root1 << ".\n";
}
else {
if (d>0){
cout << "The function has two real roots.\n";
cout << "The roots of your function are at x = " << root1 << " and x = " << root2 << ".\n";
}
else if (d==0){
cout << "The function has only one real root.\n";
cout << "The root of your function is at x = " << root1 << endl;
}
else {
cout << "Your function has no real roots.\n";
}
}
cmd = command();
}
else if (cmd =="intersect"){
intersect(a,b,c);
cmd = command();
}
else if (cmd =="extreme"){
extreme(a,b,c);
cmd = command();
}
else {
//In case of unacceptable command
cout << "Sorry, I did not understand your command.\nPlease enter a new command.\n" << endl;
cmd = command();
}
}
while (cmd != "exit");
return 0;
}
/*IMPLEMENTATION OF FUNCTIONS */
//Sets coefficients of the quadratic equation f(x)
void set(float &a, float &b, float &c){
cout << "Enter the coefficients a, b, and c: ";
cin >> a >> b >> c;
cout << "f(x) = " << a << "(x^2) + " << b << "x + " << c << ".\n";
}
//Gets command from user
string command(){
string cmd;
cout << "\nCOMMAND: ";
cin >> cmd;
return cmd;
}
//Evaluates quadratic equation f(x) at a point x
float eval(float a, float b, float c, float x){
float y, xsq;
xsq = x*x;
y = (a*xsq) + (b*x) + c;
return y;
}
// Calcuates the value of the discriminant
float discrim(float a, float b, float c){
float d;
d = (b*b)-(4*a*c);
return d;
}
//Evaluates the roots of the quadratic equation f(x)
void roots(float a, float b, float c, float &r1, float &r2){
r1 = 0;
r2 = 0;
if (a == 0){ //if the equation f(x) is linear
r1 = (-c)/b;
}
else { //if the equation f(x) is not linear, i.e. a != 0
float d;
d = discrim(a,b,c);
//Determine whether the function f(x) has 0, 1, or 2 roots using the discriminant
if (d > 0){
//Finds the two roots of f(x)
r1 = (-b + sqrt(d))/(2*a);
r2 = (-b - sqrt(d))/(2*a);
}
else if (d == 0){
//Finds the single root of f(x)
r1 = (-b)/(2*a);
}
else { }
}
}
//Evaluates intersection between the supplied quadratic equation f(x) & another quadratic equation g(x)
void intersect (float a, float b, float c){
//Gets the coefficient of the second polynomial g(x)
float a2,b2,c2;
cout << "Enter the coefficients a, b and c of the second function: ";
cin >> a2 >> b2 >> c2;
//Finds the coefficients of h(x), the result of the difference between f(x) and g(x)
float a3,b3,c3;
a3 = a-a2;
b3 = b-b2;
c3 = c-c2;
float d;
d = discrim(a3,b3,c3); //Finds the discriminant of h(x)
//Finds the roots of h(x) using the discriminant
//The roots of h(x) are the same points where f(x) and g(x) intersect
float x1, x2;
roots(a3,b3,c3, x1, x2);
if (a3==0){
cout << "The two functions intersect at x = " << x1 << ".\n";
}
else {
if (d>0){
cout << "The two functions intersect at x = " << x1 << " and x = " << x2 << ".\n";
}
else if (d==0){
cout << "The two functions intersect at" << x1 << endl;
}
else {
cout << "The two functions do not intersect.\n";
}
}
}
void extreme (float a, float b, float c){
float v;
v = (-b)/(2*a); //Calculates the value of x at the vertex
//Determines whether vertex is a minimum or maximum depending on the value of a
if (a > 0){
cout << "The function has a minimum.\n";
cout << "Its located at x = " << v << ". The value of the function at this point is " << eval(a,b,c,v) << ".\n";
}
else if (a < 0){
cout << "The function has a maximum.\n";
cout << "Its located at x = " << v << ". The value of the function at this point is " << eval(a,b,c,v) << ".\n";
}
else {
cout << "There is no extreme value for this function.\n";
}
}