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.
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.:)
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?
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.
Re: how to solve this problem? pls help me guyss
Re: how to solve this problem? pls help me guyss
Quote:
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
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
Re: how to solve this problem? pls help me guyss
Quote:
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.
Quote:
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.
Quote:
Originally Posted by
amira kausar
The square root of a number, num,
The input parameter is num.
Quote:
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.
Quote:
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.
Quote:
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.
Quote:
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.
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 :)