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.
[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).
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;
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?
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:
#include <iostream>
usingnamespace 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;
}