
April 25th, 2013, 09:18 AM
#1
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.

April 25th, 2013, 09:24 AM
#2
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 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/
C, C++ Compiler: Microsoft VS2017

April 25th, 2013, 09:30 AM
#3
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?

April 25th, 2013, 09:38 AM
#4
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 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/
C, C++ Compiler: Microsoft VS2017

April 25th, 2013, 10:06 AM
#5
Re: how to solve this problem? pls help me guyss

April 25th, 2013, 10:33 AM
#6
Re: how to solve this problem? pls help me guyss
Originally Posted by amira kausar
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

April 25th, 2013, 11:00 AM
#7
Re: how to solve this problem? pls help me guyss
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 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/
C, C++ Compiler: Microsoft VS2017

April 26th, 2013, 03:02 AM
#8
Re: how to solve this problem? pls help me guyss
Originally Posted by amira kausar
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.
Originally Posted by amira kausar
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.
Originally Posted by amira kausar
The square root of a number, num,
The input parameter is num.
Originally Posted by amira kausar
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.
Originally Posted by amira kausar
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.
Originally Posted by amira kausar
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.
Originally Posted by amira kausar
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

May 3rd, 2013, 09:24 AM
#9
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

Forum Rules

Click Here to Expand Forum to Full Width
This a Codeguru.com survey!
