Question
complete what is missing for below code in c++ and disply it class polynomial { double a; double b; double c; double d; complex x1;
complete what is missing for below code in c++ and disply it
class polynomial
{
double a;
double b;
double c;
double d;
complex x1;
complex x2;
public:
polynomial(double v1, double v2, double v3)
{
a = v1;
b = v2;
c = v3;
d = discriminant();
}
double discriminant()
{
double d = b*b - 4 * a*c;
return d;
}
void find_roots();
void display();
};
void polynomial::find_roots()
{
if (a == 0)
{
if (b != 0 && c != 0)
x1.set_complex(-c / b, 0);
}
else if (d == 0)
{
x1.set_complex(-b / (2 * a), 0);
}
else if (d > 0)
{
x1.set_complex((-b + sqrt(d)) / (2 * a), 0);
x2.set_complex((-b - sqrt(d)) / (2 * a), 0);
}
else /* if (d < 0) */
{
x1.set_complex(-b / (2 * a), sqrt(abs(d)) / (2 * a));
x2.set_complex(-b / (2 * a), -sqrt(abs(d)) / (2 * a));
}
}
void polynomial::display() { cout << "The roots of the equation are: "; x1.display_complex(); cout << " and "; x2.display_complex(); cout << endl; }
int main() { polynomial p(1, -3, 4); p.find_roots(); p.display(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started