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;
}``````

## All 10 Replies

Your compiler did not lie to you -- variable low is being used before it as initialized with a value. At that time the variable contains just some random value, whatever was left in the memory location. How to you fix that? Just initialize it when it is declared, e.g. `int low = 0;`

Either way, when I run the program, it asks me for the lower bound, upper bound... and then where I would expect the results it fails to do anything after that.

line 27 is wrong. Take a good look at it and you will see what's wrong with it.

Ok, from what I can tell I should remove the expression (test % 2 == 1) from the else statement. The program now builds correctly without any warnings or errors, but it still doesn't print anything after. Is my line 33 (with the cout) in the wrong location?

move line 33 up to just above the } on line 31.

line 13: should use variable low here, not num.

Alright, this has been a huge help. I have been fiddling with the code and I have gotten it to work for only the lower bound number, getting this output:

Enter the lower bound: 3
Enter the upper bound: 7
3 converges to 1 in 7 steps.
Press any key to continue . . .

Here is my modified code. How do I get it to work to include all the numbers to the upper bound as well?

``````#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 >> low;
cout << "Enter the upper bound: ";
cin >> up;

num = low;

while (num <= up)
{
test = num;
count = 0;
while (test != 1)
{
if (test % 2 == 1)
test = test * 3 + 1;
else
test = test / 2;
count++;
}
cout << low << " converges to 1 in " << count << " steps." << endl;
break;
}

return 0;
}``````

ON line 19 try a for statement instead of a while statement. `for(num = low; num <= high; num++)` And delete the break on line 32.

Thank you, I was able to get it to work. I appreciate your help and fast responses. Would you be able to explain to me why it worked with a for statement and those expressions though?

And excuse me for being so unknoledgable with my posts, I just started taking the C++ class at my college this semester so I only have a few weeks experience

In the while statement you posted the value of num was never changed, so all you had was an infinite loop, except for the break statement you had on line 32. The break statement cause the while loop to terminate after the first iteration.

for loops are ideal when you have to increment a counter until it reaches a specific value, such as in the project you are now working on. while loops, although they can be made to work too, are not quite as convenient as for loops.

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.