Can someone help me get rid of the errors in FindRoots function?

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!

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
#include "Polynomial.h"
#include <iostream>
#include <cmath>

using namespace std;
double tmp;
Polynomial::Polynomial() {
	x = 0;
	a = 0;
	b = 0;
	c = 0;
}

Polynomial::Polynomial(int a1, int b1, int c1) {
	
}


void Polynomial::setA(double set_a){
	set_a = tmp;
}

void Polynomial::setB(double set_b){

	set_b = tmp;
}

void Polynomial::setC(double set_c){

	set_c = tmp;
}


double Polynomial::evaluate(double x){
	double d = (pow(b, 2) - 4 * a*c);
	double positive_root = (((-b) + sqrt(d)) / (2 * a));
	double negative_root = (((-b) - sqrt(d)) / (2 * a));
	return d;
}
bool findRoots(double &r1, double &r2) {
	evaluate(x);
	if (d < 0) {
		return false;
	}
	else if (d >= 0) {
		
	}
	else (d == 0) {
		return true;
	}
	r1 = (-1 * b) / (2 * a);
	r2 = (-1 * b) / (2 * a);
	return 0;
}

void Polynomial::print(){
	cout << a << "x^2 + " << b << "x + " << c << " = 0\n";
}
if (d < 0) { Whats this mysterious "d"?

And also, why is there a return 0 in there?

Edit: Also, please copy paste your errors.
Last edited on
d is for discriminant. in the findRoots function: evaluate(x); is undefined, d is undefined, a and b are undefined
1
2
3
4
5
6
7
8
9
10
11
void Polynomial::setA(double set_a){
	set_a = tmp;
}

void Polynomial::setB(double set_b){
	set_b = tmp;
}

void Polynomial::setC(double set_c){
	set_c = tmp;
}

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.
Last edited 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!
}
Last edited on
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
Last edited on

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) {
		return false;
	}

	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
	return true;
}


Note that else statements should not have any conditionals after them.
1
2
3
4
5
6
7
        if (d < 0) {
		return false;
	}

	else (d == 0) { // error! can't have a condition after else
		return true;
	}


Edit: Fixed typos.
Last edited on
ganado: thank you so much it looks a lot better now. however b, a and c are still coming up undefined in findRoots. this is in my main

Polynomial p1(14, 19, 2);

so this is my constructor i made

Polynomial::Polynomial (int a1, int b1, int c1) {
p1.a1 = 14;
p1.b1 = 19;
p1.c1 = 2;
}
although it tells me p1 must have a class type
Topic archived. No new replies allowed.