CSCI 240 Spring 2024

Program 3
Decision Statements
(100 points)


Due: Friday, February 9 on Blackboard by 11:59PM

Overview

For this program, continue the parabola program from Program 2. This version of the program will still calculate and display the coordinates for the vertex. The new part is to determine (and display) the concavity of the parabola, and if the parabola has real roots and what they are if they exist.

The program will also do a little bit of error-checking along with the calculations.

Basic Program Logic

For this program, ask the user for the a-coefficient of the parabola, which should still be an integer value. The a-coefficient needs to be checked for validity. If the value the user enters is 0, display a message that the a-coefficient must be non-zero and ask the user to re-enter the value. This only needs to be done one time (i.e. we'll assume the user "gets it right" on the second try).

Ask the user for the b and c-coefficients. The two values are also still integer values. Any value is allowed for the b and c-coefficients.

The output for the program should be displayed in a tabular format similar to Program 2. This is a place where the table can start to be displayed. Display a title for the table and the three coefficients that have been entered by the user. Note: the display of the entire table can be saved until the end of the program if desired. It is simply a suggestion to display it in parts.

Calculate (and display) the x and Y coordinates of the vertex of the parabola.

Use the a-coefficient to determine the concavity of the parabola. If the a-coefficient is positive, then the parabola opens upward. If it's negative, then the parabola opens downward. Display the concavity (the direction that the parabola opens) if the table is being displayed in parts.

Now determine the roots of the parabola. The roots are the places where the parabola intersects the x-axis. If the parabola is entirely above or below the x-axis, there are no real roots. If it just touches the x-axis, it has one root with a multiplicity of 2. Otherwise, it intersects the x-axis at 2 points.

To determine which case holds true, calculate the discriminant and check its value. If the discriminant is positive, there are two real roots. If it's 0, there is one real root with multiplicity 2 (i.e. either root formula from below can be used to calculate the root since the result will be the same with either formula). If it's negative, there are no real roots.

Use the following equations to calculate the discriminant and roots. All the values should be float/double values:

discriminant = b2 - 4 * a * c

root 1 = ( -b + sqrt( discriminant )) / ( 2 * a )

root 2 = ( -b - sqrt( discriminant )) / ( 2 * a )

Note: see Program Requirement #2 for information about sqrt.

For the roots, display the number of roots that the parabola has and, if they exist, the values.

As mentioned earlier, all the values should be displayed in a neat tabular format with the decimal point of all the numeric values lined up and exactly 3 digits after the decimal points.

Program Requirements

  1. At the top of the C++ source code, include a documentation box that resembles the one from programs 1 and 2. This will be a part of every program that is submitted during the semester and this will be the last reminder in the program write-ups.

  2. sqrt is the name of a function that calculates the square root of a number. It is part of the cmath library. Make sure to add #include <cmath> at the top of the code so that the sqrt function can be used. To calculate the square root of a value:

  3. sqrt( value )

  4. All the values in the table should be displayed with exactly 3 digits after the decimal point, including zeroes.

  5. Use type int for the values read in from the user. Use meaningful variable names

  6. Make sure and test the program with values other than the ones supplied in the sample output.

  7. Hand in a copy of the source code (CPP file) using Blackboard.

Output:

A few runs of the program might resemble the following:

Run 1:

Enter the a coefficient (non-zero value): 0
Error: the a-coefficient MUST be non-zero. Try again: 1
Enter the b coefficient: 2
Enter the c coefficient: -1


----------------------------------------
  Quadratic Equation Analyzer
----------------------------------------
a Coefficient                          1
b Coefficient                          2
c Coefficient                         -1
----------------------------------------
Vertex
X Coordinate                      -1.000
Y Coordinate                      -2.000
----------------------------------------
The parabola opens UPWARD
----------------------------------------
The parabola has TWO roots
Root 1 - X Coordinate              0.414
Root 2 - X Coordinate             -2.414

Run 2:

Enter the a coefficient (non-zero value): -1
Enter the b coefficient: 2
Enter the c coefficient: -1


----------------------------------------
  Quadratic Equation Analyzer
----------------------------------------
a Coefficient                         -1
b Coefficient                          2
c Coefficient                         -1
----------------------------------------
Vertex
X Coordinate                       1.000
Y Coordinate                       0.000
----------------------------------------
The parabola opens DOWNWARD
----------------------------------------
The parabola has ONE root
Root 1 - X Coordinate              1.000

Run 3:

Enter the a coefficient (non-zero value): 12
Enter the b coefficient: 2
Enter the c coefficient: 3


----------------------------------------
  Quadratic Equation Analyzer
----------------------------------------
a Coefficient                         12
b Coefficient                          2
c Coefficient                          3
----------------------------------------
Vertex
X Coordinate                      -0.083
Y Coordinate                       2.917
----------------------------------------
The parabola opens UPWARD
----------------------------------------
The parabola has NO roots