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;
	}
	
}

My problem is that i do not know how to update the memory locations

It sounds like your instructor wants you to use global variables for rootone and roottwo. Check with him to see if that's true.

to write into an output file without overwriting what is already there from the previous calculations.

Open the output file for append. Use "a" instead of "w"

Thanks for the reply.
I managed to get the file printing to work, however i still cannot update the memory locations pointed to root1 and root2 with the two values of the quadratic root. Is there a specific way of writing an equation to update the memory locations?

Thanks for the reply.
I managed to get the file printing to work, however i still cannot update the memory locations pointed to root1 and root2 with the two values of the quadratic root. Is there a specific way of writing an equation to update the memory locations?

What memory locations does rootone and roottwo point to? They look like simple variables to me. And simply assigning a value with the = sign updates the variable, and that you are doing. Therefore, I guess I have no idea what your question really is.

So, I suppose the question I have is "what part of the first half of my response did you not understand?"

spec80, ignore walt. He's apparently got some hostility issues with people who have honest questions. nice quality for a moderator.

anyhow, don't panic. you're 99% done.

instead of locally calling "rootone" and "roottwo" inside your subroutine, use the global variables that you already declared outside of main() ... "root1" and "root2"

and since you're going to use them as global variables (which is the case, because you declared them outside of main), there's no need to declare them as pointers. just make them straight-up type doubles.

then in the subroutine, instead of assigning the results of your calculations to "rootone" and "roottwo", assign them directly to the globals "root1" and "root2".

when you return back to the main( ) routine, the roots will still be in those global variables, ready for you to do whatever you want with them ... how nice, isn't it! aren't globals wonderful? enjoy them now, while you can.... they'll be violently beaten from you soon enough!

one other thing. theres no reason to make your "compute_roots" subroutine declared as a type "double". you're only returning a 1 or a 0... declare it as an "int", or "unsigned"

commented: Don't start a flame with me. They were correct observations. -2

thats fine walt. you can win the point game, and you can continue with your smug self-righteousness while berating new people for asking question.

>nice quality for a moderator.
Moderators have one job: enforce the rules. Walt's behavior is no more or less restricted than your behavior, and the comment that offends you so much is acceptable according to Daniweb's rules.

ok, sure. you're the boss.

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.