C++ Solver

This is my first computer science class and I am completely lost.
Write a C++ program call QuadraticSolver that prompts the user for the coefficient of the quadratic term, the coefficient of the linear term, and the constant term of a quadratic equation. The program then invokes the quadToString function with the relevant arguments to display the quadratic equation and determines its discriminant, roots, axis of symmetry, vertex, x-intercepts and y-intercept, invoking the appropriate sub-functions, where applicable. It also determines whether the parabola is concave upward or downward. A parabola is concave upward if the coefficient of its quadratic term is positive and concave downward when the coefficient of the quadratic term is negative.
Write the program incrementally. Write a preliminary version of the program so that it prints a message indicating that the equation is not quadratic if the input for the quadratic coefficient is 0 and calls the quadToString func- tion with the relevant arguments to print the quadratic equation in standard form when a non-zero coefficient is entered for the quadratic term. You can then define the discriminant and solve sub-functions and add code to com- pute the roots and all the properties of the equation, calling the appropriate sub-functions where applicable. You may also want to incrementally add the code for solving the equation: First, the code to solve an equation whose ir- rational part is 0, second, the code to solve the equation when it has two real roots and, finally, the code to solve the equation when its roots are complex. 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.000}

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.000, 64.000)
10 y-intercept: (0.000, 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.000
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.000, 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.000i}
Last edited on
ok.
lets start slow.
do you know how to read in some numbers from the user? (show us, start writing some code that can get the steps 1-4 in your listing).

do you know the quadratic formula?
Last edited on
Yes its ax2 +bx+c=0

cout<<"Enter the coefficient of the quadratic term -> ";
cout<<"Enter the coefficient of the linear term -> ";
cout<<"Enter the constant term -> ";
Notice that all you need for a quadratic is the three numbers you just input.

After that it is straight up math. Your professor gave you the order in which to solve them. So the next thing to do is write a function that returns the discriminant. Do that, then print it to the user:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

string quadToString( double a, double b, double c )
{
  ostringstream ss;
  ss << /* the equation */ ;
  return ss.str();
}

double discriminant( double a, double b, double c )
{
  return /* math goes here */ ;
}

int main()
{
  double a, b, c;

  cout<<"Enter the coefficient of the quadratic term -> ";
  cin >> a;

  cout<<"Enter the coefficient of the linear term -> ";
  cin >> b;

  cout<<"Enter the constant term -> ";
  cin >> c;

  cout << "You have entered the quadratic: " << quadToString( a, b, c ) << "\n";

  cout << "The discriminant is: " << discriminant( a, b, c ) << "\n";
}

Once you have that working, add a function for the axis of symmetry.
And so on.

Good luck!
Write a preliminary version of the program so that it prints a message indicating that the equation is not quadratic if the input for the quadratic coefficient is 0 and calls the quadToString function with the relevant arguments to print the quadratic equation in standard form when a non-zero coefficient is entered for the quadratic term.

