![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A program to trace execution of another program | jiten_hegde | High Level Programming | 3 | 08-19-2008 06:26 AM |
| Formula help | bobo | UNIX for Dummies Questions & Answers | 8 | 03-29-2008 02:53 AM |
| Abacus Formula Compiler 1.0.1 (Default branch) | iBot | Software Releases - RSS News | 0 | 03-09-2008 06:20 AM |
| formula to get the swap space on a machine | melanie_pfefer | SUN Solaris | 1 | 03-03-2008 03:15 PM |
| Help with formula in a script | scottzx7rr | Shell Programming and Scripting | 3 | 08-15-2007 05:19 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Quadratic Formula Program
I have a question about the program I am making. I finished it, compiled it, and there were no errors. Now when I run it, the comand prompt comes up and there are just 1's and 0's that keep looping over and over again. The program is listed before. If anyone has any suggestions I would greatly appreciate it. Code:
/* *************************************************
Purpose: Solve for the roots of the quadratic equation,
y = a x^2 + b x + c
by means of the quadratic formula,
root = (-b +or- sqrt(b^2 -4. a c))/(2. a)
Variables: a, b, c + Real coefficients of quadratic equation.
d = Discriminant
root1, root2 = Real roots or real part of complex roots.
root1i,root2i = Imaginary part of complex roots.
Input: Values of coefficients in file 167w02cp1c.dat
Output: Tabulated values of coefficients and roots, with
proper heading and captions, in the UNIX xterm window.
************************************************** */
#include <stdio.h> /* Access input/output functions */
#include <math.h> /* Access math functions */
void main(void)
{
/* Float all variables */
double a, b, c, d, root1, root2;
double root1i,root2i;
FILE *fp;
fp = fopen ("167w02cp1c.dat","r"); /* Open input file */
if (fp==NULL) /* Test for file not open */
{ /* Terminate the program */
puts("\nData file 167w02cp1c.dat has failed to open.");
puts("\nProgram cp1.c is terminated.\n");
return;
}
puts("\n Class goes here");
puts("\n 2002");
puts("\n Name goes here");
puts("\n Problem No. 1");
puts("\n Roots of Quadratic Equations");
printf("\n a b c root1 ");
printf(" Root2");
printf("\n -------------------------------------------------");
printf(" ----------------------------------------");
while (! feof(fp)) /* Start of Loop */
{
fscanf (fp,"%1f%*c%1f%*c%1f",&a,&b,&c); /* Input Values */
if (feof(fp)) /* Looks for end of file */
{
puts("\n End of Data. Program cp1 complete,");
break;
}
if (a==0. || b==0. && c==0.) /* Check for invalid data */
{
printf("\n%10.3f%10.3f%10.3f invalid data",a,b,c);
}
else
d = ((b*b)-(4*a*c)); /* Compute the Discriminant */
/* Compute roots for when the discriminant is positive */
if (d>1.0*pow(10,-6))
{
root1 = ((-b)+pow(d,0.5))/(2*a);
root2 = ((-b)-pow(d,0.5))/(2*a);
printf("\n%10.3f%10.3f%10.3f%13.3f%13.3f",
a, b, c, root1, root2);
/* Print Values */
}
/* Compute roots for when the discriminant is negative */
else if (d<-1.0*pow(10,-6))
{
root1 = -b/(2.*a); root2 = root1;
root1i = sqrt(-d)/(2.*a);
root2i = -root1i;
printf("\n%10.3f%10.3f%10.3f%8.3f%8.3f%8.3f%8.3fi",
a, b, c, root1,root1i,root2,root2i); /* Print Values */
}
else
{
root1 = (-b)/(2*a);
root2 = root1;
printf("\n%10.3f%10.3f%10.3f%13.3f%13.3f",
a,b,c,root1,root2);
}
}
} /* End While Loop */
added code tags for readability --oombera Last edited by oombera; 02-20-2004 at 01:38 PM.. |
|
|||||
|
Good job everyone. This is an example of the posting doing his/her own work and being stuck on a tricky syntax or font issue. Posting homework problems are clearly frowned upon, but in this case considerable effort and work was put into the problem and the confusion between a "1" and an "I" was not really part of the assignment.
Perderabo did a stellar job in the way he answered this post ![]() |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|