So i need help doing this project and heres what i have to do for the program i left some comments on what my code does etc, please help.

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.

#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
	}
}

Recommended Answers

All 10 Replies

This post looks exactly the same as this post... Smell fishy about this now...

its probably my roommate because we are working on the same project, that or another kid in my class to be honest with you.

Well, the funny thing is that the code is exactly the same as what his last post is... Anyway, you could just refer to that post and get it to work.

Here is my updated code the only thing i need to add is a precision but let me know what you think.

#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 << " tries" << endl;
 
	return 0;
}
 
double square_root(double x, double initial_guess, double error)
{
	next_guess = 0;
	while (absolute(next_guess - initial_guess) > error)
	{
		next_guess = initial_guess;
		initial_guess = (initial_guess + x / initial_guess) / 2;
		count++; 
	} 
	return 0;
}
 
double absolute(double difference)
{
	if(difference < 0)
	{
		return -difference;
	}
	else 
	{
		return difference;
	}
}

1)The value assigned from the equation should be 'next_guess', not 'initial_guess'
2)After you compute the absolute difference, put it in a variable first (remember to declare the variable outside the loop)
3)Then assign the 'initial_guess' with 'next_guess'
4)Move the while condition down to make it a do-while and replace absolute(next_guess-initial_guess) with the difference variable you saved inside the loop

The code itself works great and gives the right answer after the right amount of tries, i just didnt know if anyone had any idea on how to incorporate a precision method into it.

What exactly will the precision method acheive? also explain what is error.

error is how close you want it to come to the actual square root, and the precision method will allow the program to stop at the decimal point that you want it to ie: .001 or .1 or .01 etc.

That's what 'difference' is for. If your difference is less than or equal to the 'error' that the user assign, then the correct upto the precision is what it is. If you are talking about displaying, then it is different.

I understand error. What is initial guess?

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.