HW1_Q1

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> #include <cmath> using namespace std; double mySqrt(double x){ double estimate = 1; // for starters, imagine that n's square root is 1 // if 1 is a square root, the 'other' square root will be n/1 (since the product of the roots needs to be n), // so our new square root estimate would be the average of the two roots double newEstimate = 0.5*(estimate + x/estimate); double epsilon = 0.000001; while(abs(estimate-newEstimate) > epsilon){ estimate = newEstimate; newEstimate = 0.5*(estimate + x/estimate); } return estimate; }// mySqrt() int main() { double s = mySqrt(5); cout << s << endl; // expect 2.236 return 0; }// main()
Editor Settings
Theme
Key bindings
Full width
Lines