- #1

- 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 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: