ernie.WPI.EDU> cat exam3_1.C // file exam3_1.C, cs2223, D97/98 // // a program to calculate a square root // 10Apr98 NW, First version // includes #include "headers.h" // prototype double unknown(double, double, double); // unknown function // global variable int counter; // main int main () { double number; while (1) // infinite loop { cout << "please enter a number greater than 0: "; cin >> number; if (number <= 0) return 0; // exit criterion counter = 0; cout << "The answer is " << unknown(number, number, number) << endl; cout << "The function was called " << counter << " times" << endl << endl; } } // end main() double unknown(double number, double guess, double increment) // unknown function { extern int counter; counter++; // count how many times the function was called double error = guess * guess - number; double abs_error = error < 0 ? -error : error; // absolute value of the error if (abs_error < 1.0 / 1048576.0) return guess; // close enough double inc2 = increment / 2.0; if (error > 0) return unknown(number, guess - inc2, inc2);// positive error else return unknown(number, guess + inc2, inc2);// negative error } // end unknown() // Copyright 1998 Norman Wittels ernie.WPI.EDU> g++ exam3_1.C ernie.WPI.EDU> a.out please enter a number greater than 0: 1 The answer is 1 The function was called 1 times please enter a number greater than 0: 2 The answer is 1.41421 The function was called 23 times please enter a number greater than 0: 3 The answer is 1.73205 The function was called 24 times please enter a number greater than 0: 8 The answer is 2.82843 The function was called 26 times please enter a number greater than 0: 19 The answer is 4.3589 The function was called 27 times please enter a number greater than 0: 25 The answer is 5 The function was called 27 times please enter a number greater than 0: 26 The answer is 5.09902 The function was called 27 times please enter a number greater than 0: 199 The answer is 14.1067 The function was called 33 times please enter a number greater than 0: 1000 The answer is 31.6228 The function was called 35 times please enter a number greater than 0: 0.5 The answer is 0.707107 The function was called 20 times please enter a number greater than 0: -3 ernie.WPI.EDU>