Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Quadratic Equation C++ Typical sample runs of the program should appear as shown below: Listing 1: Sample Run 1 Enter the coefficient of the quadratic

Quadratic Equation C++

Typical sample runs of the program should appear as shown below:

Listing 1: Sample Run

1 Enter the coefficient of the quadratic term -> 1

2 Enter the coefficient of the linear term -> -6

3 Enter the constant term -> 9

4

5 For the quadratic equation x^2 - 6x + 9 = 0:

6

7 Discriminant: 0.000

8 Axis of Symmetry : x = 3.000

9 Vertex : (3.000 , 0.000)

10 y- intercept: (0.000 , 9.000)

11 x- intercept: (3.000 , 0.000)

12 Shape: Concave upward

13 Root: x = {3.00000}

Listing 2: Sample Run

1 Enter the coefficient of the quadratic term -> -4

2 Enter the coefficient of the linear term -> 0

3 Enter the constant term -> 64

4

5 For the quadratic equation -4x^2 + 64 = 0:

6

7 Discriminant: 1024.000

8 Axis of Symmetry : x = 0.000

9 Vertex : (0.00000 , 64.000)

10 y- intercept: (0.00000 , 64.000)

11 x- intercepts: (4.000 , 0.000) and ( -4.000 , 0.000)

12 Shape: Concave downward

13 Roots: x = { -4.000 , 4.000}

Listing 3: Sample Run

1 Enter the coefficient of the quadratic term -> 3

2 Enter the coefficient of the linear term -> 45

3 Enter the constant term -> 0

4

5 For the quadratic equation 3x^2 + 45x = 0:

6

7 Discriminant: 2025.00000

8 Axis of Symmetry : x = -7.500

9 Vertex : ( -7.500 , -168.750)

10 y- intercept: (0.000 , 0.000)

11 x- intercepts: ( -15.000 , 0.000) and (0.000 , 0.000)

12 Shape: Concave upward

13 Roots: x = {0.000 , -15.000}

Listing 4: Sample Run

1 Enter the coefficient of the quadratic term -> 0

2 Enter the coefficient of the linear term -> 9

3 Enter the constant term -> 2.5

4

5 ERROR: The quadratic term must be nonzero .

Listing 5: Sample Run

1 Enter the coefficient of the quadratic term -> 12

2 Enter the coefficient of the linear term -> -7

3 Enter the constant term -> -12

4

5 For the quadratic equation 12x^2 - 7x - 12 = 0:

6

7 Discriminant: 625.000

8 Axis of Symmetry : x = 0.292

9 Vertex : (0.292 , -13.021)

10 y- intercept: (0.000 , -12.000)

11 x- intercepts: ( -0.750 , 0.000) and (1.333 , 0.000)

12 Shape: Concave upward

13 Roots: x = {1.333 , -0.750}

Listing 6: Sample Run

1 Enter the coefficient of the quadratic term -> 9

2 Enter the coefficient of the linear term -> 0

3 Enter the constant term -> 16

4

5 For the quadratic equation 9x^2 + 16 = 0:

6

7 Discriminant: -576.000

8 Axis of Symmetry : x = -0.000

9 Vertex : ( -0.00000 , 16.000)

10 y- intercept: (0.000 , 16.000)

11 x- intercepts: none

12 Shape: Concave upward

13 Roots: x = {1.333i, -1.333i}

Listing 7: Sample Run

1 Enter the coefficient of the quadratic term -> 4

2 Enter the coefficient of the linear term -> -12

3 Enter the constant term -> 25

4

5 For the quadratic equation 4x^2 - 12x + 25 = 0:

6

7 Discriminant: -256.000

8 Axis of Symmetry : x = 1.500

9 Vertex : (1.500 , 16.000)

10 y- intercept: (0.000 , 25.000)

11 x- intercepts: none

12 Shape: Concave upward

13 Roots: x = {1.500+2.000i, 1.500 -2.000 i}

I can't get these sample runs, and I don't know what's wrong with the set precision, x-intercepts, and roots. I've attached my code.

#include #include #include #include using namespace std;

string quadToString(double qCoef, double linCoef, double cTerm); double discriminant(double qCoef, double linCoef, double cTerm); void solve(double qCoef, double linCoef, double cTerm, double& rat, double& irrat, bool& cmplx);

int main() { double qCoef, linCoef, cTerm;

cout << "Enter the coefficient of the quadratic term -> " ; cin >> qCoef; cout << "Enter the coefficient of the linear term -> "; cin >> linCoef; cout << "Enter the constant term -> "; cin >> cTerm;

if(qCoef == 0) { cout << "ERROR: The quadratic term must be non-zero" << endl; return 1; }

double rat, irrat; bool cmplx; solve(qCoef, linCoef, cTerm, rat, irrat, cmplx); cout << fixed << setprecision(3); cout << "For the quadratic equation " <

double root1 , root2; root1 = rat + irrat; root2 = rat - irrat;

if (discriminant==0) { cout << "x-intercept: ("<

if(qCoef > 0) cout << "Shape: Concave upward" << endl; else cout << "Shape: Concave downward" << endl;

if(cmplx=true) { cout << "Roots: x = {" << rat << "+" << irrat << "i, " << rat << "-" << irrat << "i}" << endl; } else { cout << "Roots: x = {" << root1 << ", " << root2 << "}" << endl; } return 0; }

string quadToString(double qCoef, double linCoef, double cTerm) { stringstream ss; if (qCoef == 1) { ss << "x^2"; } else if (qCoef == -1) { ss << "-x^2"; } else { ss << qCoef << "x^2"; } if (linCoef == 1) { ss << " + x "; } else if (linCoef == -1) { ss << " - x "; } else if (linCoef != 0) { if (linCoef > 0) { ss << " + " << linCoef << "x"; } else { ss << " - " << abs(linCoef) << "x"; } } if (cTerm != 0) { if (cTerm > 0) { ss << " + " << cTerm; } else ss << " - " << abs(cTerm); } ss << " = 0:"; return ss.str(); }

double discriminant(double qCoef, double linCoef, double cTerm) { return linCoef * linCoef - 4 * qCoef * cTerm; }

void solve(double qCoef, double linCoef, double cTerm, double& rat, double& irrat, bool& cmplx) { double disc = discriminant(qCoef, linCoef, cTerm);

rat = -linCoef / (2 * qCoef); if(discriminant < 0) { cmplx = true; irrat = sqrt(abs(disc)) / (abs(2 * qCoef)); } else { cmplx = false; irrat = sqrt(disc) / (2 * qCoef); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions