• 03-09-2008
mrusnak
Hey I am new to the whole C programming and I am trying to write simple code that computes the roots of the quadratic equation and prints the coefficients and the roots into a seperate text file, roots.txt. The coefficients are found in a text file input_equations.txt. Here is my text file

1 -3 2
1 0 -4.84
1 0 1

And here is my program, I am getting wacky numbers in the roots.txt file and I cant seem to work it out.

Code:

```#include <stdio.h> #include <stdlib.h> #include <math.h> int compute_roots(double a, double b, double c, double *root1, double *root2) {     double disc;     disc = pow(b,2) - (4.0*a*c);             if (disc < 0)     {         return (0);     }         if (disc >= 0)     {         *root1 = (-1.0*b+sqrt(disc))/(2.0*a);         *root2 = (-1.0*b-sqrt(disc))/(2.0*a);         return (1);       } } int main() {     double a,b,c,root1,root2;             FILE *inp, outp;     inp = fopen("input_equations.txt", "r");     outp = fopen("roots.txt", "w");                             while (fscanf(inp, "%f %f %f", &a, &b, &c) != EOF)            {         fprintf(outp, "%f %f %f ", a, b, c);         if(compute_roots(a, b, c, &root1, &root2) == 1)                         fprintf(outp, "root1 = %g root2 = %g\n", root1, root2);         else                 fprintf(outp, "The roots are not real numbers.\n");     }             fclose(inp);     fclose(outp);             return(0); }```
It compiles fine, but the output into the roots.txt file is not giving the right coefficients. Any help would be great, I just can't seem to find the bug! Thanks alot!
• 03-09-2008
mike_g
Code:

`fprintf(outp, "root1 = &#37;g root2 = %g\n", root1, root2);`
Well tbh I dont know exactly what %g does in fprintf but i guess its specifying doubles to print out. Here:
Code:

`fprintf(outp, "%f %f %f ", a, b, c);`
you are outputting doubles as floats. As this is the only real difference I can see, it might work if you change the line to:
Code:

`fprintf(outp, "%g %g %g ", a, b, c);`
Edit: Wikipedia says:
Quote:

'%f' : Scan a floating-point number in normal (fixed-point) notation.
'%g', '%G' : Scan a floating-point number in either normal or exponential notation. '%g' uses lower-case letters and '%G' uses upper-case.
• 03-09-2008
tabstop
mike_g is on the right track, but he took the left turn at Albuquerque. The problem is not with printf, but with scanf: in order to read in values to a double variable, you need to use the %lf specifier.
• 03-09-2008
mrusnak
well i changed the %f to %lf (i cant believe i did that by the way) but the numbers are still coming out weird. I think maybe because it is scanning for doubles, but some of the numbers are ints. Could that be a problem? And how would I fix it so that it wouldnt matter what kind of number I am scanning for. And now when the roots are printed out into the output file, numbers are no longer printed but rather NaN or Inf is printed. Im pretty confused about it. I appreciate any help.
• 03-09-2008
mrusnak
Hey i figured it out tabstop and mike_g. Thanks for all the help!
• 03-09-2008
tabstop
Quote:

Originally Posted by mrusnak
well i changed the %f to %lf (i cant believe i did that by the way) but the numbers are still coming out weird. I think maybe because it is scanning for doubles, but some of the numbers are ints. Could that be a problem? And how would I fix it so that it wouldnt matter what kind of number I am scanning for. And now when the roots are printed out into the output file, numbers are no longer printed but rather NaN or Inf is printed. Im pretty confused about it. I appreciate any help.

Lack of decimal points doesn't matter. You have to scan for whatever number your variable is supposed to hold. My output (after changing outp from a FILE to a FILE *):
Quote:

1.000000 -3.000000 2.000000 root1 = 2 root2 = 1
1.000000 0.000000 -4.840000 root1 = 2.2 root2 = -2.2
1.000000 0.000000 1.000000 The roots are not real numbers.
• 03-09-2008