Monday, February 20, 2012

Testing code for the Babylonian Algorithm

So, here I was happy until I saw in the lab that I had to compute the square root of a number. Thing is, I'm still to lazy to grab a calculator and put some numbers in it.

So, I searched in Google how to compute it in C++ and I found that I had to include a new source code called "cmath". I tried it and it worked. Here's the source code for testing the sqrt() function from the cmath source code:

Source Code:
 #include <iostream>  
 #include <cmath>  
 using namespace std;  
   
 int main()  
 {  
   
 long double number_to_calculate_square_root_of;  
 char ans;  
 cout << endl;  
 cout << "Greetings.\n";  
   
 do  
 {  
 cout << "Please enter the number you would like\n";  
 cout << "to calculate the square root of: ";  
 cin >> number_to_calculate_square_root_of;  
 cout << "The square root of that number is: " << sqrt(number_to_calculate_square_root_of) << ".\n";  
   
 cout << endl;  
 cout << "Would you like to repeate this process again?";  
 cout << "Press 'Y' for yes and 'N' for no:";  
   
 }while(ans == 'y' || ans == 'Y');  
   
 cout << endl;  
 cout << "Thank you for testing.";  
 return 0;  
 }  


PS: I'm definitely going to use this for the Babylonian Algorithm just to check that the algorithm is actually approaching the square root of the inputted number.

PPS: My professor did mention this, but he told me he did...


Note to self: Pay MORE attention.

No comments:

Post a Comment