Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Quadratic Equation C++ Typical sample runs of the program should appear as shown

ID: 3735837 • Letter: Q

Question

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 <iostream>
#include <cmath>
#include <iomanip>
#include <sstream>
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 " <<setprecision(0)<<quadToString(qCoef, linCoef, cTerm) << ": " << endl;
cout << "Discriminant: " << discriminant(qCoef, linCoef, cTerm) << endl;
cout << "Axis of symmetry: x = " << (-linCoef / (2*qCoef)) << endl;

double x = -linCoef / (2 * qCoef);
double y = (4 * qCoef * cTerm - linCoef * linCoef) / (4 * qCoef);
cout << "Vertex: (" << x << "," << y << ")" << endl;


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

if (discriminant==0)
{
    cout << "x-intercept: ("<<rat<< endl;
}
else if (discriminant < 0)
{
    cout << "x-intercept: none" << endl;
}
else
{
    cout << "x-intercept: (" << root1 << ", " << 0 << ") and (" << root2 << ", " << 0 << ")" << endl;
}
   
   
cout << "y-intercept: (" << 0 << "," << cTerm << ")" << endl;


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);
}
}

Explanation / Answer

#include <iostream>
#include <cmath>
#include <iomanip>
#include <sstream>
using namespace std;
/*
* the changes made has been commented
*/
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 0;
}

double rat=0;
double irrat=0;
bool cmplx;

solve(qCoef, linCoef, cTerm, rat, irrat, cmplx);


cout << "For the quadratic equation " <<setprecision(0)<<quadToString(qCoef, linCoef, cTerm) << endl;
cout << fixed << setprecision(3);
//to set the print format of decimal number
//in your code u set to 3 and again setted to setprecision (0). so the setPrecision value is 0 now. so it doesn't work for setprecision(3).
//the return value of the function discriminant is stored in the variable
double discriminate_val = discriminant(qCoef, linCoef, cTerm);
cout << "Discriminant: "<<discriminate_val << endl;
cout << "Axis of symmetry: x = " << (-linCoef / (2*qCoef)) << endl;

double x = -linCoef / (2 * qCoef);
double y = (4 * qCoef * cTerm - linCoef * linCoef) / (4 * qCoef);
cout << "Vertex: (" << x << "," << y << ")" << endl;


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

if (discriminate_val==0)
{
//to print the x intercept when the discriminant value is 0
cout << "x-intercept: ("<<rat<<", 0.000)"<< endl;
}
else if (discriminate_val < 0)
{
cout << "x-intercept: none" << endl;
}
else
{
cout << "x-intercept: (" << root1 << ", " << 0 << ") and (" << root2 << ", " << 0 << ")" << endl;
}


cout << "y-intercept: (" << 0 << "," << cTerm << ")" << endl;


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 if(discriminate_val==0){ // if only one value exist the print only one root
cout<<"Roots : x= {"<<root1<<"}"<<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(disc < 0)//check only the value of the which the function returns
{
cmplx = true;
irrat = sqrt(abs(disc)) / (abs(2 * qCoef));
}
else
{
cmplx = false;
irrat = sqrt(disc) / (2 * qCoef);
}

}

Sample Run:

Enter the coefficient of the quadratic term -> 4
Enter the coefficient of the linear term -> -12
Enter the constant term -> 25
For the quadratic equation 4x^2 - 12x + 25 = 0:
Discriminant: -256.000
Axis of symmetry: x = 1.500
Vertex: (1.500,16.000)
x-intercept: none
y-intercept: (0,25.000)
Shape: Concave upward
Roots: x = {1.500+2.000i, 1.500-2.000i}

RUN SUCCESSFUL (total time: 15s)