Thread: Square root using iterative approach

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    5

    Square root using iterative approach

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    5

    The question

    Its more the question I dont understand than the coding

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    5

    and how would this be done then

    Is it possible to see an example of this?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hunterage2010
    Is it possible to see an example of this?
    Yes, but it is tough to give you an example without giving you the answer.

    Why don't you try using a for loop with an if statement?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by hunterage2010 View Post
    Is it possible to see an example of this?
    Do your own homework.

    Code:
    while(++x != sqrt(y)) x = rand();
    Last edited by abachler; 12-01-2009 at 04:36 AM.

  8. #8
    Registered User lattica's Avatar
    Join Date
    Aug 2008
    Location
    Spacelike Hyperplane
    Posts
    16
    Quote Originally Posted by hunterage2010 View Post
    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
    You could search mathworld for 'Square Root Algorithms.' There are a few methods described there for finding square roots.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    Code:
    x = x*x - 5 + x
    and try to apply the described in th earticle procedure to the received equation.

    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

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vart
    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
    I would caution against a more sophisticated interpretation of the question given that hunterage2010 has problems with implementing a simple loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    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.

  12. #12
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    and everyon bare in mind I.ve never touched programming before just the hello world program

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to calculate the square root
    By saszew in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 12:53 PM
  2. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 02:24 AM