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

1. ## 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);

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

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

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

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

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

6. 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;
}```

7. 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. try to remove path from defines and 5 from &#37;5g (which is strange for me....)

What compiler are you using?

9. gcc? I dont really know much about the compiler itself. I just use gcc -lm in this case.

10. If you're on Linux, then those paths are wrong; so change them to whatever the paths should be.

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

Popular pages Recent additions