Linux - Newbie This Linux forum is for members that are new to Linux. Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

 02-15-2012, 09:58 PM #1 smahb001 LQ Newbie   Registered: Feb 2012 Posts: 7 Rep: Please Help ! PLEASE can anyone tell me where i am wrong... I write a code on finding square root using babylonian method where there should be an initial guess. Initial guess is calculated Xo= L*(10 pow n) where n = round(square root of L) next step is to calculate the next guess using the formula X1= 1/2(Xo + S/Xo); where S is the input number. And if the difference between last two guess is less than or equal to some preset threshold then the program terminate. If anyone can find out the problem please help me to solve it. #include #include #include using namespace std; int main(){ const double tol = 0.000005; char s; int n, length; double old_guess, new_guess; cout << "This program will compute the square root of a number using the Babylonian Algorithm.\n"; cout << "Please enter a positive number:\n"; cin >> s; cout<< "The square root of " << s << "is:" < tol) { old_guess = new_guess; new_guess = (old_guess + s/old_guess)/2; } else while (fabs((new_guess-old_guess)/new_guess) < tol) break; }
 02-16-2012, 10:35 AM #2 smahb001 LQ Newbie   Registered: Feb 2012 Posts: 7 Original Poster Rep: Already visited by 108 visitors but no reply :-( I was surprised !!!!
02-16-2012, 11:29 AM   #3
TB0ne
LQ Guru

Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,275

Rep:
Quote:
 Originally Posted by smahb001 Already visited by 108 visitors but no reply :-( I was surprised !!!!
First, don't bump your own thread...all you've succeeded in doing is remove it from the zero-reply list, which has more visibility. Second, you don't bother telling us WHAT error(s) your code is giving you, under what version/distro of Linux, what compiler, etc.

All you've done is post a block of code. Provide details, and (if you're unhappy with the speed of responses here), PAY someone to answer you more quickly.

 02-16-2012, 12:33 PM #4 spazticclown Member   Registered: Sep 2010 Distribution: Fedora, Android, CentOS Posts: 91 Blog Entries: 2 Rep: First off your functions need to be declared before the "int main()" or they will not be compiled properly (speaking for gnu compiler & everything else I have used). Secondly not all variables are passed to the functions (length, s, old_guess, new_guess) and what is passed is not always used, this should be throwing errors. Looks like some major reworking of how this program works is in order. I would start by checking what variables are needed in each function and making a list of them (you can do this right in the function declaration). I hope this helps set you on the right path to solving this programming issue. 1 members found this post helpful.

 Tags codes