Like Tree4Likes
  • 2 Post By anduril462
  • 1 Post By stahta01
  • 1 Post By stahta01

Program keeps crashing on me

This is a discussion on Program keeps crashing on me within the C Programming forums, part of the General Programming Boards category; I'm trying to write a program that prompts the user to enter the coefficients of a quadratic equation ax^2 + ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    6

    Program keeps crashing on me

    I'm trying to write a program that prompts the user to enter the coefficients of a quadratic equation ax^2 + bx + c. This is what I have written so far. The program compiles and runs, but after entering in any coefficients it gives me outputs of root 1.#INF00 and -1.#INF00? Can anyone help me understand why this is? Thanks a bunch. Also, it doesn't crash on me, sorry about that.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main(int argc, char **argv)
    {
    double a;
    double b;
    double c;
    double root01;
    double root02;
    double quadSqRt = (pow(b, 2.0) - 4.0 * a * c);
    
    
        printf("Please input the coefficient (a) of x^2 the quadtratic equation:\n ax^2 + bx + c\n");
        scanf("%lf", &a);
        printf("Please input the coefficient (b) of x the quadtratic equation:\n ax^2 + bx + c\n");
        scanf("%lf", &b);
        printf("Please input the constant (c) of the quadtratic equation:\n ax^2 + bx + c\n");
        scanf("%lf", &c);
    
    
        if (a != 0 && quadSqRt >= 0){
            root01 = (-b + pow(quadSqRt, 0.5)) / (2 * a);
            root02 = (-b - pow(quadSqRt, 0.5)) / (2 * a);
            } else if (a != 0){
            printf("Error: One or more root of the equation are imaginary.");
            } else if (a == 0){
            printf("Error: a cannot equal 0");
            } else {
            printf("Bad inputs, please enter again.");
        }
        printf("The roots are %f and %f", root01, root02);
    
    
        return 0;
    }
    Last edited by Mhesseltine; 01-22-2013 at 07:20 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,306
    First, it is always a good idea to tell us what input you gave the program that caused it to crash. Otherwise, we are only guessing from the infinite possibilities.

    Second, compile with your options turned all the way up. You should see a list similar to this. Fix all errors and warnings. They are indications of actual or potential problems.
    Code:
    $ make quad
    gcc -g -Wall  -o quad quad.c -lpthread -lm -lpq
    quad.c: In function ‘main’:
    quad.c:17:5: warning: format ‘%lf’ expects argument of type ‘double *’, but argument 2 has type ‘double’ [-Wformat]
    quad.c:19:5: warning: format ‘%lf’ expects argument of type ‘double *’, but argument 2 has type ‘double’ [-Wformat]
    quad.c:21:5: warning: format ‘%lf’ expects argument of type ‘double *’, but argument 2 has type ‘double’ [-Wformat]
    quad.c:13:27: warning: ‘b’ is used uninitialized in this function [-Wuninitialized]
    quad.c:13:42: warning: ‘a’ is used uninitialized in this function [-Wuninitialized]
    quad.c:13:46: warning: ‘c’ is used uninitialized in this function [-Wuninitialized]
    The %lf modifier for scanf expects a pointer to float (i.e. the address of a float variable). You need an & in front of a, b and c, when you pass them to scanf. That passes in the address of a, b and c, so scanf knows the location to store the values it reads. Without the &, you are passing in data of incorrect type (float, instead of pointer to float). scanf tries to treat that bogus data as a valid address, because it doesn't know better, and thus ends up accessing memory it shouldn't, causing a crash.
    stahta01 and Salem like this.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    double quadSqRt = (pow(b, 2.0) - 4.0 * a * c);

    The changing of the values a,b,and c does NOT change the value of quadSqRt.

    Tim S.
    Salem likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    Thanks I messed around with is some more and got it almost working right. It does what it is supposed to for most inputs but is wrong on a few for some reason I can't figure out. So far some of the ones I have found to be wrong are a=1 b=10 c=25 ; a=1 b=20 c=100 : both of which should give me two roots back that are the same, but instead it tells me that one or more of the roots are imaginary, which I told the program to do only if b^2 - 4*a*c is strictly less than 0, which it would not be in those cases. It gives me back roots for inputs like a=1 b=8 c=16, which is the same. Any idea why?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main(int argc, char **argv)
    {
    double a;
    double b;
    double c;
    double root01;
    double root02;
    double quadSqRt;
    
    
        printf("Please input the coefficient (a) of x^2 the quadtratic equation:\n ax^2 + bx + c\n");
        scanf("%lf", &a);
        printf("Please input the coefficient (b) of x the quadtratic equation:\n ax^2 + bx + c\n");
        scanf("%lf", &b);
        printf("Please input the constant (c) of the quadtratic equation:\n ax^2 + bx + c\n");
        scanf("%lf", &c);
    
    
    quadSqRt = (pow(b, 2.0) - (4.0 * a * c));
    
    
        if (a == 0){
            printf("Error: a cannot equal 0");
        }   else if (quadSqRt < 0.0){
            printf("Error: One or more roots of the equation are imaginary.");
        }   else {
            root01 = (b * -1 + sqrt(quadSqRt)) / (2 * a);
            root02 = (b * -1 - sqrt(quadSqRt)) / (2 * a);
            printf("The roots are %f and %f", root01, root02);
        }
    
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    Nevermind I got it working. I had to put the quadSqRt into and absolute value function because sometimes it was giving me quadSqRt = 0 ans some times quadSqRt = -0 for some reason.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,306
    Note, the value you call 'quadSqRt' has an official mathematical name, it's called the discriminant. It couldn't hurt to rename your variable. It wont fix any problems, but it will make your code more clear.

    Also, you can simply do -b instead of b * -1. The - operator can be applied to a variable as well as a number.

    The results you are claiming do not seem to match the code you posted:
    Code:
    $ make quad
    gcc -Wall -Wunreachable-code -ggdb3 -std=c99 -pedantic  -lm -lpthread  quad.c   -o quad
    
    
    
    $ ./quad
    Please input the coefficient (a) of x^2 the quadtratic equation:
     ax^2 + bx + c
    1
    Please input the coefficient (b) of x the quadtratic equation:
     ax^2 + bx + c
    10
    Please input the constant (c) of the quadtratic equation:
     ax^2 + bx + c
    25
    The roots are -5.000000 and -5.000000
    
    $ ./quad
    Please input the coefficient (a) of x^2 the quadtratic equation:
     ax^2 + bx + c
    1
    Please input the coefficient (b) of x the quadtratic equation:
     ax^2 + bx + c
    20
    Please input the constant (c) of the quadtratic equation:
     ax^2 + bx + c
    100
    The roots are -10.000000 and -10.000000
    Those results look good to me. Are you sure you saved all your most recent changes before compiling and testing?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,306
    Quote Originally Posted by Mhesseltine View Post
    Nevermind I got it working. I had to put the quadSqRt into and absolute value function because sometimes it was giving me quadSqRt = 0 ans some times quadSqRt = -0 for some reason.
    Nice catch! Would have taken me forever to figure that one out. You can disregard the second half of my last post.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    What Every Computer Scientist Should Know About Floating-Point Arithmetic

    You are getting a small value that should be zero but, it off because of rounding errors.
    You might try b*b instead of using the power function.

    Tim S.
    iMalc likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashing
    By AlexD in forum C Programming
    Replies: 2
    Last Post: 03-13-2012, 04:56 PM
  2. Program crashing
    By krinl in forum C Programming
    Replies: 5
    Last Post: 10-18-2011, 10:07 AM
  3. crashing program, help
    By xniinja in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 11:42 AM
  4. Program Crashing - please help!
    By Surfin_Bird in forum C Programming
    Replies: 6
    Last Post: 03-23-2005, 10:34 AM
  5. help plz crashing program
    By mill1000 in forum C++ Programming
    Replies: 3
    Last Post: 08-23-2002, 09:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21