1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Square roots by approximate iterations

  1. Sep 18, 2012 #1
    1. The problem statement, all variables and given/known data

    hi every one

    I need to construct a C++ square root program that uses approximate values I've done the first part of the work;

    prompt the user for two numbers: (1) the value for which to determine the square root and (2) the number of decimal places of accuracy desired in the result. For example, find the square root of 7 to four decimal places; the actual value is 2.6458.
    The first computational step is to use event-controlled loops to find the next smaller and next larger square roots as discussed in this week's in-lab exercises. The average of these two values is then used as the first approximation of the desired square root. Continuing the example, the next smaller and next larger square roots are 2 and 3, respectively, as their squares, 4 and 9, bracket 7; consequently, the first approximation is (2 + 3) / 2 = 2.5, not far from the actual value in this case.
    But my third loop which is meant to iterate the decimal values and print out each time does not work. I know that it is because I am compounding the values but I cant figure out exactly the right way to get the correct output. heres the instruction:

    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.
    During a single pass of this count-controlled loop, the approximation will be either incremented or decremented by Δ = 1 / 10decimalPosition until the actual value is passed. On the first pass of the loop, Δ will be 1 / 101 = 0.1; on the second pass, Δ will be 1 / 102 = 0.01, and so on.
    If the approximation is too small, as in the example, an event-controlled loop inside the count-controlled loop will increment the approximation until it becomes too big; in the example, during the first pass of the count-controlled loop, 2.5 is incremented to 2.6 and then to 2.7, at which point it can be determined that 2.7 is too large. Making this decision is done by simply squaring the approximation and comparing to the original value. In the example, 2.5*2.5 = 6.25, which is less than 7; 2.6*2.6 = 6.76, which is also too small; finally, 2.7*2.7 = 7.29, which is too large. It is now clear that the square root for 7 must lie between 2.6 and 2.7.
    In the example, the second pass of the count-controlled loop will start with an approximation of 2.7, which has already been seen to be too large. The count-controlled loop's Δ is now 0.01; a second event-controlled loop is now needed to decrement the approximation until it becomes too small. In the example, the approximation 2.7 is decremented to 2.69, then 2.68, and so on down to 2.63 at which point it has become too small again.


    Can someone help?

    here is my code

    Code (Text):

    #include <iostream>
    #include <iomanip>
    using namespace std;

    int main()

        double findRoot, decimal, delta= (1.0/10.0);
        int bigRoot=1, smallRoot=1;
        cout<<"Enter number to find root: ";
        cout<<"Enter a number for the number of decimal points to use: ";
        cin>> decimal;
        if (decimal >14 || decimal <0)
            cout<<"Number must be larger than 0 and less than 14";    

        while(bigRoot*bigRoot < findRoot)

        while (smallRoot*smallRoot < bigRoot)  

        cout<<smallRoot<< "\n";// for testing
        cout<<bigRoot<< "\n"; // for testing
        double average= ( (smallRoot + bigRoot) / 2.0 ) ; //""
        cout<< average<<"\n";
        for (int position = 0; position <= decimal; position++) //count control for decimal places
                average= delta+average;
                cout<< delta<<"   "<< average<<"\n";
        return 0;

    Last edited: Sep 18, 2012
  2. jcsd
  3. Sep 18, 2012 #2


    Staff: Mentor

    I think this line is your problem:
    Code (Text):
    The values aren't doing what you want.
    At start, delta == .1.
    After one iteration, delta == .01 (OK)
    After two iterations, delta == .0001 (not OK)
    After three itereations, delta == .000001 (not OK)

    To fix this, consider delta to be a constant, and use another variable for the decimal place.

    For another thing, your for loop will run too many times. For example, if decimal is 4, the loop will run 5 times as position takes on the values 0, 1, 2, 3, and 4.
  4. Sep 18, 2012 #3
    1 recognize that as the problem, I am compounding the values, but how do i increment the decimal value

    Thanks for catching the count error

    lets say

    const delta= .1

    but what do I want in the loop to make delta go .01 , .001 .0001 .00001 etc. ?

    I just don't understand what would construct that operation?
  5. Sep 18, 2012 #4


    Staff: Mentor

    Code (Text):
    double precision = delta;
    precision *= delta;
    Or you could keep using delta, but not as a constant.
    Code (Text):
    delta *= 0.1;
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook