Hey guys, I'm almost done with my homework assignment in an intro c++ class.

My assignment found here:

http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm2.htm

My problem is with the output of the roots. If you run my program with the same numbers as sample output #2 in the assignment, I only get one root to display.

I can't get the 2nd root to display, I also realize the 2nd root should be a different number than the first root, I'm not sure how to do this.

Here is my program:

``````#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

int main()
{
double Acoe, Bcoe, Ccoe, Xvertex, Yvertex, Discriminant, Roots;
cout << "Enter an integer for A-coefficient (non-zero value):";
cin >> Acoe;
if (Acoe == 0)
{
cout << "ERROR: the A-coefficient MUST be non-zero. Try again:";
cin >> Acoe;
}
else
{
}
cout << "Enter an integer for B-coefficient:";
cin >> Bcoe;
cout << "Enter an integer for C-coefficient:";
cin >> Ccoe;
Xvertex = - Bcoe / ( 2 * Acoe);
Yvertex = ( Acoe * Xvertex * Xvertex ) + ( Bcoe * Xvertex ) + Ccoe;
Discriminant = Bcoe * Bcoe - 4 * Acoe * Ccoe;
Roots = ( -Bcoe + sqrt( Discriminant )) / ( 2 * Acoe );
cout << "Vertex Calculations" << endl;
cout << fixed << setprecision(3) << "A-coefficient: " << Acoe << endl;
cout << fixed << setprecision(3) << "B-coefficient: " << Bcoe << endl;
cout << fixed << setprecision(3) << "C-coefficient: " << Ccoe << endl;
cout << fixed << setprecision(3) << " The X Vertex: " << Xvertex << endl;
cout << fixed << setprecision(3) << " The Y Vertex: " << Yvertex << endl;
if (Acoe > 0)
{
cout << "The parabola opens UPWARD:" << endl;
}
else
{
cout << "The parabola opens DOWNWARD:" << endl;
}
if (Discriminant < 0)
{
cout << "There are no roots" << endl;
}
if (Discriminant == 0)
{
cout << "Root 1 X Coordinate: " << Roots << endl;
cout << "Root 2 X Coordinate: " << Roots << endl;
}
if (Discriminant > 0)
{
cout << "Root 1 X Coordinate: " << Roots << endl;
}
return 0;
}
``````

Any random pointers/code simplification tips are welcome!

[B]thelamb[/B] gave you just enough for you to actually [I]think[/I] about the change and do it properly. He did not give you a total solution...

## All 14 Replies

Where do you calculate both roots? I only see one calculated.

Where do you calculate both roots? I only see one calculated.

I believe there are supposed to be two calculations in one formula because of the +- (plus minus) square root in the formula.

I am using the formula the teacher provided in the link.

I'm really not sure how this Roots thing is supposed to work, why I'm stuck at this point.

I believe there are supposed to be two calculations in one formula because of the +- (plus minus) square root in the formula.

I am using the formula the teacher provided in the link.

I'm really not sure how this Roots thing is supposed to work, why I'm stuck at this point.

Am I supposed to run the formula twice, one positive and one negative?

I bet i just answered my own question, I'll make the change and post results.

Okay so I made the change, now I just can't get Root2 to output at all. Must be something in this area of the code?

``````if (Discriminant == 0)
{
cout << fixed << setprecision(3) << "Root 1 X Coordinate: " << Root1 << endl;
cout << fixed << setprecision(3) << "Root 2 X Coordinate: " << Root2 << endl;
}``````

Full Code:

``````#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

int main()
{
double Acoe, Bcoe, Ccoe, Xvertex, Yvertex, Discriminant, Root1, Root2;
cout << "Enter an integer for A-coefficient (non-zero value):";
cin >> Acoe;
if (Acoe == 0)
{
cout << "ERROR: the A-coefficient MUST be non-zero. Try again:";
cin >> Acoe;
}
else
{
}
cout << "Enter an integer for B-coefficient:";
cin >> Bcoe;
cout << "Enter an integer for C-coefficient:";
cin >> Ccoe;
Xvertex = - Bcoe / ( 2 * Acoe);
Yvertex = ( Acoe * Xvertex * Xvertex ) + ( Bcoe * Xvertex ) + Ccoe;
Discriminant = Bcoe * Bcoe - 4 * Acoe * Ccoe;
Root1 = ( -Bcoe + sqrt( Discriminant )) / ( 2 * Acoe );
Root2 = ( -Bcoe - sqrt( Discriminant )) / ( 2 * Acoe );
cout << "Vertex Calculations" << endl;
cout << fixed << setprecision(3) << "A-coefficient: " << Acoe << endl;
cout << fixed << setprecision(3) << "B-coefficient: " << Bcoe << endl;
cout << fixed << setprecision(3) << "C-coefficient: " << Ccoe << endl;
cout << fixed << setprecision(3) << " The X Vertex: " << Xvertex << endl;
cout << fixed << setprecision(3) << " The Y Vertex: " << Yvertex << endl;
if (Acoe > 0)
{
cout << "The parabola opens UPWARD:" << endl;
}
else
{
cout << "The parabola opens DOWNWARD:" << endl;
}
if (Discriminant < 0)
{
cout << "There are no roots" << endl;
}
if (Discriminant == 0)
{
cout << "Root 1 X Coordinate: " << Root1 << endl;
cout << "Root 2 X Coordinate: " << Root2 << endl;
}
if (Discriminant > 0)
{
cout << "Root 1 X Coordinate: " << Root1 << endl;
}
return 0;
}``````

