• Support PF! Buy your school textbooks, materials and every day products Here!

Square roots by approximate iterations

  • Thread starter noblepants
  • Start date
  • #1

Homework Statement



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:
#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: ";
    cin>>findRoot;
    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)
        bigRoot++;

    while (smallRoot*smallRoot < bigRoot)  
        smallRoot++;

    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;
            delta=delta*delta;
            
            cout<< delta<<"   "<< average<<"\n";
        }
    
    
    return 0;

}
 
Last edited:

Answers and Replies

  • #2
33,496
5,188

Homework Statement



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:
#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: ";
    cin>>findRoot;
    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)
        bigRoot++;

    while (smallRoot*smallRoot < bigRoot)  
        smallRoot++;

    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;
            delta=delta*delta;
            
            cout<< delta<<"   "<< average<<"\n";
        }
    
    
    return 0;

}
I think this line is your problem:
Code:
delta=delta*delta;
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.
 
  • #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?
 
  • #4
33,496
5,188
Code:
double precision = delta;
.
.
.
precision *= delta;
Or you could keep using delta, but not as a constant.
Code:
delta *= 0.1;
 

Related Threads on Square roots by approximate iterations

Replies
17
Views
11K
  • Last Post
Replies
4
Views
3K
Replies
51
Views
23K
Replies
3
Views
26K
Replies
7
Views
2K
  • Last Post
Replies
14
Views
745
  • Last Post
Replies
24
Views
3K
  • Last Post
Replies
2
Views
1K
Top