computing the square root

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 + xlastGuess2


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. Here is an example:

- Assume x is 16, the initial guess is 6, and the error is 0.5.
- So, the first repetition computes nextGuess to be (6 + 16 / 6) / 2 = 4.3333.
- Next, check the difference between the lastGuess (6) and nextGuess (4.3333). The absolute value of the difference is 1.6667, which is greater than the error of 0.5. So, repeat the calculation.
- The second repetition computes nextGuess to be (4.3333 + 16 / 4.3333) / 2 = 4.0128.
- Check the difference between lastGuess (4.3333) and nextGuess (4.0128). The absolute value of the difference is 0.3205, which is less than the error of 0.5. So, the procedure stops and returns 4.0128 as the square root of 16.


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. As always, you must validate the user's inputs. All three must be positive. If the user enters a value that is not positive, then your main function should prompt them to enter the value again (and repeat until they enter a valid input).

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 must not contain any cin or cout statements.







Here is what I have:


#include <iostream>
using namespace std;

double square_root(double x, double lastGuess, double error);
double absolute(double difference);
double x, lastGuess, error;
double nextGuess;
double difference;
int count = 0;

int main()
{
cout << "Enter a value to take the square root of: ";
cin >> x;
cout << "Enter an ititial guess for the square root: ";
cin >> lastGuess;
cout << "Enter an error (tolerance) for the calculation: ";
cin >> error;
square_root(x, lastGuess, error);
cout << "After " << count << " iterations, the square root is approximately: " << nextGuess << endl;
return 0;
}
double square_root(double x, double lastGuess, double error)
{
do
{
nextGuess = (lastGuess + x / lastGuess) / 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(nextGuess - lastGuess) < error)
{
break;
}
} while (difference > error);
return 0;
}
double absolute(double difference)
{
if (difference < error)
{
return nextGuess; // 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
}
}
Here is what i now have:
#include <iostream>
using namespace std;

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

int main()
{
double x, lastGuess, error;
double difference;
int count = 0;
cout << "Enter a value to take the square root of: ";
cin >> x;
cout << "Enter an ititial guess for the square root: ";
cin >> lastGuess;
cout << "Enter an error (tolerance) for the calculation: ";
cin >> error;
square_root(x, lastGuess, error);
cout << "After " << count << " iterations, the square root is approximately: " << nextGuess << endl;
return 0;
}
double square_root(double x, double lastGuess, double error)
{
double difference;
double nextGuess;
int count = 0;
do
{
nextGuess = (lastGuess + x / lastGuess) / 2;
count++;
if (absolute(nextGuess - lastGuess) < error)
{
break;
}
} while (difference > error);
return 0;
}
double absolute(double difference)
{
if (difference < 0)
{
return difference / (-1);
}
else
{
return difference;
}

}
You are not allowed to submit homework assignments, if you do, you can be sure that almost no one will help you.

Sorry.
We won't do homework assignments in their entirety, but we can help with questions if people get stuck and can be a little more specific where they're running into trouble.
im not asking anyone to do it. I have a specific question. Im not getting the correct approximatation of the square root and also the number of iterations. Is there something wrong with my loop?
Thanks
cout << "After " << count << " iterations, the square root is approximately: " << nextGuess << endl;
The count variable defined in main is not the same one that you're incrementing in the function, and nextguess isn't defined in main.

In the function, difference doesn't get assigned a value before you try to compare it with error in the while loop.

Seems to me that you'd want to square the current guess, and check the difference between that squared amount and the original x that was entered?

Edit: Also - looks like you want to return a calculated number from the square function, not 0?
Last edited on
So which thing do I square?

double square_root(double x, double lastGuess, double error, int& count)
{
double difference;
double nextGuess;
do
{
nextGuess = (lastGuess + (x / lastGuess)) / 2;
count++;
difference = nextGuess - lastGuess;
if (absolute(difference) < error)
{
break;
}
} while (difference > error);
return nextGuess;
}
double absolute(double difference)
{
if (difference < 0)
{
return difference / (-1);
}
else
{
return difference;
}
You'd square the current guess to compare it to x.

Difference should be the difference between x and the guess squared, not the difference between guesses. You're trying to see how close you are to the square root of the original number.

In your code here, lastGuess never changes - and it should be changing when you recalculate the line doing the guess/divide/average. You can do this with just one variable representing the guess - you don't need two different variables.
I tried what you said. It didnt change my answers at all. Can you be more specific, or show me the section how it should look.
Thank you
i worked on your first code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;

void square_root(double x, double lastGuess, double error); //void
bool absolute(double nextGuess, double lastGuess, double error);  /////
double x, lastGuess, error;
double nextGuess;
double difference;
int counT = 0;


int main()
{
cout << "Enter a value to take the square root of: ";
cin >> x;
cout << "Enter an ititial guess for the square root: ";
cin >> lastGuess; /*why bother user? set something, say, lastGuess =x;*/
cout << "Enter an error (tolerance) for the calculation: ";
cin >> error;
square_root(x, lastGuess, error);
cout << "After " << counT << " iterations, the square root is approximately: " << nextGuess << endl;
return 0;
}

/*keep two spaces between functions*/
void square_root(double x, double lastGuess, double error) ///
{
	while (true) //////
	{
	nextGuess = (lastGuess + x / lastGuess) / 2;
	counT++; 
	if (absolute(nextGuess,  lastGuess , error) ) //////
		break;
	lastGuess = nextGuess;  //// IT MUST HAVE BEEN NOTICED
}

return;
}


bool absolute(double nextGuess, double lastGuess, double error)  /////
{
	double difference = nextGuess > lastGuess ? nextGuess-lastGuess : lastGuess- nextGuess;

	if(difference < error)
		return true;
	else if (difference>=error) // else
		return false;	
} 
Topic archived. No new replies allowed.