473,396 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

reading and writing into a file

Could somebody help me please!! I've tried to solve the problem but can't get it right.Here is the question:
Solve a quadratic equation using the coefficients in a file "coef.txt" .The file is as follows:
a b c
2 6 1
3 3 0
1 3 1
0 12 -3
3 6 3
2 -4 3
1) The coefficients are to be read in the main function
2) Get the the desired solutions in a function named quad(use the formula (- b + (sqrt(b*b-4*a*c)))/(2*a);)
3) The program should also be able to find complex numbers
real part=-b/2a
imaginary part=sqrt(4*a*c-b*b) i

3) Pass the coefficient and the the values of X1 and X2 in another function named calculated_quad
4) In that same function also,write the values in a file named "results.txt"

Here is what I've tried :

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<math.h>
  3.  
  4. double quad(int a,int b,int c);
  5. double quad2(int a,int b,int c);
  6. void calculated_quad(double X1,double X2,int a,int b,int c);
  7.  void main()
  8.  {
  9.   int a,b,c,i;
  10.   double X1,X2;
  11.   char A,B,C;
  12.   FILE*val;
  13.   val=fopen("coef.txt","r");
  14.   fscanf(val,"%s%s%s",&A,&B,&C);
  15.   for(i=0;i<6;i++)
  16.   {
  17.    fscanf(val,"%d%d%d",&a,&b,&c);
  18.  
  19.    X1=quad(a,b,c);
  20.    X2=quad2(a,b,c);
  21.    calculated_quad(X1,X2,a,b,c);
  22.  
  23.   }
  24.   return;
  25.  }
  26.  
  27.  
  28.  
  29.  
  30.  double quad(int a,int b,int c)
  31.  {
  32.    double x;
  33.    x=(-b + (sqrt(b*b-4*a*c)))/(2*a);
  34.  
  35.    return x;
  36.  }
  37.   double quad2(int a,int b,int c)
  38.   {
  39.    double x2;
  40.    x2=(-b - (sqrt(b*b-4*a*c)))/(2*a);
  41.  
  42.    return x2;
  43.   }
  44.  
  45.   void calculated_quad(double X1,double X2,int a,int b,int c)
  46.   {
  47.    double real,im,check;
  48.    FILE*out;
  49.    out=fopen("results.txt","w");
  50.    fprintf(out," a   b   c      X1      X2\n");
  51.    fprintf(out," -   -   -      --      --\n\n");
  52.    check=b*b - 4*a*c;
  53.    real=-b/(2*a);
  54.    im=sqrt(4*a*c-b*b);
  55.  
  56.  
  57.    if(check<0)
  58.  
  59.    fprintf(out,"%2d%4d%4d%9.2lf+%.2lfi%9.2lf-%.2lfi\n",a,b,c,real,im,real,im);
  60.  
  61.    else
  62.  
  63.    fprintf(out,"%2d%4d%4d%9.2lf%9.2lf\n",a,b,c,X1,X2);
  64.  
  65.    return;
  66.   }
Feb 4 '09 #1
4 1850
donbock
2,426 Expert 2GB
What's the problem?
> compiler error
> linker error
> run-time error
> wrong answer
> something else

