Square roots by approximate iterations

Click For Summary

Discussion Overview

The discussion revolves around constructing a C++ program to calculate the square root of a number using approximate values. Participants are focused on implementing loops for iterative approximation and handling decimal precision in the output.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant describes the initial steps of the program, including prompting the user for input and finding the next smaller and larger square roots.
  • There is a focus on using event-controlled loops to find approximations and how to manage iterations for decimal precision.
  • Another participant expresses difficulty with a specific loop intended to iterate through decimal values, indicating that the current implementation is compounding values incorrectly.
  • Participants discuss the need for a count-controlled loop that executes for each decimal position and how to adjust the approximation based on whether it is too small or too large.
  • There are detailed instructions provided on how the approximation should be incremented or decremented based on the calculated delta for each decimal position.

Areas of Agreement / Disagreement

There is no consensus on the implementation details, as participants are seeking help with specific coding issues and expressing uncertainty about the correct approach to achieve the desired output.

Contextual Notes

Participants have not resolved the specific coding issues related to the iteration and adjustment of the approximation values, indicating potential misunderstandings of loop control and variable management in C++.

noblepants
Messages
7
Reaction score
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
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.
 
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?
 
Code:
double precision = delta;
.
.
.
precision *= delta;

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

Similar threads

  • · Replies 6 ·
Replies
6
Views
6K
Replies
12
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 30 ·
2
Replies
30
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 18 ·
Replies
18
Views
5K
  • · Replies 1 ·
Replies
1
Views
12K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K