Can someone help me with this polynomial function code?

I'm confused on the findroots part of this assignment and the evaluate function. Tmp is user inputted. The findRoots function should return true or false, depending on whether or not there are real roots(if b2-4ac is positive). The function accepts two double values using pass by reference. If there are roots, then those two parameters are set within the function. Note, if b2-4ac is zero, both roots are the same – and that is OK (you just set them both).Where the two values found can be called root1 and root2. If the value of b2-4ac is negative there are no real roots (the function never equals 0). So I know I need an if statement but am confused on how I would do this? Also the evaluate function. I'm not sure if it makes sense. The fun ction 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 I wrote an evaluate function but does it make sense? Any help you could give me is greatly 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
 #include "Polynomial.h"
#include <iostream>
#include <cmath>

using namespace std;

double tmp;

Polynomial::Polynomial() {

	x, a, b, c = 0;

}
double findRoots(double &r1, double &r2) {

}
//double Polynomial::Polynomial(a1, b1, c1){



//}

double Polynomial::evaluate(double x){
	
	double ans = a * pow(x, 2) + b * x + c;
	return ans;
}


double Polynomial::getA(double a) {
	return tmp;
}
double Polynomial::setA(double a){
	a = tmp;
	return 0;
}

double Polynomial::setB(double b){

	b = tmp;
	return 0;
}

double Polynomial::setC(double c){

	c = tmp;
	return 0;
}

void Polynomial::print(){

}
Last edited on
A polynomial class should keep track of the coefficients, not the mathematical variables. That is, given Ax²+Bx+C, your class should store A, B, and C, but not x.

(That said, I have not seen the class definition given you by your professor. It could be that he wants you to keep an x in there somewhere.)


Get rid of line 7. The point of a getter or setter is to manipulate values in a class object. (Not global variables. You don't need any global variables.)

1
2
3
4
5
6
7
8
class Polynomial
{
private:
  double a, b, c;
public:
  double getA() const { return a; }
  void setA( double new_a ) { a = new_a; }
};
Make sense?


Line 11 doesn't do what you think it does. Set each value to zero individually.


Line 23 expects you to evaluate the discriminant (the part under the radical). Remember, the quadratic formula finds the center (axis of reflection) of a curve, and then the offsets of the intersections with the X-axis. It is:

  (center) ± (offset)

   -1*b         √d
   -----   ±   -----
    2*a         2*a

where d (the discriminant) is:

    (b*b - 4*a*c)

If √d is zero, then the offset is zero, meaning the curve only touches the X-axis at one point.
If √d is a real, nonzero number (because d > 0), then there are exactly two points where the curve crosses the X-axis.
If √d is a complex number (because d < 0) then there is no point where the X-axis intersects the curve.

So your evaluate() function should compute and return the discriminant.


Line 14 should look like bool findRoots(double x, double &r1, double &r2).
(Again, I could be wrong about how your professor wants you to keep that x.)

It should first find d by calling the evaluate() function with your x.
If d < 0, then there are no real solutions. Return false.
Otherwise d >= 0.
Set both r1 and r2 equal to (-1*b)/(2*a).
If d == 0, you're done. Return true.
Otherwise, you also need to calculate the offset part: k = √(d)/(2*a). And then add the offset to r2 and subtract it from r1.
Return true.


When you print() your polynomial, just output the standard equation but with a,b,and c in the proper spots:

cout << a << "x^2 + " << b << "x + " << c << " = 0\n";

Hope this helps.
duoas: thanks for the detailed answer! i changed a bunch of things like you said but am getting tons of errors that things in findRoots function are undefined. also i think i need the universal variable tmp. i dont know where to declare it otherwise. here's what i have

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";
}
Last edited on
Topic archived. No new replies allowed.