Question

In: Computer Science

Find Roots of a Quadratic Equation Step 1: Analyze the Problem Accept three coefficients a, b,...

Find Roots of a Quadratic Equation

  • Step 1: Analyze the Problem
    • Accept three coefficients a, b, and c (all of data type double) of a quadratic equation: ax2+bx+c=0
    • Output real roots (double) of the equation

  • Step 2: Develop a Solution
    • Check for degenerate case (user enters 0 for both a and b), no solution
    • Check if the equation is linear (user enters 0 for a), x = -c/b
    • Calculate the discriminant = b2-4ac (obviously, the data type will be double)
    • Check if discriminant > 0, two real roots
    • Check if discriminant < 0, two complex roots
    • Check if discriminant = 0, repeated roots, x = -b/(2a)

Hand calculate and write down the results for the following different cases:

  • Real roots case: a = 1, b = 2, c = -35 -> x = 5, -7
  • Degenerate roots case: a = b = 0, c = 16
  • Repeated roots case: a = 1, b = -14, c = 49 -> x = ?, ?
  • Imaginary roots case: a = 2, b = -14, c = 25
  • Linear equation case: a = 0, b = -14, c = 28 -> x = ?
  • Imaginary roots case: a = 2, b = 0, c = 25 -> x = ?, ?

  • Step 3: Code the Solution – Program included in the next page.

  • Step 4: Test and Correct the Program for at least the following 5 cases
    • Real roots case: a = 1, b = 2, c = -35
    • Degenerate roots case: a = b = 0, c = 16
    • Repeated roots case: a = 1, b = -14, c = 49
    • Imaginary roots case: a = 2, b = -14, c = 25
    • Linear equation case: a = 0, b = -14, c = 28
    • Imaginary roots case: a = 2, b = 0, c = 25

(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:

Solutions

Expert Solution

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

Related Solutions

Write a program to input the coefficients of a quadratic equation and solve roots for all...
Write a program to input the coefficients of a quadratic equation and solve roots for all cases (including complex roots). VBA. x=(b ^2) - (4ac) I have it for 2 complex and 2 real and repeated. Is that all cases?
1). Consider the quadratic equation x^2+ 100 x + 1 = 0 (i) Compute approximate roots...
1). Consider the quadratic equation x^2+ 100 x + 1 = 0 (i) Compute approximate roots by solving x^2 -100 x = 0 (ii) Use the quadratic formula to compute the roots of equation (iii) Repeat the computation of the roots but use 3 digit precision. (iv) Compute the relative absolute errors in the two 3 digit precision root approximations in (iii). (v) With x1 =1/2a (-b + sqrt b^2 - 4ac and x2 = 1/2a (-b + sqrt b^2...
use Java The two roots of a quadratic equation ax^2 + bx + c = 0...
use Java The two roots of a quadratic equation ax^2 + bx + c = 0 can be obtained using the following formula: r1 = (-b + sqrt(b^2 - 4ac)) / (2a) and r2 = (-b - sqrt(b^2 - 4ac)) / (2a) b^2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no...
1. The equation 8?2 − 3?? = 0 has three roots. Find: (i) The first root,...
1. The equation 8?2 − 3?? = 0 has three roots. Find: (i) The first root, which is between -1 and 0, by the bisection method; (ii) The second root, which is between 1 and 2, by Newton’s method; (iii) The third root, which is between 3 and 4, by both the bisection method and Newton’s method. All the answers should be correct to 2 decimal places.
1. Solve quadratic equation Ax^2+Bx+C=0 using the quadratic formula x = (-B+ and - sqrt(B^2-4ac)) /...
1. Solve quadratic equation Ax^2+Bx+C=0 using the quadratic formula x = (-B+ and - sqrt(B^2-4ac)) / 2a) and output the two solution with clear explanation could you please do it in MATLAB
1) Solve the given quadratic equation by using Completing the Square procedure and by Quadratic formula...
1) Solve the given quadratic equation by using Completing the Square procedure and by Quadratic formula ( you must do it both ways). Show all steps for each method and put your answer in simplest radical form possible. 2) Which part of the Quadratic Formula can help you to find the Nature of the roots for the Quadratic Equation. Explain how you can find the nature of the roots and provide one Example for each possible case with solution.
Roots of a complex number Find all the solutions to the equation (a) z^ 4 −...
Roots of a complex number Find all the solutions to the equation (a) z^ 4 − 1 = 0 (b) z ^5 + 2^5 i = 0 (c) z^ 6 − 16z^ 3 + 128 = 0
Find all roots of the equation z^5=i, i.e. find the five values of i^1/5 and show...
Find all roots of the equation z^5=i, i.e. find the five values of i^1/5 and show them on an Argand diagram. Show all working
Write a program usingif-elseif-else statements to calculate the real roots of a quadratic equation ax^2+bx+c=0
Write a program usingif-elseif-else statements to calculate the real roots of a quadratic equation ax^2+bx+c=0
Use method of undetermined coefficients to find a particular solution of the differential equation ?′′ +...
Use method of undetermined coefficients to find a particular solution of the differential equation ?′′ + 9? = cos3? + 2. Check that the obtained particular solution satisfies the differential equation.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT