<< 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.

Questions & Answers

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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