LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 02-15-2012, 09:58 PM   #1
smahb001
LQ Newbie
 
Registered: Feb 2012
Posts: 7

Rep: Reputation: Disabled
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 <iostream>
#include <cmath>
#include <cstring>
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:" <<new_guess <<endl;


double InitialGuess(double num);
double SqrtCalc(double num);

cout << endl;

return 0;

}


double InitialGuess(double num){
//calculate the length of the input number.
length =srtlen(s);
cout<< "Length of the input number is: " << length << endl;
while (n = double round double sqrt(length)){
InitialGuess = length * pow (10,n);
return InitialGuess;
}

}

double SqrtCalc(double num){
if (s < 0.0)
cout << "Cannot find square root of negative number"<< endl;
else
if (s == 0.0)
cout << "square root of "<< s<< " is 0.00"<< endl;
else if
old_guess = InitialGuess(double num);
new_guess = (old_guess + s/old_guess)/2;
while (fabs((new_guess-old_guess)/new_guess) > tol)
{
old_guess = new_guess;
new_guess = (old_guess + s/old_guess)/2;
}
else
while (fabs((new_guess-old_guess)/new_guess) < tol)
break;

}
 
Old 02-16-2012, 10:35 AM   #2
smahb001
LQ Newbie
 
Registered: Feb 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Already visited by 108 visitors but no reply :-( I was surprised !!!!
 
Old 02-16-2012, 11:29 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,916

Rep: Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689
Quote:
Originally Posted by smahb001 View Post
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.
 
Old 02-16-2012, 12:33 PM   #4
spazticclown
Member
 
Registered: Sep 2010
Distribution: Fedora, Android, CentOS
Posts: 91
Blog Entries: 2

Rep: Reputation: 21
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.
  


Reply

Tags
codes


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off




All times are GMT -5. The time now is 10:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration