LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Please Help ! (https://www.linuxquestions.org/questions/linux-newbie-8/please-help-929589/)

smahb001 02-15-2012 08:58 PM

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;

}

smahb001 02-16-2012 09:35 AM

Already visited by 108 visitors but no reply :-( I was surprised !!!!

TB0ne 02-16-2012 10:29 AM

Quote:

Originally Posted by smahb001 (Post 4604129)
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.

spazticclown 02-16-2012 11:33 AM

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.


All times are GMT -5. The time now is 02:36 AM.