Connecting Tech Pros Worldwide Forums | Help | Site Map

Using Babylonian algorithm to find square root

Member
 
Join Date: Sep 2007
Posts: 67
#1: Sep 26 '07
my program is trying to use the babylonian algorithm in order to find the square root... i have a number for the number to have the square root taken of and also a number to run the loop... Whenever i go to print out the answer its rounding instead of giving me the answer i need... any help??

Expand|Select|Wrap|Line Numbers
  1.   double num1, num2, the_root;
  2.  
  3.  
  4.    cout<< "Enter number to have square root taken of it\n";
  5.    cin>> num1;
  6.    cout<< "Enter number for how many times to run Babylonian Algorithm\n";
  7.    cin>> num2;
  8.  
  9.    the_root= sqrt(num1);
  10.  
  11.    double the_root2 = num1/2;
  12.  
  13.    for (double count = 1; count <= num2; count++);
  14.     {
  15.       the_root2=(the_root2+num1/the_root2)/2;
  16.     ;}   
  17.  
  18.    cout.setf(ios::fixed);
  19.    cout.setf(ios::showpoint);
  20.    cout.precision(10);  
  21.    cout<< "The root of " <<num1<<" is: "<< the_root<<".\n";
  22.    cout<< "The root from the Babylonian Algorithm is: "<<the_root2<<".\n";
  23.  
  24.  

arunmib's Avatar
Member
 
Join Date: May 2007
Posts: 104
#2: Sep 26 '07

re: Using Babylonian algorithm to find square root


Quote:

Originally Posted by socondc22

for (double count = 1; count <= num2; count++);

Just TAKE A LOOK at this line again.....
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#3: Sep 26 '07

re: Using Babylonian algorithm to find square root


socondc22-

Please have a look at your Private Messages (PM's) accessible through the link in the top right corner of the page.

Thanks
Member
 
Join Date: Sep 2007
Posts: 67
#4: Sep 27 '07

re: Using Babylonian algorithm to find square root


Quote:

Originally Posted by arunmib

Just TAKE A LOOK at this line again.....


I still don't know what is wrong...
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#5: Sep 27 '07

re: Using Babylonian algorithm to find square root


Well, what have you tried doing on that line to fix it?
Member
 
Join Date: Sep 2007
Posts: 67
#6: Sep 27 '07

re: Using Babylonian algorithm to find square root


Quote:

Originally Posted by sicarie

Well, what have you tried doing on that line to fix it?

i really don't know what is wrong with the line... like i thought it was right... i prolly need to change something but i just dont know what....
Member
 
Join Date: Sep 2007
Posts: 67
#7: Sep 28 '07

re: Using Babylonian algorithm to find square root


can someone please help me to figure out why this part of my code is wrong...

for (double count = 1; count <= num2; count++);
Member
 
Join Date: Sep 2007
Posts: 67
#8: Sep 28 '07

re: Using Babylonian algorithm to find square root


im trying to figure out why its stopping at the first time through the loop of my babylonian algorithm...
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#9: Sep 28 '07

re: Using Babylonian algorithm to find square root


What is num2's value??
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#10: Sep 28 '07

re: Using Babylonian algorithm to find square root


socondc22-

Please confine your questions on the same topic to a single thread.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#11: Sep 28 '07

re: Using Babylonian algorithm to find square root


Quote:

Originally Posted by socondc22

can someone please help me to figure out why this part of my code is wrong...

for (double count = 1; count <= num2; count++);

hint: It's got a sad looking winking smiley at the end ... (rotated 180 degrees)

kind regards,

Jos (<--- ps. pssst: semi-colon alert! semi-colon alert! ;-)
Reply