You can't get root2 to output.... are you sure that you are entering values for A, B, C etc. so that Discriminant is 0? As when it's > 0 only Root1 is printed of course.

Also have a look at how you check if 'A' is non-zero. What happens if I enter a non-zero value twice in a row? Validating user-input is a very important aspect of coding, and a lot of people think lightly about it but they absolutely shouldn't.

Another note on your code... You want to check if Discriminant is < 0, == 0 or > 0... so you start with checking < 0.
If this is true ( Discriminant < 0 ), what can you say about the 'Discriminant > 0' and 'Discriminant == 0' checks? Are they useful?

You can't get root2 to output.... are you sure that you are entering values for A, B, C etc. so that Discriminant is 0? As when it's > 0 only Root1 is printed of course.

Also have a look at how you check if 'A' is non-zero. What happens if I enter a non-zero value twice in a row? Validating user-input is a very important aspect of coding, and a lot of people think lightly about it but they absolutely shouldn't.

Another note on your code... You want to check if Discriminant is < 0, == 0 or > 0... so you start with checking < 0.
If this is true ( Discriminant < 0 ), what can you say about the 'Discriminant > 0' and 'Discriminant == 0' checks? Are they useful?

Late last night I found the error that I was using the < where I should have used the >, once I switched these I was able to achieve my teacher's outputs.

I know if you enter "A" as a non zero twice, an error will come up. I believe my teacher only wanted this error to appear once, but by all means if I could make it appear multiple times that would be great. I believe it would involve loops, which is next weeks lectures.

On your last note about checking discriminants, I realize the last two checks would be useless if the first check passes, but I wasn't sure if there was a better way of performing the required action.

Here is the code i came up with last night.

``````#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main()
{
double Acoe, Bcoe, Ccoe, Xvertex, Yvertex, Discriminant, Root1, Root2;
cout << "Enter an integer for A-coefficient (non-zero value):";
cin >> Acoe;
if (Acoe == 0)
{
cout << "ERROR: the A-coefficient MUST be non-zero. Try again:";
cin >> Acoe;
}
else
{
}
cout << "Enter an integer for B-coefficient:";
cin >> Bcoe;
cout << "Enter an integer for C-coefficient:";
cin >> Ccoe;
cout << "*******************************************" << endl;
Xvertex = - Bcoe / ( 2 * Acoe);
Yvertex = ( Acoe * Xvertex * Xvertex ) + ( Bcoe * Xvertex ) + Ccoe;
Discriminant = Bcoe * Bcoe - 4 * Acoe * Ccoe;
Root1 = ( -Bcoe + sqrt( Discriminant )) / ( 2 * Acoe );
Root2 = ( -Bcoe - sqrt( Discriminant )) / ( 2 * Acoe );
cout << "Vertex Calculations" << endl;
cout << "*******************************************" << endl;
cout << fixed << setprecision(3) << "A-coefficient: " << Acoe << endl;
cout << fixed << setprecision(3) << "B-coefficient: " << Bcoe << endl;
cout << fixed << setprecision(3) << "C-coefficient: " << Ccoe << endl;
cout << "*******************************************" << endl;
cout << fixed << setprecision(3) << "The X-Vertex : " << Xvertex << endl;
cout << fixed << setprecision(3) << "The Y-Vertex : " << Yvertex << endl;
cout << "*******************************************" << endl;
if (Acoe > 0)
{
cout << "The parabola opens UPWARD" << endl;
}
else
{
cout << "The parabola opens DOWNWARD" << endl;
}
cout << "*******************************************" << endl;
if ( Discriminant < 0)
{
cout << "There are NO real roots" << endl;
}
if ( Discriminant > 0)
{
cout << fixed << setprecision(3) << "Root 1 X Coordinate: " << Root1 << endl;
cout << fixed << setprecision(3) << "Root 2 X Coordinate: " << Root2 << endl;
}
if ( Discriminant == 0 )
{
cout << fixed << setprecision(3) << "Root 1 X Coordinate: " << Root1 << endl;
}
cout << "*******************************************" << endl;
return 0;
}``````

