Hi,
I need to write a program that reads from an input file (input_equations.txt) the values of the numbers that will be inputed into a function to find the quadratic root of the 3 numbers.
For example, the input file could contain:
1 0 1
1 3 -4
2 -4 3
The program will then call the function, which in this case is called compute_roots, and in that function the memory location of root 1 and root 2 will be updated if the real-valued roots are present, otherwise the memory location will be left unchanged. The function will ultimately return 1 if the roots are real and 0 otherwise. The values for each of the roots from the input file then need to be printed into another file called roots.txt .
So far i have the following program... My problem is that i do not know how to update the memory locations or to write into an output file without overwriting what is already there from the previous calculations.
#include<stdio.h>
#include<math.h>
double compute_roots();
double *root1,*root2;
FILE *input_equations;
FILE *roots;
main()
{
double a,b,c;
double roota,rootb;
input_equations= fopen("input_equations.txt","r");
roots= fopen("roots.txt","w");
open = fscanf(input_equations,"%lf %lf %lf",&a,&b,&c);
while (open != EOF)
{
printf("Values Read: %f %f %f\n",a,b,c);
compute_roots(a,b,c);
open = fscanf(input_equations,"%lf %lf %lf",&a,&b,&c);
}
}
double compute_roots(double a,double b,double c)
{
double discriminant,rootone,roottwo;
discriminant = b*b - 4.0*a*c;
if (discriminant<0)
{
printf("Roots are Complex\n");
return 0;
}
else
{
rootone= (1/(2*a))*(-b-sqrt(pow(b,2)-4*a*c));
roottwo= (1/(2*a))*(-b+sqrt(pow(b,2)-4*a*c));
printf("ROOT 1: %f ROOT 2: %f\n",rootone,roottwo);
return 1;
}
}