# 2.3 Selection statements  (Page 5/5)

 Page 5 / 5

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 can chip be made from sand
Eke Reply
is this allso about nanoscale material
Almas
are nano particles real
Missy Reply
yeah
Joseph
Hello, if I study Physics teacher in bachelor, can I study Nanotechnology in master?
Lale Reply
no can't
Lohitha
where is the latest information on a no technology how can I find it
William
currently
William
where we get a research paper on Nano chemistry....?
Maira Reply
nanopartical of organic/inorganic / physical chemistry , pdf / thesis / review
Ali
what are the products of Nano chemistry?
Maira Reply
There are lots of products of nano chemistry... Like nano coatings.....carbon fiber.. And lots of others..
learn
Even nanotechnology is pretty much all about chemistry... Its the chemistry on quantum or atomic level
learn
Google
da
no nanotechnology is also a part of physics and maths it requires angle formulas and some pressure regarding concepts
Bhagvanji
hey
Giriraj
Preparation and Applications of Nanomaterial for Drug Delivery
Hafiz Reply
revolt
da
Application of nanotechnology in medicine
has a lot of application modern world
Kamaluddeen
yes
narayan
what is variations in raman spectra for nanomaterials
Jyoti Reply
ya I also want to know the raman spectra
Bhagvanji
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
Damian
yes that's correct
Professor
I think
Professor
Nasa has use it in the 60's, copper as water purification in the moon travel.
Alexandre
nanocopper obvius
Alexandre
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
Rafiq
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
Damian
How we are making nano material?
LITNING Reply
what is a peer
LITNING Reply
What is meant by 'nano scale'?
LITNING Reply
What is STMs full form?
LITNING
scanning tunneling microscope
Sahil
how nano science is used for hydrophobicity
Santosh
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
Rafiq
what is differents between GO and RGO?
Mahi
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
Rafiq
if virus is killing to make ARTIFICIAL DNA OF GRAPHENE FOR KILLED THE VIRUS .THIS IS OUR ASSUMPTION
Anam
analytical skills graphene is prepared to kill any type viruses .
Anam
Any one who tell me about Preparation and application of Nanomaterial for drug Delivery
Hafiz
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
Bob
The nanotechnology is as new science, to scale nanometric
brayan
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Damian
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

### Read also:

#### Get Jobilize Job Search Mobile App in your pocket 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? By Megan Earhart By Stephen Voron By OpenStax By Stephen Voron By Courntey Hub By Tess Armstrong By Janet Forrester By Cath Yu By Richley Crapo By OpenStax