Square roots by approximate iterations

In summary: 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
  • #1
noblepants
7
0

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 can't figure out exactly the right way to get the correct output. here's 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:
Physics news on Phys.org
  • #2
noblepants said:

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 can't figure out exactly the right way to get the correct output. here's 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
Code:
double precision = delta;
.
.
.
precision *= delta;

Or you could keep using delta, but not as a constant.
Code:
delta *= 0.1;
 
  • #5


I can see that you have made a good start on your code. However, there are a few issues that need to be addressed to make your program work correctly.

Firstly, in your code you have used the variable "delta" to represent both the change in the approximation and the square of the change. This can lead to errors in your calculations. I would suggest using two different variables to represent these values.

Secondly, in your for loop, you are using the wrong formula to update your approximation. Instead of "average = delta + average" it should be "average = average + delta". This will ensure that your approximation is being incremented correctly.

Lastly, in the event-controlled loop inside the count-controlled loop, you are using the wrong formula to update your approximation. Instead of "average = delta + average", it should be "average = average - delta". This will ensure that your approximation is being decremented correctly.

I would also suggest adding some comments to your code to explain each step and make it easier to follow. Additionally, you may want to consider adding some error handling for cases where the user enters a negative number or a number larger than 14 for the decimal places.

Overall, your approach using approximate iterations is a valid and efficient way to find square roots. With these adjustments, I believe your program should work correctly. Good luck!
 

FAQ: Square roots by approximate iterations

What is the concept of "Square roots by approximate iterations"?

"Square roots by approximate iterations" is a method used to estimate the square root of a number by repeatedly making a guess and refining it until the desired accuracy is achieved. It is based on the principle that the square root of a number lies between its two consecutive perfect square roots.

How does the method of "Square roots by approximate iterations" work?

The method starts by making an initial guess for the square root of the given number. This guess is then squared and compared to the original number. If the squared value is close to the original number, the initial guess is accepted as the square root. If not, the guess is refined by taking the average of the original guess and the original number divided by the guess. This process is repeated until the desired level of accuracy is achieved.

What are the advantages of using "Square roots by approximate iterations" over other methods?

This method is relatively simple and does not require extensive mathematical knowledge or the use of complex formulas. It also allows for a flexible level of accuracy, as the number of iterations can be adjusted to achieve the desired precision. Additionally, it can be easily implemented in computer programs and calculators.

What are the limitations of using "Square roots by approximate iterations"?

This method may not always give an exact solution, as it relies on approximations. The accuracy of the estimate also depends on the initial guess made. It may also require more iterations for larger numbers, which can be time-consuming.

How is "Square roots by approximate iterations" used in practical applications?

This method is commonly used in engineering and scientific calculations where an approximate solution is acceptable. It can also be used for quick mental calculations or to check the accuracy of other methods. Additionally, it is used in digital signal processing and data compression algorithms.

Similar threads

Replies
5
Views
2K
Replies
6
Views
5K
Replies
30
Views
5K
Replies
1
Views
1K
Replies
18
Views
4K
Replies
1
Views
12K
Replies
10
Views
3K
Back
Top