Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this program you are to write a simple function to find the roots of quadrat

ID: 3587016 • Letter: F

Question

For this program you are to write a simple function to find the roots of quadratic equations. The function proto type should be: int roots(double, double, double, double &, double &, double &, double &); the first three parameters should be the coefficients of the quadratic polynomial. The first, the x 2 coefficient, should be non-zero, in this case roots should return 1, if the x 2 coefficient is zero roots should return 0. If the x 2 coefficient is non-zero the roots should be returned in the last four reference parameters. The forth parameter should contain the real part, and the fifth the imaginary part of one solution. The sixth the real and the seventh the imiginary part of the other root. If the x 2 coefficient is zero the last four parameters should not have their values changed. In addition there should be no output satatments in your function roots. Notice that to test your function, you will also need to write a main() function to call the roots function with the appropriate parameters. If the x 2 coefficient is zero, your program should output that the equation is not quadratic, otherwise your program should output the two roots. Also you may wish to use the sqrt() function, in this case you may need to inculde the cmath header file. 1 Solving a Quadratic Equation. If a 6= 0 the equation ax2 + bx + c = 0 has 2 solutions given by x = b b 2 4ac 2a and x = b + b 2 4ac 2a . The quantity b 2 4ac is called the discriminant. If b 2 4ac = 0 then the two separate solutions become the same and there is only one solution. If b 2 4ac < 0 then the two solutions are complex numbers. In this case both solutions have the same real part, that is b 2a and the imaginary parts are 4acb 2 2a and 4acb 2 2a . Notice also that a real number is a complex number with the imaginary part equal to zero. Test data for you program • 2x 2 + 10x + 1 = 0 • x 2 1 = 0 • x 2 2x + 1 = 0 • 5x 2 + 1x + 7 = 0 • 3x + 5 = 0

Explanation / Answer

#include <iostream>
#include <cmath>
using namespace std;

int main() {

double a, b, c, x1, x2, discriminant, realPart, imaginaryPart,realPart1,imaginaryPart1;
cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
int findroot(a,b,c,realPart,imaginaryPart,realPart1,imaginaryPart1);
return 0;
}
int findroot(double a,double b,double c,double& realpart,double& imaginary,double& realpart1,double& imaginary1)

{

discriminant = b*b - 4*a*c;

if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}

else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = (-b + sqrt(discriminant)) / (2*a);
cout << "x1 = x2 =" << x1 << endl;
}

else {
realPart = -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote