# Thread: help find my error?

1. ## help find my error?

So far i have been able to successfully output the first three that are asked for, shown in copied and pasted problem below, but when i input 0, 8 , -12 i get the output.

nan
-inf

I'm utterly unsure why i would get this kind of output, but i'm assuming it's an obvious indicator that the last section of my code is where the error, or errors, are. I'm certainly not asking for anyone to complete my assignment, as i have about half of it left, but any help on any of my code to point me in the right direction would be very much appreciated.
__________________________________________________ ____________

The question:
__________________________________________________ _________________

The solution of the equation ax2 + bx + c has 6 cases, that can occur with various values of a, b, and c.
Test for these 6 cases in the order given here.
Do not compute any values until you have determined what calculations are needed.
The 6 cases are:

1. When a, b, and c are all zero, any value of x is a solution. Print: Any value of x is a solution.
2. When a and b are zero and c is not, no solution exists. Print: No solution exists.
3. When a is zero and b is not zero, the only solution is x = -c/b. Calculate the value of x and print the solution.

The remaining three cases are determined by the value of the determinant.
The determinant is b2 - 4ac.
Compute and save the value of the dererminant now.
You can use the value of the determinant you saved to select one of the remaining three cases.
4. When the determinant is zero, the only solution is x = -b/2a. Calculate the value of x and print the solution.
5. When the determinant is positive, two solutions are given by the following two expressions:
x1 = ( -b + √b2 - 4ac ) / 2a
x2 = ( -b - √b2 - 4ac ) / 2a
Print both solutions.
6. When the determinant is negative, the solutions have an imaginary component. Print: The solutions have an imaginary component.
If you are fimiliar with imaginary numbers, you may compute and print the result, but this is not required.

Test it 7 times:

• a = 0 b = 0 c = 0
• a = 0 b = 0 c = 4
• a = 0 b = 8 c = -12
• a = 2 b = 4 c = 2
• a = 2 b = 2 c = 0
• a = 100 b=100 c= -11
• a = 1 b = 1 c = 1

Check your results by hand, by substituting your results back into the equation and verify that they are roots.

__________________________________________________ _______

my code:
__________________________________________________ __________

Code:
``` #include <cmath>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
double a;
double b;
double c;
double determinant = pow(b, 2) - (4 * a *c);
double x3 = -c / b;
double x4;
double x5;
double x5a;

// Established the variables a, b, and c.
// Also established determinant.
// Established x outputs as x4 for the fourth situation, hence x4.
// x5 and x5a are defined for the to potential solutions for situation 5,
// hence 5 and 5a.

cout << "please enter value for a:   ";
cin >> a;
cout << "please enter a value for b:  ";
cin >> b;
cout << "please enter c value:  ";
cin >> c;

if (a == 0 && b == 0){ // if a, b, and c = 0 any x is correct.
if (c == 0)
cout << "any x value is correct" << endl;
}
if (a == 0 && b == 0 && c != 0) {  // if a and b are zero and c isn't, no solution
cout << "invalid, no solution" << endl;
}
determinant = pow(b, 2) - (4 * a *c);
if (determinant == 0 && a != 0){
x4 = -b / (2 * a);
cout << x4 << endl;
}

if (determinant > 0){
x5 = (-b + sqrt(pow(b,2)- (4*a*c))) / (2 * a);
x5a = (-b - sqrt(pow(b,2)- (4*a*c))) / (2 * a);
cout << x5 << endl;
cout << x5a << endl;
}

}```

2. Code:
``` #include <cmath>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
double a;
double b;
double c;
double determinant = pow(b, 2) - (4 * a *c);
double x3 = -c / b;
double x4;
double x5;
double x5a;

// Established the variables a, b, and c.
// Also established determinant.
// Established x outputs as x4 for the fourth situation, hence x4.
// x5 and x5a are defined for the to potential solutions for situation 5,
// hence 5 and 5a.

cout << "please enter value for a:   ";
cin >> a;
cout << "please enter a value for b:  ";
cin >> b;
cout << "please enter c value:  ";
cin >> c;

if (a == 0 && b == 0){ // if a, b, and c = 0 any x is correct.
if (c == 0)
cout << "any x value is correct" << endl;
}
if (a == 0 && b == 0 && c != 0) {  // if a and b are zero and c isn't, no solution
cout << "invalid, no solution" << endl;
}
determinant = pow(b, 2) - (4 * a *c);
if (determinant == 0 && a != 0){
x4 = -b / (2 * a);
cout << x4 << endl;
}

if (determinant > 0){
x5 = (-b + sqrt(pow(b,2)- (4*a*c))) / (2 * a);
x5a = (-b - sqrt(pow(b,2)- (4*a*c))) / (2 * a);
cout << x5 << endl;
cout << x5a << endl;
}

}```

