The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-07-2002
zeek zeek is offline
Registered User
  
 

Join Date: Feb 2002
Posts: 1
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..
  #2 (permalink)  
Old 02-08-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,127
Re: Quadratic Formula Program

We are not allowed to do your schoolwork for you. But you have one nasty hard-to-find bug that may not be your fault:
Quote:
Originally posted by zeek
fscanf (fp,"%1f%*c%1f%*c%1f",&a,&b,&c);
That can't be right. I think those one's should be letter l's. A letter-l says that a, b, and c are doubles, which they are. A one says that field is a single character wide.

Always make sure that you are using a font that lets you see the difference between 1 and l. Ditto for O and 0.
  #3 (permalink)  
Old 02-22-2002
Neo's Avatar
Neo Neo is online now Forum Staff  
Administrator
  
 

Join Date: Sep 2000
Location: Asia Pacific
Posts: 6,781
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:03 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0