a help

Can someone help me with this:

Problem:

The Babylonian algorithm to compute the square root of a number n is as follows:
1. Make a guess at the answer (you can pick n/2 as your initial guess).
2. Compute r = n / guess
3. Set guess = (guess +r ) / 2
4. Repeat steps 2 and 3 three more times. Note, the more that steps 2 and 3 are repeated, the closer guess will become to the square root of n . Complete the following program so that it inputs an integer for n , iterates through steps 2 and 3 as mentioned above, and outputs the answer as a double to two decimal places. Your answer will be most accurate for small values of n .

CodeMate Hints: Make guess a double using type casting, static_cast<double>(n)
Last edited on
Yes.
Here's some starting code:

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    std::cout << "Please input an integer: ";
    int n;
    while(!std::cin >> n)
    {
        std::cout << "Error: Integer expected.\n\n";
        std::cin.ignore(256,'\n');
    }
    // Here implement the algorithm for f(n)
    return 0;
}
This is a simple algorithm.
if you're having trouble with it, draw a flowchart for this algorithm and it can help you understand better.
but if you want us to do YOUR HOMEWORK, I'm afraid that's not possible.
It's not an assignment; i'm just taking some online lessons on C++ and faced this problem :)
If you wanna help, then help. If you don't want, then keep your silly comments to yourself :)
Thank you
Topic archived. No new replies allowed.