Half the problem is you guys don't want to take 10 seconds and add a few cout statements so you know what your code is doing. You just want to print the number to the screen and you think you have done something...

Add a few cout's and you can actually see what you are doing, and the user is informed as well.

Code:
```#include <cmath>#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
double a;
double b;
double c;
double determinant = pow(b, 2) - (4 * a *c);
double x3 = -c / b;
double x4;
double x5;
double x5a;

// Established the variables a, b, and c.
// Also established determinant.
// Established x outputs as x4 for the fourth situation, hence x4.
// x5 and x5a are defined for the to potential solutions for situation 5,
// hence 5 and 5a.

cout << "please enter value for a:   ";
cin >> a;
cout << "please enter a value for b:  ";
cin >> b;
cout << "please enter c value:  ";
cin >> c;

if  (a+b+c==0)
{
cout << "any x value is correct" << endl;
}
if  ((a == 0 )&& (b == 0) && (c != 0))
{
cout << "invalid, no solution" << endl;
}
else
{
determinant = pow(b, 2) - (4 * a *c);
cout << "Determinant is " << determinant << endl;

if ((determinant == 0) && (a != 0))
{
x4 = -b / (2 * a);
cout << x4 << endl;
}

if (determinant > 0)
{
x5 = (-b + sqrt(pow(b,2)- (4*a*c))) / (2 * a);
x5a = (-b - sqrt(pow(b,2)- (4*a*c))) / (2 * a);
cout << "X5 is " << x5 << endl;
cout << "X5a is " << x5a << endl;
}
else
{
if (determinant<0)
{
cout << "Determinant is less  than zero" << endl;
}
}

}

return 0;
}```

3. Personally, instead of pow(b, 2) I would just write b * b.

4. Originally Posted by Ghostrider
...
Code:
```    if  (a+b+c==0)
{
cout << "any x value is correct" << endl;
}```
Why did you do this?

5. It was shorter than if a==0 && b==0 && c==0...

if a+b+c=0 then it stands to reason they are all three zero...

6. Originally Posted by Ghostrider
It was shorter than if a==0 && b==0 && c==0...

if a+b+c=0 then it stands to reason they are all three zero...
Yes, it would stand to reason if a, b, c and were natural numbers, but they're not Consider a polynomial (of the one given) with the coefficients a=4, b=2,c=-6. This is perfectly valid and they sum to 0.

7. Originally Posted by Hodor
Yes, it would stand to reason if a, b, c and were natural numbers, but they're not Consider a polynomial (of the one given) with the coefficients a=4, b=2,c=-6. This is perfectly valid and they sum to 0.
Code:
```double a;
double b;
double c;
double determinant = pow(b, 2) - (4 * a *c);
double x3 = -c / b;
double x4;
double x5;
double x5a;

// Established the variables a, b, and c.
// Also established determinant.
// Established x outputs as x4 for the fourth situation, hence x4.
// x5 and x5a are defined for the to potential solutions for situation 5,
// hence 5 and 5a.

cout << "please enter value for a:   ";
cin >> a;
cout << "please enter a value for b:  ";
cin >> b;
cout << "please enter c value:  ";
cin >> c;

if  (a+b+c==0)
{
cout << "any x value is correct" << endl;```
according to the code I saw, they are doubles, 3 separate inputs. They have not been modified at the point I entered that line, nothing cycles back to it after they have been modified... If it were a function instead of a statement, I would agree with you. Since it's not, I stand beside my code sir.

8. Originally Posted by Ghostrider
according to the code I saw, they are doubles, 3 separate inputs. They have not been modified at the point I entered that line, nothing cycles back to it after they have been modified... If it were a function instead of a statement, I would agree with you. Since it's not, I stand beside my code sir.
Just ignoring some things for the moment.

a) You're saying that a double cannot be negative?
b) Since they are double you should not be using == 0 anyway

I don't understand your statement "They have not been modified at the point I entered that line"... they are read from stdin

Edit: And, also, now that I look at the code the determinant is calculated before a, b & c are initialised (not due to your change of course)

