In: Computer Science
Find Roots of a Quadratic Equation
Hand calculate and write down the results for the following different cases:
(a) Compile and run the following program using test conditions outlined above. [5+2*5 points]
#include <iostream>
#include <cmath>
using namespace std;
// this program solves for the roots of a quadratic equation
int main()
{
double a, b, c, disc, root1, root2;
cout << "This program calculates the roots of a\n";
cout << "\tquadratic equation of the form\n";
cout << "\t\t 2\n";
cout << "\t\tax + bx + c = 0\n\n";
cout << "Please enter values for a, b, and c: ";
cin >> a >> b >> c;
if ( a == 0.0 && b == 0.0)
cout << "The equation is degenerate and has no roots.\n";
else if (a == 0.0)
cout << "The equation has the single root x = " << -c/b << endl;
else {
disc = pow(b,2.0) - 4 * a * c; // calculate discriminant
if (disc > 0.0) {
disc = sqrt(disc);
root1 = (-b + disc) / (2 * a);
root2 = (-b - disc) / (2 * a);
cout << "The two real roots are " << root1 << " and " << root2
<< endl;
}
else if (disc < 0.0)
cout << "Both roots are imaginary.\n";
else
cout << "Both roots are equal to " << -b / (2 * a) << endl;
}
return 0;
}
Copy and paste your Console Debug window outputs here
(b) Modify the program to output complex roots in a format like “4.0 + 3.0i”. [5 + 5 points]
HINT: For disc < 0.0, you may separately compute real and imaginary parts prior to displaying them.
Copy and paste your modified code here:
Copy and paste Console Debug window output for the complex conjugate roots here:
Solution :
Console output for all the 6 cases :
a)
1.
This program calculates the roots of a
quadratic equation of the form
2
ax + bx + c = 0
Please enter values for a, b, and c: 1 2 -35
The two real roots are 5 and -7
2.
This program calculates the roots of a
quadratic equation of the form
2
ax + bx + c = 0
Please enter values for a, b, and c: 0 0 16
The equation is degenerate and has no roots.
3.
This program calculates the roots of a
quadratic equation of the form
2
ax + bx + c = 0
Please enter values for a, b, and c: 1 -14 49
Both roots are equal to 7
4.
This program calculates the roots of a
quadratic equation of the form
2
ax + bx + c = 0
Please enter values for a, b, and c: 2 -14 25
Both roots are imaginary.
5.
This program calculates the roots of a
quadratic equation of the form
2
ax + bx + c = 0
Please enter values for a, b, and c: 0 -14 28
The equation has the single root x = 2
6.
/tmp/AX1BKNaHpO.o
This program calculates the roots of a
quadratic equation of the form
2
ax + bx + c = 0
Please enter values for a, b, and c: 2 0 25
Both roots are imaginary.
b) Following is the program for to output imaginary case as well :
#include <iostream>
#include <cmath>
using namespace std;
// this program solves for the roots of a quadratic equation
int main()
{
double a, b, c, disc, root1, root2;
cout << "This program calculates the roots of a\n";
cout << "\tquadratic equation of the form\n";
cout << "\t\t 2\n";
cout << "\t\tax + bx + c = 0\n\n";
cout << "Please enter values for a, b, and c: ";
cin >> a >> b >> c;
if ( a == 0.0 && b == 0.0)
cout << "The equation is degenerate and has no roots.\n";
else if (a == 0.0)
cout << "The equation has the single root x = " << -c/b << endl;
else {
disc = pow(b,2.0) - 4 * a * c; // calculate discriminant
if (disc > 0.0) {
disc = sqrt(disc);
root1 = (-b + disc) / (2 * a);
root2 = (-b - disc) / (2 * a);
cout << "The two real roots are " << root1 << " and " << root2
<< endl;
}
else if (disc < 0.0)
{
double x,y;
x=-b/(2*a);
y= sqrt(-disc)/(2*a);
cout << "The two complex roots are "<<x<<"+"<<y<<"i"<<endl;
}
else
cout << "Both roots are equal to " << -b / (2 * a) << endl;
}
return 0;
}
Code demo :
Output :
Console copy-pasted output :
This program calculates the roots of a
quadratic equation of the form
2
ax + bx + c = 0
Please enter values for a, b, and c: 2 -14 25
The two complex roots are 3.5+0.5i