There seems to be a need for one test before line 32 (of Duthomas' code)
What do I need to fix to make this code work?

#include<iostream.h>
#include<conio.h>
#include<math.h>

using namespace std;


int quadraticsolver();
int quadToString(int,int,int);
int shape(int);
int display(int,int,int);
int quadraticsolver()
{
int a,b,c;
cout<<"\nEnter coefficient of the quadratic term->";
cin>>a;
cout<<"\nEnter coefficient of the linear term->";
cin>>b;
cout<<"\nEnter the constant term->";
cin>>c;

quadToString(a,b,c);
}

void quadToString(int a,int b,int c)
{
double r1,r2;
double real,imag;
if(a==0)
{
cout<<"\nERROR: The quadratic term must be nonzero.";
}
else
{

display(a,b,c);
double d=(b*b)-(4*a*c);
double axis=-b/(2*a);

cout<<"\n\nDiscriminant:"<<d;
cout<<"\nAxis of Symmetry: x="<<axis;
cout<<"\nvertex: ("<<-b/(2*a)<<","<<a*pow(axis,2)+b*axis+c<<")";
if(d>0)
{
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
cout<<"\ny-intercept:("<<"0.000, "<<c<<")";
cout<<"\nx-intercept:("<<r2<<", 0.000 ) and ("<<r1<<", 0.000)";
shape(a);
cout<<"\nRoots: x ={"<<r1<<","<<r2<<"}";

}
else if(d==0)
{
r1=r2=-b/2*a;
cout<<"\ny-intercept:("<<"0.000, "<<c<<")";
cout<<"\nx-intercept:("<<r1<<", 0.000 )";
shape(a);
cout<<"Root: x={"<<r1<<"}";
}
else
{
real=-b/(2*a);
imag=sqrt(-d)/(2*a);
cout<<"\ny-intercept:("<<"0.000, "<<(double)c<<")";
cout<<"\nx-intercept: none";
shape(a);
cout<<"\nRoots: x={"<<real<<"+"<<imag<<"i,"<<real<<"-"<<imag<<"i}";
}
}
}
void shape(int a)
{
if(a>0)
cout<<"\nShape: Concave upward";
else
cout<<"\nShape: Concave downward";
}
void display(int a, int b, int c)
{
char c1,c2;
if(b>0)
{ c1='+';
}
if(c>0)
{
c2='+';
}
if(b>0&&c>0)
{
cout<<"\nFor the quadratic equation "<<a<<"x^2"<<c1<<b<<"x"<<c2<<c<<"=0";
}
else if(b<0&&c>0)
{
cout<<"\nFor the quadratic equation "<<a<<"x^2"<<b<<"x"<<c2<<c<<"=0";
}
else if(b>0&&c<0)
{
cout<<"\nFor the quadratic equation "<<a<<"x^2"<<c1<<b<<"x"<<c<<"=0";
}
else if(b==0&&c>0)
{
cout<<"\nFor the quadratic equation "<<a<<"x^2"<<c2<<c<<"=0";
}
else if(b==0&&c<0)
{
cout<<"\nFor the quadratic equation "<<a<<"x^2"<<c<<"=0";
}
else if(b>0&&c==0)
{
cout<<"\nFor the quadratic equation "<<a<<"x^2"<<c1<<b<<"x=0";
}
else
{
cout<<"\nFor the quadratic equation "<<a<<"x^2"<<b<<"x"<<c<<"=0";
}
}
int main()
{
clrscr();
quadraticsolver();
getch();
}
what exactly is not working? Its easier to find if you describe the issues...

put it in code tags <> on the editor pane next door. You can edit and fix this, and people will be more inclined to read the code.

minor:
you are using C headers. <iostream>, <cmath> are C++ versions. It works regardless but it causes trouble in more complicated programs to mix and match them.

extra credit: c++ has a complex data type, if you want to recode to use that after its working instead of doing it manually.

You need to declare the functions consistently (they should all return void).

display()can be greatly simplified.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <cmath>

using namespace std;

void quadraticsolver();
void quadToString(int, int, int);
void shape(int);
void display(int, int, int);

void
quadraticsolver()
{
    int a, b, c;
    cout << "\nEnter coefficient of the quadratic term->";
    cin >> a;
    cout << "\nEnter coefficient of the linear term->";
    cin >> b;
    cout << "\nEnter the constant term->";
    cin >> c;

    quadToString(a, b, c);
}

void
quadToString(int a, int b, int c)
{
    double r1, r2;
    double real, imag;
    if (a == 0) {
	cout << "\nERROR: The quadratic term must be nonzero.";
    } else {

	display(a, b, c);
	double d = (b * b) - (4 * a * c);
	double axis = -b / (2 * a);

	cout << "\n\nDiscriminant:" << d;
	cout << "\nAxis of Symmetry: x=" << axis;
	cout << "\nvertex: (" << -b / (2 * a) << "," << a * pow(axis,
								2) + b * axis + c << ")";
	if (d > 0) {
	    r1 = (-b + sqrt(d)) / (2 * a);
	    r2 = (-b - sqrt(d)) / (2 * a);
	    cout << "\ny-intercept:(" << "0.000, " << c << ")";
	    cout << "\nx-intercept:(" << r2 << ", 0.000 ) and (" << r1 << ", 0.000)";
	    shape(a);
	    cout << "\nRoots: x ={" << r1 << "," << r2 << "}";

	} else if (d == 0) {
	    r1 = r2 = -b / 2 * a;
	    cout << "\ny-intercept:(" << "0.000, " << c << ")";
	    cout << "\nx-intercept:(" << r1 << ", 0.000 )";
	    shape(a);
	    cout << "Root: x={" << r1 << "}";
	} else {
	    real = -b / (2 * a);
	    imag = sqrt(-d) / (2 * a);
	    cout << "\ny-intercept:(" << "0.000, " << (double) c << ")";
	    cout << "\nx-intercept: none";
	    shape(a);
	    cout << "\nRoots: x={" << real << "+" << imag << "i," << real << "-" << imag
		<< "i}";
	}
    }
}

void
shape(int a)
{
    if (a > 0)
	cout << "\nShape: Concave upward";
    else
	cout << "\nShape: Concave downward";
}

void
display(int a, int b, int c)
{
    cout << "\nFor the quadratic equation " << a << "x^2";

    if (b<0) {
	cout << b << 'x';
    } else if (b>0) {
	cout << '+' << b << 'x';
    } // else it's zero and you print nothing

    if (c<0) {
	cout << c;
    } else if (c>0) {
	cout << '+' << c;
    } // else it's zero and you print nothing

    cout << "=0";
}

int
main()
{
    quadraticsolver();
}

Topic archived. No new replies allowed.