In the findRoots function, everything is coming up undefined variable errors. What i tried to do is evaluate (that function is meant to find discriminant of quadratic equation) and then I want to take values from there into my findRoots function to find the roots of the equation. I can't figure out how to get rid of these errors and also if I'm calling my other function inside findRoots correctly. Another thing I'm confused about is the constructor Polynomial::Polynomial. I know I need it for something but I don't really understand what. Any help you could give me is appreciated!
The variable on the left-hand side is what is assigned the value. You're setting the value of your local variable set_a/b/c, which changes nothing.
Also, I don't see what the purpose of your tmp variable is.
The point of a constructor is to conveniently set all values and initial conditions of your object at creation. I would suggest you put
1 2 3
a = a1;
b = b1;
c = c1;
inside your second (a, b, c) constructor.
_________
Furthermore, the purpose of your evaluate functions is unclear, too. Why does it take in the variable x if it doesn't use it? Why are you returning the discriminate?
_________
Also, you didn't actually copy your error messages, and we don't even know how you're using your class so you're making it hard for us to tell what's going on.
Ganado: the tmp variable is the user input. they are inputting the values for a, b and c. The evaluate function should return a double. There should be a single parameter, called x. When called, the function computes Ax2 + Bx + C using the polynomial’s A, B, and C values and returns the result. So for that one I wasn't sure what the point of x was. Is it x in the equation? if so what is x? and for that do i just write the formula in there? and I'm returning the discriminant so i can use it in findRoots...or so I thought
For your evaluate function: I think you are misinterpreting it wrong.
If I understand, you should simply be plugging in x for the equation Ax2 + Bx + C.
ex: if the equation represented is 4x2 + 2x + 1, evaluate(2) would return:
4(2)2 + 2(2) + 1 ==> 21
In your findroots function is where you should be determining the roots using the quadratic equation.
Note that a local variable in one function does not exist inside another function - this is what "scope" is.
1 2 3 4 5 6 7 8
void faa()
{
double d = 3.2;
}
double foo(double x)
{
return x + d; // error: d does not exist in this functions scope!
}
ganado: oh wow it looks like i was misinterpreting it. so i would just write the equation with the x's plugged in. my problem with findRoots is that I can't put the variables in the parameters. the parameters should only be r1 and r2. so I'm really stuck on how to get this function to work
Here's an example of how, by the looks of it, you should be calling your findRoots function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int main()
{
Polynomial equation(2, 3, -10);
// set values of equation.a, equation.b, equation.c
// for example:
equation.a = 2;
equation.b = 3;
equation.c = -10;
// The above should be in your 3-arg constructor, though!
double root1;
double root2;
bool success = equation.findRoots(root1, root2);
if (success)
cout << "The roots are: " << root1 << " " << root2 << endl;
else
cout << "Error: unreal roots not supported!" << endl;
}
There's no need to call evaluate in your findRoots function.
Here's a better findRoots function I made by reorganizing your code:
1 2 3 4 5 6 7 8 9 10 11 12
bool findRoots(double &r1, double &r2) {
double d; // local variable;
d = b*b - 4 * a * c;
if (d < 0) {
returnfalse;
}
r1 = (((-b) + sqrt(d)) / (2.0 * a)); // r1 is a reference to root1 in main
r2 = (((-b) - sqrt(d)) / (2.0 * a)); // r2 is a reference to root2 in main
returntrue;
}
Note that else statements should not have any conditionals after them.
1 2 3 4 5 6 7
if (d < 0) {
returnfalse;
}
else (d == 0) { // error! can't have a condition after else
returntrue;
}