how to solve this problem? pls help me guyss
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

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

  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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,020

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  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
    Dec 2012
    Location
    England
    Posts
    2,020

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,986

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

    What is numGuess?

  6. #6
    Join Date
    Apr 1999
    Posts
    27,418

    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

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,020

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,883

    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

  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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center