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

• 03-12-2008
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);

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

• 03-13-2008
vart
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
• 03-13-2008
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

1 -3 2 root1=2 root2=1
1 0 -4.84 root1=2.2 root2=-2.2
1 0 1 The roots are complex
• 03-13-2008
vart
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

-------------
• 03-13-2008
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?
• 03-13-2008
vart
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);

}
return 0;
}

• 03-13-2008
Tried compiling that, got:

Code:

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

• 03-13-2008
vart
try to remove path from defines and 5 from &#37;5g (which is strange for me....)

What compiler are you using?
• 03-13-2008