Write a program to compute the square root of a number. DO NOT USE any math libraries in this program. You will be using the Babylonian method (a.k.a. Heron’s method) to approximate the square root. More information is available on wikipedia:

http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method

To calculate the square root of x, the Babylonian method requires three inputs: x, an initial guess for the square root, and the error (tolerance). It uses a repetitive calculation to get closer and closer to the actual value of the square root:

nextGuess = (lastGuess + x / lastGuess) / 2

After each repetition, the method checks if the absolute value of the difference between nextGuess and lastGuess is less than the error. If so, it stops and returns the value of nextGuess as the square root. If the difference between nextGuess and lastGuess is larger than the error, it repeats the calculation.

Your program needs three functions: main, square root, and absolute value. The main function should get all three inputs from the user(x, initial guess, and error), run the square root function, and output the approximate value of the square root. In addition, the square root function should keep track of how many repetitions are used to calculate the square root value, which should be printed out along with the square root value in the main function. As you can not use any math libraries, you will also need to write your own absolute value function. Only the main function should interact with the user (getting inputs and displaying results). The square root and absolute value functions should not contain any cin or cout statements.


This is what I have so far and kinda stuck on what to do next
I think I did the absolute function part wrong (PLEASE TAKE A LOOK AT IT)
Please help me

#include <iostream>
using namespace std;

void square_root(double x, double initial_guess, double error, double& next_guess);
double absolute(double x, double initial_guess, double error, double next_guess);

double x, initial_guess, error;
double next_guess;

int main()
{
	cout << "Enter a number to square root: ";
	cin >> x;
	cout << "Enter a number for guess: ";
	cin >> initial_guess;
	cout << "Enter a number for error: ";
	cin >> error;
	square_root(x, initial_guess, error, next_guess);
	absolute(x, initial_guess, error, next_guess);
	cout << "Square Root of " << x << " is " << next_guess << endl;

	return 0;
}

void square_root(double x, double initial_guess, double error, double& next_guess)
{
	next_guess = (initial_guess + x / initial_guess) / 2;
}

double absolute(double x, double initial_guess, double error, double next_guess)
{
	next_guess = next_guess - initial_guess;

	if (next_guess > error)
	{
		square_root(x, initial_guess, error, next_guess);
	}
	if (next_guess < 0)
	{
		next_guess = next_guess * -1;
		square_root(x, initial_guess, error, next_guess);
	}
	else
	{
	}
	return next_guess;
}

Recommended Answers

All 5 Replies

Why do you do everything again in the absolute() function for? What you need to keep track is the previous value you have computed thus far. Compare the value with the new value each time you get a new value. If the new value and the previous value are not different (by the different of your specified error), then you get the answer.

The problem I have with your input is the error. What does it mean by error? Does it mean precision requires for correctness (i.e. 3 means must be correct up to 0.001)? Or you enter it in as 0.001???

Oh and you don't have anything to hold on the returned value from absolute() which I don't know why you implement this function this way (you may not need it anyway). You could change the square_root() function to return double, and you do everything in square_root() function instead.

// don't need to pass by reference because you can return the value instead
double square_root(double x, double initial_guess, double error) {

  // start do-while loop here
  // compute the next_guess
  // compare the difference between your next_guess and initial_guess
  // if the difference is lower or equal to your specified error, break out of the loop
  // the difference means both negative and positive (which may require the check of absolute value)
  // otherwise, assign initial_guess value with next_guess
  // (end of loop with while statement here)

  // return your next_guess here
}

yes the error i believe is for correctness.
For this part of the question
"(After each repetition, the method checks if the absolute value of the difference between nextGuess and lastGuess is less than the error. If so, it stops and returns the value of nextGuess as the square root. If the difference between nextGuess and lastGuess is larger than the error, it repeats the calculation.)"
how would i start it cus i have to check the value between next_guess and initial_guess to see if its <= error.
btw i was confused when i started this problem

If you follow my comment in the previous post, you should at least get some ideas... The part I commented about absolute value should be like...

// in the loop
if (absolute(next_guess-initial_guess)<=error)) {  // found the solution
  break;
}

// and the absolute value function is very simple like...
double absolute(double v) {
  if (v>=0) { return v; }
  else { return (-1*v); }
}

Hi, I'm really confused, don't know what to do now
As I mentioned in my previous post that every repetition must be checked in the absolute function and then checks if the difference between next_guess and initial_guess is < error if so it returns the value of next_guess but if not it should continue with the repetition until the difference is < error.

#include <iostream>
using namespace std;

double square_root(double x, double initial_guess, double error);
double absolute(double x, double initial_guess, double error);

double x, initial_guess, error;
double next_guess;
double difference;
int count = 0;

int main()
{
	cout << "Enter a number to square root: ";
	cin >> x;
	cout << "Enter a number for intitial_guess: ";
	cin >> initial_guess;
	cout << "Enter a number for error: ";
	cin >> error;
	square_root(x, initial_guess, error);
	cout << "The square root of " << x << " is " << next_guess << " after " << count << " try" << endl;

	return 0;
}

double square_root(double x, double initial_guess, double error)
{
	do
	{
		next_guess = (initial_guess + x / initial_guess) / 2;
		count++;
		absolute(x, initial_guess, error);
	}
	while (difference > error);

	return 0;
}

double absolute(double x, double intitial_guess, double error)
{
	difference = next_guess - initial_guess;
	if (difference < error)
	{
		return next_guess;
	}
	else 
	{

	}
	return 0;
}

This is what I have after following your comments and your previous post on the absolute function

#include <iostream>
using namespace std;

double square_root(double x, double initial_guess, double error);
double absolute(double difference);

double x, initial_guess, error;
double next_guess;
double difference;
int count = 0;

int main()
{
	cout << "Enter a number to square root: ";
	cin >> x;
	cout << "Enter a number for intitial_guess: ";
	cin >> initial_guess;
	cout << "Enter a number for error: ";
	cin >> error;
	square_root(x, initial_guess, error);
	cout << "The square root of " << x << " is " << next_guess << " after " << count << " try" << endl;

	return 0;
}

double square_root(double x, double initial_guess, double error)
{
	do
	{
		next_guess = (initial_guess + x / initial_guess) / 2;
		count++; // this is the keep track of how many times i did the calculation until i get the closest sqrt value for x
		if(absolute(next_guess - initial_guess) < error)
		{
			break;
		}
	}
	while (difference > error);

	return 0;
}

double absolute(double difference)
{
	if(difference < error)
	{
		return next_guess; // if its true it returns the sqrt value of the number the user entered for x
	}
	else 
	{
		// if false it should continue to with the repetition but i don't know how to do that
		// i need some help
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.