``````if ( Discriminant < 0)
{
cout << "There are NO real roots" << endl;
}else if ( Discriminant > 0 ) // if Discriminant is not smaller than 0
{
cout << fixed << setprecision(3) << "Root 1 X Coordinate: " << Root1 << endl;
cout << fixed << setprecision(3) << "Root 2 X Coordinate: " << Root2 << endl;
}``````

``````if ( Discriminant < 0)
{
cout << "There are NO real roots" << endl;
}else if ( Discriminant > 0 ) // if Discriminant is not smaller than 0
{
cout << fixed << setprecision(3) << "Root 1 X Coordinate: " << Root1 << endl;
cout << fixed << setprecision(3) << "Root 2 X Coordinate: " << Root2 << endl;
}``````

Would I still be leaving the code in there for when discriminant == 0 ?

thelamb gave you just enough for you to actually think about the change and do it properly. He did not give you a total solution...

thelamb gave you just enough for you to actually think about the change and do it properly. He did not give you a total solution...

Sounds good, playing with the code will be good practice :)

Came up with a final solution a few days ago, just forgot to repost here. I also talked with my TA on future ways to right the code in a better format, although for the assignment purposes, the program is 100%.

Thanks for everyone's help.

``````#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main()
{
double Acoe, Bcoe, Ccoe, Xvertex, Yvertex, Discriminant, Root1, Root2;
cout << "Enter an integer for A-coefficient (non-zero value):";
cin >> Acoe;
if (Acoe == 0)
{
cout << "ERROR: the A-coefficient MUST be non-zero. Try again:";
cin >> Acoe;
}
else
{
}
cout << "Enter an integer for B-coefficient:";
cin >> Bcoe;
cout << "Enter an integer for C-coefficient:";
cin >> Ccoe;
cout << "*******************************************" << endl;
Xvertex = - Bcoe / ( 2 * Acoe);
Yvertex = ( Acoe * Xvertex * Xvertex ) + ( Bcoe * Xvertex ) + Ccoe;
Discriminant = Bcoe * Bcoe - 4 * Acoe * Ccoe;
Root1 = ( -Bcoe + sqrt( Discriminant )) / ( 2 * Acoe );
Root2 = ( -Bcoe - sqrt( Discriminant )) / ( 2 * Acoe );
cout << "Vertex Calculations" << endl;
cout << "*******************************************" << endl;
cout << fixed << setprecision(3) << "A-coefficient: " << Acoe << endl;
cout << fixed << setprecision(3) << "B-coefficient: " << Bcoe << endl;
cout << fixed << setprecision(3) << "C-coefficient: " << Ccoe << endl;
cout << "*******************************************" << endl;
cout << fixed << setprecision(3) << "The X-Vertex : " << Xvertex << endl;
cout << fixed << setprecision(3) << "The Y-Vertex : " << Yvertex << endl;
cout << "*******************************************" << endl;
if (Acoe > 0)
{
cout << "The parabola opens UPWARD" << endl;
}
else
{
cout << "The parabola opens DOWNWARD" << endl;
}
cout << "*******************************************" << endl;
if ( Discriminant < 0)
{
cout << "There are NO real roots" << endl;
}
if ( Discriminant > 0)
{
cout << fixed << setprecision(3) << "Root 1 X Coordinate: " << Root1 << endl;
cout << fixed << setprecision(3) << "Root 2 X Coordinate: " << Root2 << endl;
}
if ( Discriminant == 0 )
{
cout << fixed << setprecision(3) << "Root 1 X Coordinate: " << Root1 << endl;
}
cout << "*******************************************" << endl;
return 0;
}``````

> although for the assignment purposes, the program is 100%.
Your assessor missed a big mistake then.

> if ( Discriminant < 0)
Oops - too late!
You've already done the sqrt() on a negative number.

> although for the assignment purposes, the program is 100%.
Your assessor missed a big mistake then.

> if ( Discriminant < 0)
Oops - too late!
You've already done the sqrt() on a negative number.

LOL, thanks! :) It took me about 2 hrs to figure out that little error, that was the reason I couldn't get the 2nd root to display, but never received an error or anything because mathematically everything was working fine and under the circumstances root2 would have never displayed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.