Could someone please show me what code would be used to find the square root of a number using an iterative approach, using guesses and incrementation
Could someone please show me what code would be used to find the square root of a number using an iterative approach, using guesses and incrementation
What have you tried?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Its more the question I dont understand than the coding
Well, it sounds like you are supposed to loop over the non-negative integers in order, square each of them, until you find the integer whose square is not less than the given number. This integer will then be either the square root, or the smallest integer greater than the square root of the given number.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Is it possible to see an example of this?
Yes, but it is tough to give you an example without giving you the answer.Originally Posted by hunterage2010
Why don't you try using a for loop with an if statement?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Do your own homework.
Code:while(++x != sqrt(y)) x = rand();
Last edited by abachler; 12-01-2009 at 04:36 AM.
it does not sounds as "iteration method"
Iterative method - Wikipedia, the free encyclopedia
I would think You need to represent the required solution in the form x = f(x) (for example if we need to find sqare root of 5 the reqired x will be root of the equation
and try to apply the described in th earticle procedure to the received equation.Code:x = x*x - 5 + x
Of cource we need to be sure that the attraction condition Attractor - Wikipedia, the free encyclopedia
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
I would caution against a more sophisticated interpretation of the question given that hunterage2010 has problems with implementing a simple loop.Originally Posted by vart
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I'm also not allowed to use the sqrt() function. All I wanna know is do I:
print "Enter a number you wish to find the square root of"
scan the number like 15 for instance
print the guess of what might make 15
compare the guess to the answer of 15
if the guess is too low or too high
increment or decrement by a number say 0.1 or something not sure till the answer is there ot there about.
and everyon bare in mind I.ve never touched programming before just the hello world program
Well, is your input and approximation of the square root supposed to be integers? For example, in the case of 15, what would be your output?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)