u could use a do while loop like this....
Code:
x = guess*guess - n;
do
{
if (x>0)
{
tooHigh = guess;
guess=(tooHigh + tooLow)/2;
times= times + 1;
}
else
{
tooLow = guess;
guess = (tooHigh + tooLow)/2
times= times + 1;
}
x = guess*guess - n;
}while( |x| > tol);
print times;
return guess;
and as u can see this wont work properly for checking the first guess...so u can strike out using do while loop
u can use a while loop like this
Code:
x = guess*guess - n;
while( |x| > tol)
{
if (x>0)
{
tooHigh = guess;
guess=(tooHigh + tooLow)/2;
times= times + 1;
}
else
{
tooLow = guess;
guess = (tooHigh + tooLow)/2
times= times + 1;
}
x = guess*guess - n;
}
print times;
return guess;
or a for loop like this....
Code:
for(x=guess*guess-n;|x| > tol;x=guess*guess-n)
{
if (x>0)
{
tooHigh = guess;
guess=(tooHigh + tooLow)/2;
times= times + 1;
}
else
{
tooLow = guess;
guess = (tooHigh + tooLow)/2
times= times + 1;
}
}
print times;
return guess;
but really u should go through the tutorials....thats the way u can learn how to use the functions to ur needs....