Edit 2: To the OP: I doubt that since this is an introductory exercise a, b, and c should be of type double anyway (I would hazard to guess that your instructor meant you to use int). A polynomial can have real (e.g. double) coefficients but... I'm not sure you should be handling that (best to ask your instructor)

9. a) You're saying that a double cannot be negative?
b) Since they are double you should not be using == 0 anyway

I don't understand your statement "They have not been modified at the point I entered that line"... they are read from stdin

Edit: And, also, now that I look at the code the determinant is calculated before a, b & c are initialised (not due to your change of course)[/QUOTE]

No I'm not saying they can't be negative, and I'm not saying the code is correct. I'm saying a+b+c==0 is a valid replacement for if a==0 and b==0 and c==0.
since the original line of code assumes they can be zero, so can mine. Don't go bustin my chops for being lazy while I was griping him out for bein lazy!!!

Edit: And I don't see where they have been modified on initilization... that doesn't happen until around line 40 - 45 somewhere.

10. Replacing if a == 0 && b == 0 && c == 0 with a + b + c == 0 iff a, b, and c are natural numbers (i.e. unsigned int for example). And they're not.

You can test for yourself easily enough.

Edit: Since you can't seem to believe me here is a practical example (it's in C.. the same applies for C++ or any language)

Code:
```#include <stdio.h>
#include <assert.h>

int main(void)
{
int a = 4, b = 2, c = -6;

int b1 = (a == 0 && b == 0 && c == 0);
int b2 = (a + b + c == 0);

assert(b1 == b2);

return 0;
}```
Does the assertion hold true?

11. Originally Posted by Hodor
Does the assertion hold true?
Code:
```#include <iostream>
#include <stdio.h>
#include <assert.h>
using namespace std;

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

double b1 = (a == 0 && b == 0 && c == 0);
cout << "b1 returns "<< b1 << endl;

double b2 = (a + b + c == 0);
cout << "b2 returns "<< b2<< endl;

if (a==0 && b==0 & c==0) { cout << "They all compared to zero." << endl;}

if ( a+b+c==0) {cout << "They add up to zero." << endl;}

assert(b1 == b2);

return 0;
}```
Not being a smartass, but we're comparing zeros (as per the original code)... Your equations return 1... I fail to see the difference between my line of code, and his as it pertains to this code, this situation. I'll read up on assert, but it really comes off to me as you're splitting hairs, I still see nothing wrong with that line.

Note: If you are trying to say it's not proper c++, Okay, I'll bite... Hell, I'll throw you a ticker taped parade for catching it, if you want one... but if you are telling me that my thinking is stinking... comparing 3 variables with the value of zero is different than adding 3 variables with the value of zero... I don't buy it. I still come up with zero either way. The rest of the statement will be ignored, the program continues, the sun still shines and the planet still spins... I may feel differently in other codes, but for this code, here and now... I'm still good with it. The logic is solid from my perspective.

12. Why are you assuming a, b, and c are 0?

Code:
`double a = 0, b = 0, c = 0;`
That doesn't make sense. The original code is:

Code:
```    cout << "please enter value for a:   ";
cin >> a;
cout << "please enter a value for b:  ";
cin >> b;
cout << "please enter c value:  ";
cin >> c;```
I assure you that I am not splitting hairs.

Edit: Also, my "equations" do not both evaluate to 1.

Even ignoring that your change is incorrect, a+b+c==0 is less efficient than a == 0 && b == 0 && c == 0

Additionally, as I've also mentioned, you should not be using == when comparing floating point numbers.

Code:
```#include <stdio.h>
#include <assert.h>

int main(void)
{
int a = 4, b = 2, c = -6;

int b1 = (a == 0 && b == 0 && c == 0);
int b2 = (a + b + c == 0);

printf("b1 is %d and b2 is %d\n", b1, b2);

assert(b1 == b2);

return 0;
}

t: t.c:14: main: Assertion `b1 == b2' failed.
b1 is 0 and b2 is 1```

13. Well Hodor.. I read up on this (as I said I would) and I have made a discovery you should hear. You are 100% correct, right down to the time when the compare breaks out of the statement vs the mathematical one. I was 100% wrong. I bow to your insight.

Now Don't just sit there and watch me eat this crow... pass the salt.

14. Originally Posted by Ghostrider
Now Don't just sit there and watch me eat this crow... pass the salt.
/me passes the salt. Can't we eat something a little bit tastier?