简体   繁体   中英

Finding a square root through iteration in c++

I have a homework assignment due and I am having trouble with my loops. I first have to find the next highest and lowest square roots through loops which I already can do. Next my assignment tells me that I need to get an approximation of the square root which I do by averaging the next highest and lowest square roots of the integer. Then I must ask the user for the number of decimal places of accuracy they want. Here is a quote from the assignment:

A count-controlled loop should then be constructed; it will execute once for each of the desired decimal positions; in the example, this loop will execute four times (once each for the tenths, hundredths, thousandths, and ten-thousandths decimal positions). Use a counter such as decimalPosition to keep track of which pass the loop is on.

This is where I have trouble, I am using a while loop based on the number of decimal positions the user has entered but my loop doesn't complete the loop. I am new to programming so please forgive me if this is a truly simple. Here is my while code:

for (int decimalPosition = 1; decimalPosition <= decimal; decimalPosition++)
{
    while (baseRoot*baseRoot > num)
    {
        baseRoot = baseRoot - (pow((.1),decimalPosition));
        cout << fixed << setprecision(decimal) << baseRoot << endl;
    }

}

here is my output so far

Enter a number you wish to know the square root of: 8
Enter the number of decimal places of accuracy you want: 7
Find the square root of 8 to 7 decimal places: 
2.6000000
2.7000000
2.8000000
2.9000000
2.9000000 square root of 8.0000000

It's called Newton's method , and its convergence is quadratic. That should help you figure it out.

PS - The Babylonians discovered it first, but Newton gets credit for it.

To make your loop works, you can add a statement

baseRoot = baseRoot + (pow((.1),decimalPosition));

after the while loop because your need to make sure baseRoot is bigger than the answer before each iteration. Like this:

for (int decimalPosition = 1; decimalPosition <= decimal; decimalPosition++)
{
    while (baseRoot*baseRoot > num)
    {
        baseRoot = baseRoot - (pow((.1),decimalPosition));
        cout << fixed << setprecision(decimal) << baseRoot << endl;
    }
    baseRoot = baseRoot + (pow((.1),decimalPosition));
}

Now you can get the answer 2.8284271 .

By the way, there's another efficient way called bisection method (similar to binary search) to solve this kind of problem (related to Monotonic function ) without too much math:

double mySqrt(double x, double epsilon) {
    double left = 0, right = x;
    while (right - left > epsilon) {
        double mid = (left + right) / 2;
        if (mid * mid > x) {
            right = mid;
        } else {
            left = mid;
        }
    }
    return left;
}

It's simple, stupid :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM