Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- double mySqRt (double, int *count);
- double mySqRt (double x, int *count)
- {
- double low, mid, high;
- if (x < 1)
- {
- low = x;
- high = 1.0;
- }
- else
- {
- low = 0;
- high = x;
- }
- mid = (low + high) / 2.0;
- *count = 0;
- cout << *count << endl;
- while (abs(mid*mid - x) > 0.000000000)
- {
- if (mid*mid > x)
- high = mid;
- else
- low = mid;
- mid = (low + high) / 2;
- *count++;
- cout << *count << endl;
- }
- return mid;
- }
- int main()
- {
- int x, count;
- double SqRt = mySqRt(x, &count);
- cout << "Enter a number larger then 0" << endl;
- cin >> x;
- while (x > 0)
- {
- cout << "The number is " << x << " and the square root is " << SqRt << " and it went through " << count << " iterations." << endl;
- cin >> x;
- }
- cout << "You entered a negative number" << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment