<< Chapter < Page Chapter >> Page >

root2 = (-b - squared-root(delta))/(2a)

Using these equations, we will write a C++ program to solve for the roots of a quadratic equation.

Step 1: analyze the problem

The problem requires that we accept three inputs – the coefficients a, b and c of a quadratic equation – and compute the roots of the equation using the given formulas.

Step 2: develop a solution

A first attempt at a solution is to use the user-entered values of a, band c to directly calculate a value for each of the roots. Thus, our first solution is:

Display a program purpose message.

Accept user-input values for a, b, and c.

Calculate the two roots.

Display the values of the calculated roots.

However, this solution must be refined further to account for a number of possible input conditions. For example, if a user entered a value of 0 for both a and b, the equation is neither quadratic nor linear and has no solution (this is referred to as a degenerate case).

Another possibility is that the user supplies a nonzero value for b but make a 0. In this case, the equation becomes a linear one with a single solution of –c/b. A third possibility is that the value of the term b^2 – 4ac, which is called the discriminant, is negative. Since the square root of a negative number cannot be taken, this case has no real roots. Finally, when the discriminant is 0, both roots are the same (this is referred to as the repeated roots case).

Taking into account all four of these limiting cases, a refined solution for correctly determining the roots of a quadratic equation is expressed by the following pseudocode:

Display a program purpose message.

Accept user-input values for a, b, and c.

If a = 0 and b = 0 then

Display a message saying that the equation has no solution.

Else if a = 0 then

calculate the single root equal to –c/b.

display the single root.

Else

Calculate the discriminant.

If the discriminant>0 then

Solve for both roots using the given formulas.

Display the two roots.

Else if the discriminant<0 then

Display a message that there are no real roots.

Else

Calculate the repeated root equal to –b/(2a).

Display the repeated root.

Endif.

Endif.

Notice in the pseudocode that we have used nested if-else structures. The outer if-else structure is used to validate the entered coefficients and determine that we have a valid quadratic equation. The inner if-else structure is then used to determine if the equation has two real roots (discriminant>0), two imaginary roots (discriminant<0) or repeated roots (discriminant =0).

Step 3 : code the algorithm

The equivalent C++ code corresponding to our pseudocode is listed as the following program

// This program can solve quadratic equation

#include<iostream.h>

#include<math.h>

#include<iomanip.h>

int main()

{

double a, b, c, del, x1, x2;

cout<<“This program calculates the roots of a\n”;

cout<<“ quadratic equation of the form\n”;

cout<<“ 2\n”;

cout<<“ ax + bx + c = 0\n\n”;

cout<<“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

{

del = b*b – 4.0*a*c;

if (del>0.0)

{

x1 = (-b + sqrt(del))/(2*a);

x2 = (-b – sqrt(del))/(2*a);

cout<<"The two roots are “

<<x1<<“ and “<<x2<<endl;

}

else if (del<0)

cout<<"Both roots are imaginary.\n";

else

cout<<“Both roots are equal to “<<-b/(2*a)<<endl;

}

return 0;

}

Step 4: test and correct the program

Test values should include values for a, b and c that result in two real roots, plus limiting values for a and b that result in linear equation (a = 0, b != 0), a degenerate equation ( a = 0, b = 0), and a negative and 0 discriminant. Two such test runs of the above program follow:

This program calculates the roots of a

quadratic equation of the form

ax^2 + bx + c = 0

Please enter values for a, b and c: 1 2 -35

The two real roots are 5 and –7

and

This program calculates the roots of a

quadratic equation of the form

ax^2 + bx + c = 0

Please enter values for a, b and c: 0 0 16

This equation is degenerate and has no roots.

We leave it as an exercise to create test data for the other limiting cases checked for by the program.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask