Babylonian Algorithm

Feb 25, 2010 at 2:50pm
Hello everyone. So I recently registered for a math class that is teaching C++ programming. Well, the basics of C++ anyways. I'm not on here for any answers, just a little help in the right direction. I have to write a program that will use thr babylonian algorithm to find the square root of a number. It must iterate as many times as needed to be able to get a more precise answer. So this is what I have so far:

[code]
#include <iostream>
using namespace std;
int main()
{
int n;
double answer_n(0), guess(2), prev_guess(0), r;


cout << "This program will compute the square root\n";
cout << "of a number using the Babylonian algorithm.\n";
cout << "Please enter a whole number and press the return key:\n";
cin >> n;

r = n/guess;

guess = (guess+r)/2;

prev_guess += guess;


while (n > guess);
{

if (prev_guess <= (guess * 0.01) + guess)
answer_n += prev_guess;
else
r = n/guess;

guess = (guess+r)/2;

prev_guess += guess;
}

cout << "The sqaure root of "<< n << " is " << answer_n;
cout << endl;


return 0;

}

[code]
I know that I am to use a while loop, but I can't get my program to run. Well, ok, it runs but it won't go past entering the integer. Any help would be appreciated.
Last edited on Feb 25, 2010 at 3:25pm
Feb 25, 2010 at 3:23pm
Please edit your post and put the code inside [code] tags.
Feb 25, 2010 at 7:07pm
[Edit:]
As corrected by R0mai:
double answer_n(0), guess(2), prev_guess(0), r;
should be
double answer_n=0, guess=2, prev_guess=0, r;
and
your most critical mistake:
 
while (n > guess);

should be
while (n > guess)

while (something); is the same as while (something){} which is an infinite loop unless something equals zero.

Your while cycle makes a wrong check. Other than that your "guess" does converge to sqrt(n).
Last edited on Feb 25, 2010 at 10:28pm
Feb 25, 2010 at 8:17pm
double answer_n(0), guess(2), prev_guess(0), r;
should be [sometinhg else]

This constructor-like initialization form is totally correct.
Feb 25, 2010 at 9:07pm
yeah, the real bug is the semicolon that tition pointed out.
Feb 26, 2010 at 1:15am
Thanks everyone for your help! I actually figured it out. I was able to make it run so I hope it's right. :D

[code]
#include <iostream>
using namespace std;
int main()
{
int n, count(10);
double answer_n, guess, r;


cout << "This program will compute the square root\n";
cout << "of a number using the Babylonian algorithm.\n";
cout << "Please enter a whole number and press the return key:\n";
cin >> n;
cout << "Please enter a 'guess' number to divide by:\n";
cin >> guess;

r = n/guess;
guess = (guess + r)/2;

while (count > 0)
{
r = n/guess;
guess = (guess + r)/2;

if (guess <= (guess * 0.01) + guess)
answer_n = guess;
else
r = n/guess;
guess = (guess + r)/2;

count-=1;
}


cout << "The sqaure root of "<< n << " is " << answer_n;
cout << endl;


return 0;

}
[code]

I ran the program like that and it works!
Feb 26, 2010 at 1:19am
Hey,
while (count > 0) doesn't make sense because you are running it only once.
if (guess <= (guess * 0.01) + guess) doesn't make sense because it's always true if guess is a non-negative number.

Why don't you try evaluating whether the difference between your *previous* guess and your *current* guess is in absolute value less than 0.01?

Also you close the [code] tag by typing [ /code]
Last edited on Feb 26, 2010 at 1:20am
Feb 26, 2010 at 1:34am
Ok. So I might sound dumb asking this but I will anyways. My problem is trying to define "previous" guess and "current" guess. I get stuck at writing the program in that way. How would I be able to define "previous" and "current" in the program? Because I'm thinking that I need to write the program so that it loops until "guess" is within 1% of the previous guess, but guess will just keep re-writing over itself, right? So, if I initiate guess, previous guess, and current guess, how would I be able to do that? That's what I get stuck on. For example, My last program I wrote, I had to write a program that would read ten whole numbers, positive and/or negative, sum all the positive, sum all the negative, and then sum both positive and negative numbers. The way I wrote that program:
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
using namespace std;
int main()
{
	
	int num_a, num_b, num_c, num_d, num_e, num_f, num_g, num_h,
		num_i, num_j, num_k, sum_pos(0), sum_neg(0), sum;

	cout << "This program sums all the positive numbers,\n";
	cout << "all the negative numbers, and then sums the\n";
	cout << "positive and negative numbers together.\n";
	cout << "Enter ten whole numbers, positive or negative,\n";
	cout << "then press the return key after each number is\n";
	cout << "entered.\n";
	cin >> num_n;

	if (num_a > 0)
		sum_pos+=num_a;
	else
		sum_neg+=num_a;

	if (num_b > 0)
		sum_pos+=num_b;
	else
		sum_neg+=num_b;

	if (num_c > 0)
		sum_pos+=num_c;
	else
		sum_neg+=num_c;

	if (num_d >0)
		sum_pos+=num_d;
	else
		sum_neg+=num_d;

	if (num_e > 0)
		sum_pos+=num_e;
	else
		sum_neg+=num_e;

	if (num_f > 0)
		sum_pos+=num_f;
	else 
		sum_neg+=num_f;

	if (num_g > 0)
		sum_pos+=num_g;
	else
		sum_neg+=num_g;

	if (num_h > 0)
		sum_pos+=num_h;
	else 
		sum_neg+=num_h;

	if (num_i > 0)
		sum_pos+=num_i;
	else
		sum_neg+=num_i;

	if (num_j > 0)
		sum_pos+=num_j;
	else
		sum_neg+=num_j;
	

	sum = sum_pos + sum_neg;


	cout << "Your sum for positive is\n";
	cout << sum_pos << endl;
	cout << "Your sum for negative is\n";
	cout << sum_neg << endl;
	cout << "Your total sum of both positive and negative is\n";
	cout << sum << endl;
	
	return 0;
	
}
Feb 26, 2010 at 1:36am
After writing out that program, I got 100 on my program but my teacher then told me an easier way:

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
#include <iostream>
using namespace std;
int main()
{

	int num_n, sum_pos(0), sum_neg(0), count(9), sum;

	cout << "This program sums all the positive numbers,\n";
	cout << "all the negative numbers, and then sums the\n";
	cout << "positive and negative numbers together.\n";
	cout << "Enter ten whole numbers, positive or negative,\n";
	cout << "then press the return key after each number is\n";
	cout << "entered.\n";
	cin >> num_n;

	while (count > 0)
	{
		cout << "Please enter another number.\n";
		cin >> num_n;

	if (num_n > 0)
		sum_pos += num_n;
	else 
		sum_neg += num_n;

	count -= 1;
	}
	

	sum = sum_pos + sum_neg;


	cout << "Your sum for positive is\n";
	cout << sum_pos << endl;
	cout << "Your sum for negative is\n";
	cout << sum_neg << endl;
	cout << "Your total sum of both positive and negative is\n";
	cout << sum << endl;

	
	return 0;
	
}
Topic archived. No new replies allowed.