# Thread: Program keeps crashing on me

1. ## 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("The roots are %f and %f", root01, root02);

return 0;
}```

2. 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
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.

3. 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.

4. 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;

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. 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. 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

ax^2 + bx + c
1
ax^2 + bx + c
10
ax^2 + bx + c
25
The roots are -5.000000 and -5.000000

ax^2 + bx + c
1
ax^2 + bx + c
20
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. Originally Posted by Mhesseltine
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. 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.