Program to to compute roots of a quadratic equation (HELP PLEASE!)

This is a discussion on Program to to compute roots of a quadratic equation (HELP PLEASE!) within the C Programming forums, part of the General Programming Boards category; Hey all. New problem. Been working on this one though during TA hours. Here's the problem (#1): http://ece.arizona.edu/~ece175/assig...signment06.pdf Ignore #2. ...

  1. #1
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31

    Program to to compute roots of a quadratic equation (HELP PLEASE!)

    Hey all. New problem. Been working on this one though during TA hours. Here's the problem (#1):

    http://ece.arizona.edu/~ece175/assig...signment06.pdf

    Ignore #2. I have a separate thread for that one.

    Now here's my program:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define INFILE "input_equations.txt"
    #define OUTFILE "roots.txt"
    
    int compute_roots(float x2,float x,float term,double *root1,double *root2)
    {
    float t1;
    t1=x*x-4*x2*term;
    if (t1<0) return 0;
    else if (t1==0) *root1=*root2=-1*x/(2*x2);
    else
    {
    *root1=(-1*x+sqrt(t1))/(2*x2);
    *root2=(-1*x-sqrt(t1))/(2*x2);
    }
    return 1;
    
    }
    int main(int argc, char* argv[])
    {
    float a,b,c;
    double r1,r2;
    FILE *ifp,*ofp;
    if ((ifp=fopen(INFILE,"r"))!=NULL)
    {
    ofp=fopen(OUTFILE,"w");
    
    
    while (!feof(ifp))
    {
    fscanf(ifp,"&#37;f %f %f",&a,&b,&c);
    if (compute_roots(a,b,c,&r1,&r2))
    fprintf(ofp,"%g %g %g root1=%g root2=%g\n",a,b,c,r1,r2);
    else fprintf(ofp,"%g %g %g The roots are complex\n",a,b,c);
    }
    fclose(ifp);
    fclose(ofp);
    
    }
    else puts("ERROR:FILE NOT FOUND");
    return 0;
    }
    My problem is, I messed up the formatting somewhere it seems. It compiles fine and runs almost as it should. But here's what my roots.txt looks like after the program has run:

    1 -3 2 root1=2 root2=1
    1 0 -4.84 root1=2.2 root2=-2.2
    1 0 1 The roots are complex
    1 0 1 The roots are complex

    Not only is the spacing not organized as it should be, but the 1 0 1 line is recorded into the file twice, thus two "The roots are complex" lines...

    Can someone please help me!

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    while (!feof(ifp)) - do not use foef to control a loop - read faq

    should be

    Code:
    while (fscanf(ifp,"&#37;f %f %f",&a,&b,&c) == 3)
    {
    Thus you will avoid wrong double processing of the last line

    To organize spacing - use width modifiers of the format

    And try to indent your code
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    Ok vart, that eliminated the double processing, but it also eliminated the first line.

    So the output was

    1 0 -4.84 root1=2.2 root2=-2.2
    1 0 1 The roots are complex

    Instead of

    1 -3 2 root1=2 root2=1
    1 0 -4.84 root1=2.2 root2=-2.2
    1 0 1 The roots are complex

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    I do not know
    Here is my input
    ----------
    1 -3 2
    1 0 -4.84
    1 0 1
    ----------------
    And here is the output

    ---------
    1 -3 2 root1=2 root2=1
    1 0 -4.84 root1=2.2 root2=-2.2
    1 0 1 The roots are complex
    ------------

    And Here is the output for &#37;5g format:
    -----------
    Code:
        1    -3     2 root1=    2 root2=    1
        1     0 -4.84 root1=  2.2 root2= -2.2
        1     0     1 The roots are complex
    -------------
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    Yeah I'm still getting the messed up output (missing line). Could you maybe post the whole code so I can see if anything else is different?
    Last edited by toadkiwi; 03-13-2008 at 10:20 AM.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define INFILE "c:\\input.txt"
    #define OUTFILE "c:\\roots.txt"
    
    int compute_roots(float x2,float x,float term,double *root1,double *root2)
    {
    	float t1;
    	t1=x*x-4*x2*term;
    	if (t1<0) return 0;
    	else if (t1==0) *root1=*root2=-1*x/(2*x2);
    	else
    	{
    		*root1=(-1*x+sqrt(t1))/(2*x2);
    		*root2=(-1*x-sqrt(t1))/(2*x2);
    	}
    	return 1;
    	
    }
    int main(int argc, char* argv[])
    {
    	float a,b,c;
    	double r1,r2;
    	FILE *ifp,*ofp;
    	if ((ifp=fopen(INFILE,"r"))!=NULL)
    	{
    		ofp=fopen(OUTFILE,"w");
    		
    		
    		while (fscanf(ifp,"&#37;f %f %f",&a,&b,&c) == 3)
    		{
    			if (compute_roots(a,b,c,&r1,&r2))
    				fprintf(ofp,"%5g %5g %5g root1=%5g root2=%5g\n",a,b,c,r1,r2);
    			else fprintf(ofp,"%5g %5g %5g The roots are complex\n",a,b,c);
    		}
    		fclose(ifp);
    		fclose(ofp);
    		
    	}
    	else puts("ERROR:FILE NOT FOUND");
    	return 0;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    Tried compiling that, got:

    Code:
    quadratic-roots.c: In function `main':
    quadratic-roots.c:34: error: missing terminating " character
    quadratic-roots.c:35: error: `root2' undeclared (first use in this function)
    quadratic-roots.c:35: error: (Each undeclared identifier is reported only once
    quadratic-roots.c:35: error: for each function it appears in.)
    quadratic-roots.c:35: error: syntax error before '&#37;' token
    quadratic-roots.c:35:8: invalid suffix "g" on integer constant
    quadratic-roots.c:35: error: stray '\' in program
    quadratic-roots.c:35: error: missing terminating " character
    quadratic-roots.c:36: error: missing terminating " character
    quadratic-roots.c:37: error: stray '\' in program
    quadratic-roots.c:37: error: missing terminating " character

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    try to remove path from defines and 5 from &#37;5g (which is strange for me....)

    What compiler are you using?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    gcc? I dont really know much about the compiler itself. I just use gcc -lm in this case.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you're on Linux, then those paths are wrong; so change them to whatever the paths should be.

  11. #11
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    Alright so I changed what vart said to, and I fixed some of the line errors. One problem that happened a few times was the line breaks caused the program to get wacky. But here's what it looks like now, I just tested it and it seems all good.

    *EDIT* It seems it was my own fault for the errors. I screwed up the line breaks by accident and that threw everything off.
    Last edited by toadkiwi; 03-13-2008 at 11:06 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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