Objective

Practically, you do not find a program that only follows a sequence of statements in order. Whilst, most programs contain one or more blocks of statements where they can be selected or executed frequently. This kind of activities is called flow control. In C++, there are many coding structures allowed you to develop a particular flow in the program. This laboratory requires a special flow control, called conditional statements or if-else statements, to solve a quadratic equation.

Quadratic Equation

A quadratic equation is an equation defined as a polynomial of degree two, it could be represented by:

ax2 + bx + c = 0,

where a ≠ 0, b and c are called coefficient of the equation and x is the unknown value we would like to find. To solve the quadratic equation, the first step is to use the following equation to calculate the discriminant (note that Δ cannot be used as an identifier in C++):

Δ = b2 - 4ac.

Then, the (real) solution of the quadratic equation is given by:

  • if Δ > 0, then x = or x =
  • if Δ = 0, then x =
  • if Δ < 0, then no real solution.

Please read the detail derivation in Appendix, if you are interested.

Review of solving quadratic equation

To derive the given method, we can simply use the “completing square” method.

The reason of performing so many “strange things” in the derivation is that we want to reach the key factorization in the step (1). Now, when the right-hand-side is non-negative, we can take the square root on both sides; otherwise, we can’t do this and hence no real solution exists. When the right-hand-side is non-negative, then

The plus/minus operator in (3) means the value is either positive or negative, since (-x)2 = x2, for any x. Therefore, in the given method, we consider three different cases, where the first two cases can be combined, according to the sign of the right-hand-side (Δ = b2 - 4ac) in (2).

Requirements

You are required to write a program so that it calculates the solution of a user specified quadratic equation. The program should first welcome the user by printing a welcome banner:

Welcome to use Quadratic Calculator

Starting on the next line, the program should prompt the user to input the coefficients of the quadratic equation: a, b and c. It will prompt the user by printing:

Please enter the value of a:  
 
Please enter the value of b:  
 
Please enter the value of c:

The user will input the corresponding value after the space (after the colon, at the end of each line). Notice the number (input by the user) can be assumed to be correct (this lab does not require input validation). Therefore, you can assume that the user won’t input some invalid numbers, like “a”, “abc”, “-12-0.-2” etc. However, you must check whether the input a is equal to zero or not. If it is zero, then you must print

Invalid input

and exit the program immediately. Otherwise, The program should print out the corresponding (real) solutions according to the above given method: x = 1 x = 2


  1. If there are no real roots (case 3), you should print the line “There is no real solution.”
  2. There is a newline after printed out the final solution of x.

As a final reminder, you can simply use the cout statement to output the solution of type double.

Handling linear equation

A linear equation is another simple-to-solve equation. Specifically, a = 0 in the quadratic equation, but b must be non-zero (otherwise, print “Invalid input” as above). When a = 0, the quadratic equation degenerates to bx + c = 0, which is trivial to solve. The requirement of the bonus is to process them correctly as well. Despite having only one solution, the solution is not repeated and therefore the word “[repeated]” should not be used. Here are two more sample executions for this bonus part:

Welcome to use Quadratic Calculator  
 
Please enter the value of a: 0  
 
Please enter the value of b: 0  
 
Please enter the value of c: 1  
 
Invalid input  

Welcome to use Quadratic Calculator  
 
Please enter the value of a: 0  
 
Please enter the value of b: -2  
 
Please enter the value of c: 1  
 
x = 0.5 

Sample Executions

In this section, we show several sample executions so that you can follow and write the program. Please make sure your program run correctly in general cases, not only in the following cases.

Distinct Solutions

Welcome to use Quadratic Calculator  
 
Please enter the value of a: 1  
 
Please enter the value of b: 3.5  
 
Please enter the value of c: 2  
 
x = -2.7808  
 
x = -0.71922  

Note that you can output the two roots in any order.


Repeated Solution

Welcome to use Quadratic Calculator  
 
Please enter the value of a: 1  
 
Please enter the value of b: 2  
 
Please enter the value of c: 1  
 
x = -1 [repeated]  

No Solution

Welcome to use Quadratic Calculator  
 
Please enter the value of a: 1  
 
Please enter the value of b: 0  
 
Please enter the value of c: 1  
 
There is no real solution  

Input Validation

Welcome to use Quadratic Calculator  
 
Please enter the value of a: 0  
 
Please enter the value of b: 0  
 
Please enter the value of c: 1  
 
Invalid input   

Sample Solution

As for your undestanding, we provide you a sample program so that you can download it and play around before you start coding.