Guest User

Untitled

a guest
Jan 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. double mySqRt (double, int *count);
  8.  
  9. double mySqRt (double x, int *count)
  10. {
  11.     double low, mid, high;
  12.     if (x < 1)
  13.     {
  14.         low = x;
  15.         high = 1.0;
  16.     }
  17.     else
  18.     {
  19.         low = 0;
  20.         high = x;
  21.     }
  22.     mid = (low + high) / 2.0;
  23.    
  24.     *count = 0;
  25.    
  26.     cout << *count << endl;
  27.    
  28.     while (abs(mid*mid - x) > 0.000000000)
  29.     {
  30.         if (mid*mid > x)
  31.             high = mid;
  32.         else
  33.             low = mid;
  34.         mid = (low + high) / 2;
  35.         *count++;
  36.         cout << *count << endl;
  37.  
  38.     }
  39.    
  40.     return mid;
  41. }
  42.  
  43. int main()
  44. {
  45.     int x, count;
  46.     double SqRt = mySqRt(x, &count);
  47.    
  48.     cout << "Enter a number larger then 0" << endl;
  49.     cin >> x;
  50.    
  51.     while (x > 0)
  52.     {
  53.         cout << "The number is " << x << " and the square root is " << SqRt << " and it went through " << count << " iterations." << endl;
  54.         cin >> x;
  55.     }
  56.    
  57.     cout << "You entered a negative number" << endl;
  58.    
  59.     return 0;
  60. }
Add Comment
Please, Sign In to add comment