Please describe the gap between what you expect and what you're getting.
Feb 4 '09 #2
newb16
687 512MB
@amateur073532
How are you handling non-quadratic, linear equations?
And why are all coefficients integer?
Btw, why did you decided to use "%9.2lf" for output?
Feb 4 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
What if the surd is negative resulting in complex roots?
Feb 4 '09 #4
never mine guys i've got the answers.Thanx a million...

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<math.h>
  3.  
  4.  
  5. void quad(int a,int b,int c,int i);
  6. void calculated_quad(int a,int b,int c,double X1,double X2);
  7.  
  8.  void main()
  9.  {
  10.      int a,b,c,i;
  11.      char A,B,C;
  12.      FILE*coef_val;
  13.      coef_val=fopen("coef.txt","r");
  14.      fscanf(coef_val,"%s%s%s",&A,&B,&C);
  15.      printf("---------------------------------------------------------------------\n");
  16.      printf("    This program finds the solution for quadratic equation only!!\n\n");
  17.      printf("---------------------------------------------------------------------\n");
  18.  
  19.      for(i=1;i<=6;i++)
  20.      {
  21.          fscanf(coef_val,"%d%d%d",&a,&b,&c);
  22.          printf("Equation%d : %d(x2) + (%d)x + (%d)\n\n",i,a,b,c);
  23.          quad(a,b,c,i);
  24.  
  25.      }
  26.      printf("All the solutions for the equations have been written to the file results.txt   Feel free to view them!\n");
  27.      return;
  28.  }
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. void quad(int a,int b,int c,int i)
  36. {
  37.      double x1,x2;
  38.      double real,im,check;
  39.      FILE*out;
  40.      out=fopen("results.txt","a");
  41.      check=b*b - 4*a*c;
  42.      if(i==1)
  43.      {
  44.          fprintf(out," a   b   c            X1            X2\n");
  45.          fprintf(out," -------------------------------------------\n");
  46.      }
  47.  
  48.  
  49.      if(a==0)
  50.      {
  51.  
  52.          fprintf(out,"%2d%4d%4d         undefined      undefined (#This is not a quadratic equation)\n",a,b,c);
  53.      }
  54.      else if(check<0)
  55.      {
  56.          real=(-b)/(2*a);
  57.          im=(sqrt(4*a*c-b*b))/(2*a);
  58.          calculated_quad(a,b,c,real,im);
  59.      }
  60.  
  61.      else
  62.      {
  63.          x1=(-b + (sqrt(b*b-4*a*c)))/(2*a);
  64.          x2=(-b - (sqrt(b*b-4*a*c)))/(2*a);
  65.          calculated_quad(a,b,c,x1,x2);
  66.      }
  67.  
  68.     return;
  69. }
  70.  
  71.  
  72.  
  73.  
  74.  
  75. void calculated_quad(int a,int b,int c,double X1,double X2)
  76. {
  77.     double check;
  78.     FILE*out;
  79.     out=fopen("results.txt","a");
  80.  
  81.     check=b*b - 4*a*c;
  82.  
  83.  
  84.     if(check<0)
  85.  
  86.         fprintf(out,"%2d%4d%4d%12.2lf+%.2lfi%10.2lf-%.2lfi\n",a,b,c,X1,X2,X1,X2);
  87.  
  88.     else
  89.  
  90.         fprintf(out,"%2d%4d%4d%15.2lf%15.2lf\n",a,b,c,X1,X2);
  91.  
  92.      return;
  93.  
  94. }
  95.  
  96.  
Feb 9 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
2
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
2
by: John Salerno | last post by:
I wrote this code just to experiment with writing to and reading from a file. It seems to work fine when writing, but when reading the file, it only prints the filepath to the screen, not the file...
7
by: utab | last post by:
Hi there, I am trying to read from a file and at the same time change certain fields of the same field, there are 6 fields in this file like 1 2 3 4 5 6...
8
by: smeenehan | last post by:
This is a bit of a peculiar problem. First off, this relates to Python Challenge #12, so if you are attempting those and have yet to finish #12, as there are potential spoilers here. I have five...
5
by: UJ | last post by:
I have a system that has five programs that all communicate with each other via Message Queues. Works well. One program is a watchdog that will make sure the others are up and going. Currently I...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
0
by: yogesh_anand | last post by:
Hi I am using sun solaris 5.9 operating system.Sometimes after reading from and while writing to file process size used to get increase.(it can be after 20th times 40 th time) I doesn't happen...
42
by: psbasha | last post by:
Hi, Is it necessary in Python to close the File after reading or writing the data to file?.While refering to Python material ,I saw some where mentioning that no need to close the file.Correct me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.