CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    Apr 2013
    Posts
    2

    Lightbulb how to solve this problem? pls help me guyss

    Approximating the square root

    Implement the sqrt function. The square root of a number, num, can be approximated by repeatedly performing a calculation using the following formula :

    nextGuess = (lastGuess + (num / numGuess)) /2

    The initial guess can be any positive value (e.g., 1). This value will be the starting vale for lastGuess. If the difference between nextGuess and lastGuess is less than a very small number, such as 0.0001, you can claim that nextGuess is the approximated square root of num. If not, nextGuess becomes the lastGuess and the approximation process continues.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: how to solve this problem? pls help me guyss

    We'll help you - but not write the program for you as I'm guessing this is a homework assignment and that would be cheating. You'll need to ask the user to enter the number for which the square root is required, then set the necessary variables then perform a loop until the exit condition is met.

    If you try to code it and post your code here we'll provide advice.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Apr 2013
    Posts
    2

    Re: how to solve this problem? pls help me guyss

    but i'm stuck right now. could u pls give me a little bit hint how to solve this?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: how to solve this problem? pls help me guyss

    Quote Originally Posted by amira kausar View Post
    but i'm stuck right now. could u pls give me a little bit hint how to solve this?
    You don't "solve" writing a computer program. This isn't math or physics class.

    You have to produce a program by writing a plan on paper, and then translating this plan to a set of discrete steps. Those steps are in your case, must be written in C++. Once you do that, you run the program to see if it works. If it doesn't work then you debug your program to fix any issues.

    In other words, an assignment where you need to write a program requires you to do all of these things -- there is no "solve a computer program".

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: how to solve this problem? pls help me guyss

    Quote Originally Posted by amira kausar View Post
    but i'm stuck right now. could u pls give me a little bit hint how to solve this?
    Okay, some hints follow. You always start with requirements analysis.

    Quote Originally Posted by amira kausar View Post
    Implement the sqrt function.
    So, the task is to implement function sqrt. This implies you already have some experience in using/writing C/C++ functions.

    Quote Originally Posted by amira kausar View Post
    The square root of a number, num,
    The input parameter is num.
    Quote Originally Posted by amira kausar View Post
    can be approximated by repeatedly performing a calculation using the following formula :

    nextGuess = (lastGuess + (num / numGuess)) /2
    The main function body is a cycle, which body is the formula.

    Quote Originally Posted by amira kausar View Post
    The initial guess can be any positive value (e.g., 1). This value will be the starting vale for lastGuess.
    This is about variable's initial value. Hope you're familiar with variables and initialization values.

    Quote Originally Posted by amira kausar View Post
    If the difference between nextGuess and lastGuess is less than a very small number, such as 0.0001,
    This is about condition when to leave the cycle.

    Quote Originally Posted by amira kausar View Post
    you can claim that nextGuess is the approximated square root of num. If not, nextGuess becomes the lastGuess and the approximation process continues.
    The highlighted part is about additional assignment in the cycle body to be done prior to starting next iteration. The value to return is nextGuess.

    So, the main meta hint here is: Learn to understand your assignment. Dissect it to essential clauses. Identify variables, cycles, elementary operations, etc. which will build the "meat" of your program.

    Another hint: do this exercise as many times as you need to get it to a habit. Your habits amount to your skills. Gain good habits to build good skills.

    And please don't hesitate to rephrase your questions in case you think you're not understood. Communication skill is one of the most important skills in engineering disciplines.
    Last edited by Igor Vartanov; April 26th, 2013 at 03:21 AM.
    Best regards,
    Igor

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: how to solve this problem? pls help me guyss

    I've given you a hint. What are you stuck with? You have the formula. What code have you written so far? As I've said, we've not going to write the code for you so you'll need to produce something first that we can comment upon.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: how to solve this problem? pls help me guyss

    What is numGuess?

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: how to solve this problem? pls help me guyss

    What is numGuess?
    He's trying to use Newton method for solving numerical equations - but he's misquoted the formulae or written it down wrong. numGuess should be lastGuess.

    nextGuess = (lastGuess + (num / lastGuess)) / 2
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    May 2013
    Location
    hyderabad
    Posts
    3

    Re: how to solve this problem? pls help me guyss

    Hey Amira ,,
    That's really great Question But answer you have in it Think logically

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured