Square roots with precision

Hello all!

This is my first time using C++ and I have the homework assignment as follows, with an attempt below it.

**You are to write a program which will compute square roots to a desired degree of accuracy. Begin the program by prompting 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. Use loops that determine the next highest perfect square and the next lowest perfect square respectively. A count-controlled loop should then be constructed; it will execute once for each of the desired decimal positions;

**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 an event-controlled loop inside the count-controlled loop will increment the approximation until it becomes too big.

I am unsure as how to start constructing the loops for the process in paragraph two. I understand that its iteration the is the concern but not how.

Some one mind helping?

My progress so far:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

square_root()

{
   cout << setiosflags (ios::fixed|ios::showpoint);
  
    
   int number;
   cout<<"Enter the number to find the square root of: ";
      cin>>number;
   int sqRoot = 1;            
   while (sqRoot*sqRoot < number)   // sqRoot is too small
      sqRoot++;                     //  try the next number
  
   int y=0;
   int newRoot =1;
   while (y < number)
   {
      if (newRoot*newRoot > number )
         break;
      y=newRoot*newRoot;
      newRoot++;
   }
    
   
   int decimalInput;
   int decimalPosition= 0;
      cout<<"Enter the desired decimal position: "<<endl;
         cin>>decimalInput;
   while (decimalPosition < decimalInput)
      decimalPosition++;
      
   return 0;
    
}

Last edited on
First of all, you are using int which means your answer will be an integer (no decimal places).

Secondly, could you give any context on the question? The easiest way of determining a square root is using the sqrt() function using the cmath header, but it seems unlikely that this is what you are expected to do.
Initialize sqRoot and newRoot as float or double?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <iomanip>
using namespace std;
double square_root(double &number,int &decPlaces)
{
   double ret(0),lower(1),upper(1);            
   
   // Find the lower
   while (lower*lower < number)   // sqRoot is too small
      lower++;                    //  try the next number

   // Check to see if the lower is the sqarue root
   if ( lower*lower != number ) {
      upper = lower+1;
     
      // Don't think this is needed, because if (lower)^2 < number
      // and we have checked to see that (lower)^2 != number then
      // (lower+1)^2 must be greater then number
      while(upper*upper < number)
          upper++;

      double guess((upper+lower)/2.0),
             sqrAns(guess*guess),inc(1),
             diff(0);

      //  From you:  During a single pass of this 
      //    count-controlled loop, the approximation will be 
      //    either incremented or decremented by Δ = 1 / 10 
      //    decimalPosition until the actual value is passed.
      //
      //  From me:  Below is what (I think) you need to do.  Keep in mind
      //    that there are many ways to solve a problem. So if helps, use it.
      //
      // For the number of desired decimal positions+1 (count-controlled loop) 
      //   Set the increment
      //   do
      //     Is the guess high or low 
      //     If high substract increment (either incremented or decremented)
      //     If low add increment
      //     Check the new guess
      //   while we have not over/under estimated the root ( until the actual value is passed)
      // end of for
      ret = guess;
   }
   else {
      ret = lower;
   } 
   return ret;
}

int main(){
   double num;
   int    decimalInput;
   cout <<"Enter the number to find the square root of: ";
   cin  >> num;
   cout << "Enter the desired decimal position: "<< endl;
   cin  >> decimalInput;
   if ( decimalInput <= 14 && num > 0 ) {
      cout << "sqrt("<<num<<")  = " 
           << setiosflags (std::ios::fixed|std::ios::showpoint)
           << setprecision(decimalInput+1) 
           << square_root(num,decimalInput) << endl;
   }
   else {
     cout << "Error:  Desired decimal position(" 
          << decimalInput << ") was > 14 or number("
          << num << ") was <= 0" << endl;
   }
   return 0;
}

EDIT: Forgot to protect against negative number and zero. When you get this working add zero and negative number to give imaginary numbers as the square root.
Last edited on
Topic archived. No new replies allowed.