Thread: help find my error?

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    3

    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. #2
    Registered User
    Join Date
    Sep 2015
    Location
    Virginia
    Posts
    19
    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. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,313
    Personally, instead of pow(b, 2) I would just write b * b.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,795
    Quote Originally Posted by Ghostrider View Post
    ...
    Code:
        if  (a+b+c==0)
        {
            cout << "any x value is correct" << endl;
        }
    Why did you do this?

  5. #5
    Registered User
    Join Date
    Sep 2015
    Location
    Virginia
    Posts
    19
    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. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,795
    Quote Originally Posted by Ghostrider View Post
    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. #7
    Registered User
    Join Date
    Sep 2015
    Location
    Virginia
    Posts
    19

    Cool

    Quote Originally Posted by Hodor View Post
    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. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,795
    Quote Originally Posted by Ghostrider View Post
    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)
    Last edited by Hodor; 10-28-2015 at 07:13 AM.

  9. #9
    Registered User
    Join Date
    Sep 2015
    Location
    Virginia
    Posts
    19
    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.


    Last edited by Ghostrider; 10-28-2015 at 07:19 AM.

  10. #10
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,795
    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?
    Last edited by Hodor; 10-28-2015 at 07:30 AM.

  11. #11
    Registered User
    Join Date
    Sep 2015
    Location
    Virginia
    Posts
    19
    Quote Originally Posted by Hodor View Post
    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.
    Last edited by Ghostrider; 10-28-2015 at 09:26 AM.

  12. #12
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,795
    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.

    Edit 2: Using your own advice I added a printf() to display the values
    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
    Last edited by Hodor; 10-28-2015 at 09:34 AM.

  13. #13
    Registered User
    Join Date
    Sep 2015
    Location
    Virginia
    Posts
    19
    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. #14
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,795
    Quote Originally Posted by Ghostrider View Post
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can't find the error
    By endrick in forum C Programming
    Replies: 4
    Last Post: 09-21-2015, 05:53 PM
  2. Cannot find ( before ; error
    By seannabeast in forum C Programming
    Replies: 4
    Last Post: 12-07-2011, 11:29 AM
  3. i cant find the error... Plz help!!
    By fredsilvester93 in forum C Programming
    Replies: 4
    Last Post: 11-29-2011, 03:13 PM
  4. Cant Find Error
    By Toonzaka in forum C Programming
    Replies: 3
    Last Post: 08-10-2008, 02:00 PM
  5. one error....can't find it
    By ammanbesaw in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2007, 10:36 AM