Quadratic Formula Program


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Quadratic Formula Program
# 1  
Old 02-07-2002
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  
Old 02-08-2002
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  
Old 02-22-2002
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 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Formula calculate scan rate

Hi There, Just curious here... If there formula to calculate scan rate on solaris? Thanks Edy (3 Replies)
Discussion started by: edydsuranta
3 Replies

2. Shell Programming and Scripting

A strange formula

Dear Masters, Kindly need your helps for this issue.. I face a formula as a picture... the input file is A DEPT Vp Vs rho VPperVS ------------------------ ------------ ------------ ------------ 700 151.9218 121.9269 ... (11 Replies)
Discussion started by: ipatah
11 Replies

3. Shell Programming and Scripting

Formula missing in excel after using perl

Hi all, I use "Spreadsheet::ParseExcel::SaveParser" to - read a existing excel file with : $Parser = Spreadsheet::ParseExcel::SaveParser->new(); $HeaderFile = $Parser->Parse("XLS_FILE_ACCESS"); - modify some values in somes cell with : $worksheet->AddCell($row_target,$col_max,... (3 Replies)
Discussion started by: Yom
3 Replies

4. Shell Programming and Scripting

formula missing in excel after using perl

Dear all, I got a template excel file which contains several worksheets. Each worksheet contains some formulas in the cells. I am using the perl script to read a CSV file and then put the data of CSV into template excel file(first worksheet) and then save it as new file name using ... (0 Replies)
Discussion started by: eldonlck
0 Replies

5. Shell Programming and Scripting

Convert Formula to Value

I'm new to Perl, but I need to use it to write into an excel spreadsheet. I want to copy the values of certain cells into a new sheet in the workbook. I'm unsure of how to do this because I don't know how to copy values instead of formulas. Note: by formula I mean '=SUM(A1,A2)' and by value I... (0 Replies)
Discussion started by: AgentSmith88
0 Replies

6. Shell Programming and Scripting

Formula to get combination… sum

Formula to get combination Lats say, I have 5 values ID Price 1 5 2 10 3 30 4 5 Resule with Possible combinations will be ID Price 1 5 2 10 3 30 4 5 1+1 10 1+2 15 1+3 35 1+4 10 (1 Reply)
Discussion started by: Amit.Sagpariya
1 Replies

7. Shell Programming and Scripting

how to create script for this formula?

Hello Unix gurus, Can anyone tell me the most efficient way to create script for the formula? Formula: Ans = 1 - ((Buffer pool data physical reads + Buffer pool xda physical reads + Buffer pool index physical reads + Buffer pool temporary data physical reads + Buffer pool temporary xda... (6 Replies)
Discussion started by: Rahulpict
6 Replies

8. UNIX for Dummies Questions & Answers

Formula help

The formula below will calculate a distance in miles between 2 points in excel. Can some put it to work in unix? Lat = 43.335 Lon1 = -70.9884 Lat2 = 43.4829 Lon2 = -71.246 distance... (8 Replies)
Discussion started by: bobo
8 Replies

9. Solaris

formula to get the swap space on a machine

Hello there are different opinions on how to get the swap space on Solaris. some say: swap -s and the space= used + available others say swap -l (donno how they get the swap size) other say 'top' command others say using format command (in print sub-command) Could you please advise on... (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

10. Shell Programming and Scripting

Help with formula in a script

I need some help. I been doing system admin for the last several years and gotten very rusty on scripting. I need to write a script that will take a log entry and do math using two columns of data to create a new column. I am taking the column(#2) that contains the time and the column(#5) that... (3 Replies)
Discussion started by: scottzx7rr
3 Replies
Login or Register to Ask a Question