Hi, I'm working through Programming in C by Stephen G. Kochan and am at Chapter 8: Working with Functions. Exercise 8.4 has me add a printf statement to show the value of a float variable called 'guess' each time the while loop iterates.
If I put the printf function below the calculation in the while loop all is good. When I experimented and added a second printf statement above the calculation I get the never-ending print.
This was curious enough to post, as I wouldn't have thought a printf statement could impact anything to do with the actual while termination point.
Thanking you in advance.
Paul.
here goes with the tags
Code:
float squareRoot (float x)
{
const float epsilon = 0.00001;
float guess = 1.0;
while (absoluteValue(guess * guess - x) >= epsilon)
//take away the comment and the while loop goes forever.
//printf("guess pre = %.2f\n", guess);
guess = ( x / guess + guess ) / 2.0;
printf("guess post = %.2f\n", guess);
return guess;
}