Square Root Function

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
#include <iostream>

double squareRoot(double number)
{
   const double ACCURACY = .0000001;
   double lower, upper, guess;

   if (number < 1)
   {
      lower = number;
      upper = 1;
   }
   else
   {
      lower = 1;
      upper = number;
   }
   while ((upper - lower) > ACCURACY)
   {
      guess = (upper + lower) / 2;
      if (guess*guess > number)
         upper = guess;
      else
         lower = guess;
   }
   return (lower + upper) / 2;
}
int main(){
   double value;

   while (value != 0.0 || value < 0.0)
   {
      do {
         cout << "Enter a number from 1 to 1000: ";
         cin >> value;
         if (value < 0.0 || value > 1000.0)
            cout << "Number is not in proper range!" << endl;
         else 
            cout << "Square root of " << value << " is " << squareRoot(value) << endl;
      } while (value < 0.0 || value > 1000.0);
      cout << "Enter a number from 1 to 1000: " << endl;
      cin >> value;
      cout << "Square root of " << value << " is " << squareRoot(value) << endl;
   }
   cout << "Loop exiting! Goodbye!" << endl;


   return 0;
}


Output:
Enter a number from 1 to 1000: 200.0
Square root of 200 is 14.1421
Enter a number from 1 to 1000:
-6.0
Square root of -6 is -6 //why is it doing this, it's supposed to display "Value not in range"
Enter a number from 1 to 1000: 2000.0
Number is not in proper range!
Square root of 0 is 2.98023e-08 //why is it trying to calculate this, supposed to output Loop exiting! Goodbye! when it encounters a zero
Enter a number from 1 to 1000:
Square root of 0 is 2.98023e-08
Loop exiting! Goodbye!
Last edited on
1
2
3
4
5
6
7
8
float sqrt(int i, int cycle){
int guess = 1;
while(cycle != 0){
guess = (guess + (i / guess ) ) / 2; 
cycle --; 
}
return guess; 
}

the more cycles you run the more accurate the square root is.
Last edited on
I'm guessing that solves the 0 problem, but it still doesn't hint at me why it's letting -6 go through
value < 0.0 || value > 1000.0

do if(i > 0) std::cout << sqrt(i,50);
never mind, it seems to screw up around the do while loop and letting some numbers go through depending on my amount of input, but that makes no sense, which leads me to believe that I have the syntax wrong
there is also a sqrt function in math.h if u wanted.
Notice that your first while loop while (value != 0.0 || value < 0.0) does absolutely nothing on the first iteration since value isn't initialized. I would take put a cin before the loop
assignment prohibits use of math library, I have everything correct...except the do-while and the freakin' 0 problem
maybe something in the limits library could help you. otherwise
while(i > 0 && i < 1000){ //stuff here} should work perfectly fine.
figured out the do-while. I shouldn't of included that code below the do-while loop that is within the loop. Now to deal with the pesky 0
Topic archived. No new replies allowed.