Quadratic equation with input from another file - help please

This is a discussion on Quadratic equation with input from another file - help please within the C Programming forums, part of the General Programming Boards category; Hey I am new to the whole C programming and I am trying to write simple code that computes the ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    5

    Quadratic equation with input from another file - help please

    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!

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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:
    '%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.
    Last edited by mike_g; 03-09-2008 at 10:31 AM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    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.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    Hey i figured it out tabstop and mike_g. Thanks for all the help!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by mrusnak View Post
    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 *):
    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.

  7. #7
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    You program works for me. I copied the code above and all I changed was &#37;f's in scanf to %lf's and FILE *inp, outp; to FILE *in, *outp;
    [edit] nvm [/edit]
    Don't quote me on that... ...seriously

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    So what was the final working code? I fail to see the errors.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    mrusnak you still around?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  4. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

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