SHARE
TWEET

Untitled

a guest Jan 20th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*  Yana Domashitsky
  2.     Homework 2
  3.     10.17.12
  4.     Professor Cox
  5.     Advanced C++ Wed. Eve.
  6.  */
  7.  
  8. #include <iostream>
  9. #include <cmath>
  10.  
  11. using namespace std;
  12.  
  13. double mySqRt (double);
  14.  
  15. double mySqRt (double x, int *count)
  16. {
  17.     double low, mid, high;
  18.     if (x < 1)
  19.     {
  20.         low = x;
  21.         high = 1.0;
  22.     }
  23.     else
  24.     {
  25.         low = 0;
  26.         high = x;
  27.     }
  28.     mid = (low + high) / 2.0;
  29.    
  30.     *count = 0;
  31.    
  32.     cout << *count << endl;
  33.    
  34.     while (abs(mid*mid - x) > 0.000000000)
  35.     {
  36.         if (mid*mid > x)
  37.             high = mid;
  38.         else
  39.             low = mid;
  40.         mid = (low + high) / 2;
  41.         *count ++;
  42.     }
  43.    
  44.     return mid;
  45. }
  46.  
  47. int main()
  48. {
  49.     int x, count;
  50.    
  51.     cout << "Enter a number larger then 0" << endl;
  52.     cin >> x;
  53.    
  54.     while (x > 0)
  55.     {
  56.         cout << "The number is " << x << " and the square root is " << mySqRt(x, &count) << " and it went through " << count << " iterations." << endl;
  57.         cin >> x;
  58.     }
  59.    
  60.     cout << "You entered a negative number" << endl;
  61.    
  62.     return 0;
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top