In this homework you will be writing a program that finds

The Quadratic Formula

In this homework you will be writing a program that finds the roots and vertex of the graph that corresponds to a quadratic equation given its coefficients.

Problem Statement:

This program will prompt the user for three integers and return the roots (as doubles) of the quadratic equation corresponding to those coefficients. In addition, the program will find and print out the x and y coordinates of the vertex of the parabola represented by the quadratic equation.

Learning Objectives

  • Integer arithmetic
  • Translating equations to valid C++ code
  • Use of library functions
  • Formatting numerical output (setprecision)

Quadratic Equations:

Quadratic equations are in the form y = ax2 + bx + c and form a parabola when graphed. For example, this is a graph of the quadratic equation y = x2 - 2x - 2:

For this assignment we have three points of interest: the two points where the graph intersects the xaxis (called the roots of the equation) and the vertex (the "bottom" point of the graph).


The Quadratic Formula

The quadratic formula is a way to solve for the roots of equations of the form ax2 + bx + c = 0, where a, b and c are integer coefficients. The formula is as follows:

x = -5 ± √b2 - 4 * a* c/ 2 * a

or the 2 equations

Root1 = -b + √b2 - 4 * a * c/ 2 * a

Root2 = -b - √b2 - 4 * a * c/ 2 * a

These 2 equations are heart of the program you need to write.

For example, the equation x2 + 5x + 6 = 0 has the coefficients 1, 5 and 6 (corresponding to a, b and c respectively) which can be plugged into the quadratic formula:

x = -5 ± √52 - 4 * 1* 6/2 * 1

This results in two equations (one for each of the two roots). After completing the arithmetic the two roots are:

x1 = -5 - √52 - 4 * 1 * 6/2 * 1 = -2

x2 = -5 - √52 - 4 * 1 * 6/2 * 1 = -3

Note: Root1 should be the result of the equation that contains the addition ( -b + ...) and Root2 should be the result of the equation that contains the subtraction ( -b - ...).

Note: For those of you who know this might be a problem... For this assignment, the test cases in webcat will not include any inputs that would result in imaginary roots, so you do not need to worry about them in your solution. Assume all inputs will result in real roots.


With the information above you should be able to write the code for Part 1 of the assignment. Make sure to match the format shown in the example output EXACTLY. Start by writing code to prompt the user for a, b and c. Then write the code that plugs those three values into the quadratic formulas. Finally, print the result out (the two roots of the equation).

Sample Output: Your program will be tested with several input sets, including the one shown here in RED. Your program must exactly match the words and format in this example. (Please copy and paste the text from these prompts into your code!

Please enter a, b and c: 1 -1 -2

Root1: 2.00

Root2: -1.00

Please enter a, b and c: 6 11 -35

Root1: 1.67

Root2: -3.50

Set Precision:

If you look at the output you'll notice that all of the answers have two decimal places. In your solution, the output should also always be formatted to exactly two decimal places. This can be achieved by using the function setprecision(). If you have not already done so, add this to your solution.


Finding the vertex of the parabola

To find the vertex of a parabola represented by a quadratic equation there are two steps: find the xcoordinate and then the y-coordinate.

Finding the x-coordinate of a vertex

There is a simple formula for finding the x-coordinate of a parabola given the coefficents of the quadratic equation that describes it:

x = -b/2 * a

Plug in the "b" and the "a" that you used before to get the Root1 and Root2 answers

Finding the y-coordinate of a vertex

To get the y-coordinate of the parabola simply plug in the answer you got for the x-coordinate to the quadratic equation. For example, using the same a, b and c as up above (1,5 and 6). The x-coordinate would be:

x = -5/2 * 1 = 2.5

Remember that the original equation was x2 + 5x + 6. So when we plug in the x-coordinate to the equation we get:

y = (x)2 + 5 * (x) + 6

y = (-2.5)2 + 5 * (-2.5) + 6 = -0.25


Using the information given above you should now be able to write the code for part 2. Start by finding the x coordinate and storing it in a variable (should be a double). Then use that variable to find the y coordinate. Finally print out the x and y coordinates in the format shown below. Here is an example of a full run of your program after part 2 has been completed:

Please enter a, b and c: 2 3 -4

Root1: 0.85

Root2: -2.35

Vertex Coordinates (x, y): (-0.75, -5.13)

Solution Preview :

Prepared by a verified Expert
Computer Engineering: In this homework you will be writing a program that finds
Reference No:- TGS01119510

Now Priced at $80 (50% Discount)

Recommended (98%)

Rated (4.3/5)