\"6.32 Algebra: solve quadratic equations) The two roots of a quadratic equation
ID: 3598848 • Letter: #
Question
"6.32 Algebra: solve quadratic equations) The two roots of a quadratic equation ax2 + bx + x = 0 can be obtained using the following formula: and r2 = 2a 2a Write a function with the following header void solveQuadraticEquation (double a, double b, double c, double& discriminant, double& rl, double& r2) b2 - 4ac is called the discriminant of the quadratic equation. If the discriminant is less than 0, the equation has no roots. In this case, ignore the value in r1 and r2. Write a test program that prompts the user to enter values for a, b, and c and displays the result based on the discriminant. If the discriminant is greater than or equal to 0, display the two roots. If the discriminant is equal to O, display the one root. Otherwise, display the equation has no roots" . See Programming Exercise 3.1 for sample runs.Explanation / Answer
//solution
#include<iostream>
using namespace std;
void SolveQuadraticEquation(double a, double b, double c, double &discriminant, double& r1, double& r2);
int main()
{
double a, b, c, discriminant, r1, r2;
cout << "Enter value for a : ";
cin >> a;
cout << "Enter value for b : ";
cin >> b;
cout << "Enter value for c : ";
cin >> c;
//call function
SolveQuadraticEquation(a, b, c, discriminant, r1, r2);
cout << "Roots of quadratic equation are r1 = " << r1 << " and r2 = " << r2 << endl;
}
void SolveQuadraticEquation(double a, double b, double c, double &discriminant, double& r1, double& r2)
{
//calclulate discriminant
discriminant = (b*b - 4 * a*c);
if (discriminant < 0)
{
cout << "discriminant is less than zero hence equation has no roots" << endl;
exit(0);
}
discriminant = sqrt(discriminant);
r1 = (-b + discriminant) / 2 * a;
r2 = (-b - discriminant) / 2 * a;
return;
}
-----------------------------------------------------------------------------------------------
//output1
Enter value for a : 1
Enter value for b : 3
Enter value for c : -4
Roots of quadratic equation are r1 = 1 and r2 = -4
//output2
Enter value for a : 1
Enter value for b : -18
Enter value for c : 81
Roots of quadratic equation are r1 = 9 and r2 = 9
//output3
Enter value for a : 3
Enter value for b : -10
Enter value for c : 3
Roots of quadratic equation are r1 = 27 and r2 = 3
//output4
Enter value for a : 1
Enter value for b : 1
Enter value for c : 1
discriminant is less than zero hence equation has no roots
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.