Hi everyone,
I am new to C++ and I'm having difficulty getting this program to work to calculate Haistone numbers. So please - any advice or solution as to what I'm doing wrong would be greatly appreciated.
The program is to take two numbers, and then for each number in the sequence between them it is to print out the number of steps it takes the starting number to reach 1 using the hailstone procedure. Recall that we are generating a sequence of numbers, x0, x1, x2, … using the procedure with the next number generated using the current number as input. So x1 = f(x0), x2 = f(x1), etc.
The procedure works that if a number is odd, it multiplies by 3 and adds 1. But if it is even, it divides by 2 until it converges to 1
Output should look something like this:
Enter the lower bound: 3
Enter the upper bound: 7
3 converges to 1 in 7 steps
4 converges to 1 in 2 steps
5 converges to 1 in 5 steps
6 converges to 1 in 8 steps
7 converges to 1 in 16 steps
Here is the code that I have at the moment. I am getting a warning saying "warning C4700: uninitialized local variable 'low' used"
#include <iostream>
using namespace std;
int main()
{
int low; // lower bound (starting value)
int up; // upper bound (ending value)
int num; // current value
int count; // holds # of steps taken
int test; // value being worked with
cout << "Enter the lower bound: ";
cin >> num;
cout << "Enter the upper bound: ";
cin >> up;
num = low;
while (num <= up)
{
test = num;
count = 0;
while (test != 1)
{
if (test % 2 == 0)
test = test / 2;
else (test % 2 == 1);
test = test * 3 + 1;
count++;
}
}
cout << low << "converges to 1 in " << count << "steps" << endl;
return 0;
}