Hailstone sequence

Hi, I am trying to figure out how to list the hailstone sequence numbers after I type in a random number. please help!

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
#include <cstdio>
using namespace std;

int Hailstone(int n)
{
	if(n % 2 == 0)
	{
		n /= 2;		//number is even 
	}
	else
	{
		n = (3 * n )+ 1;	//number is odd
	}
	return n;		//returns the value 
}


int lengthHail(int num)
{
	int count = 1;
	while(num != 1)
	{
		num = Hailstone(num);
		count++;		//adds one to count everytime a calculation is done 
	}
	return count;
}



int main(int argc, char** argv)
{
  
  
  	printf( "Enter a number: " );
	scanf( "%i", &n );
	printf( "You entered: %i\n",&n); 
  
  
  return 0;
}
first of all, use the #include <stdio.h> instead of #include <cstdio> , next, you haven't declared a variable for n in main. Next, for your question, you have to call the function Hailstone and lengthHail with those numbers that your user inputted (which by the way you need to do twice). When doing so, do not include line 37
@RUNNER
No, #include <cstdio> is correct.

@Atrain
Since you are programming in C++, you should be using the C++ I/O facilities.

1
2
3
4
5
6
7
8
int main()
{
	int n;
	cout << "Enter a number: ";
	cin >> n;

	int length = lengthHail(n);
	cout << "The Hailstone sequence is " << length << ".\n";

If you wish to also show the hailstone sequence, you should print each calculated value. You can do this by simply adding a cout statement somewhere (like between lines 13 and 14, or between lines 23 and 25).

Other possibilities are:

use a vector
1
2
3
4
5
6
7
8
9
10
11
12
13
vector<int> generate_hailstone_sequence(int n)
{
	vector<int> seq;

	do
	{
		seq.push_back(n);
		n = Hailstone(n);

	} while (n != 1);

	return seq;
}

Conveniently, the vector knows its length, which is also the length of the generated Hailstone sequence.

without vectors (or arrays)
You can also avoid storing the sequence itself, but simply print it to the screen. The cleanest way to do this is to create another function that does it:

1
2
3
4
5
6
7
8
9
10
void print_hailstone_sequence(int n)
{
	do
	{
		cout << n << " ";
		n = Hailstone(n);

	} while (n != 1);
	cout << "\n";
}

Your main() can then use it:

1
2
3
4
5
6
7
8
9
10
11
int main()
{
	int n;
	cout << "Enter a number: ";
	cin >> n;

	cout << "The Hailstone sequence is: ";
	print_hailstone_sequence(n);

	int length = lengthHail(n);
	cout << "The Hailstone sequence is " << length << " values long.\n";


Final words: notice that I made a change in the loop in your function -- the sequence terminates with 1, but that 1 should not be excluded from the sequence. Hence, lengthHail(1) should be 1, not 0.

Hope this helps.
Last edited on
BTW, many of us frequent more than one forum.
http://stackoverflow.com/questions/26074458/hailstone-sequence-in-c
No no, what I meant Duoas, was, as i forgot to mention in my previous post, is including stdio.h is better since you then don't need the std namespace and can terminate line 2.
No, what I meant was, after having studied this for over ten years, that including <cstdio> is better because it:

- fixes the mess that stdio.h leaves behind
- properly enables stuff that belongs to C++ from C++'s stdio
- obviates (future) compiler errors

At this time, most modern compilers treat the two the same, AFAIK. YRMV.

Don't teach people to walk around with the safety off.
What about using namespace std. If you are correct, then that line should be removed and everything should be prefixed with std:: for efficiency.
Random words strung together does not a valid argument make.

(Hint: 'efficiency' is not an issue.)
@Duoas ...Random Words?

What makes you think they are random?
Last edited on
What makes you think they are not?
Because, as in many forums you yourself have mentioned before, using namespace std; is a bad line of code.
Last edited on
Yay! I love strawman!

I myself have been very explicit that using namespace std; should not be used in specific contexts, but otherwise that it is fine. It is true that there are members here who disagree with me. Nevertheless, if you really think you can disabuse me of hypocrisy concerning my true position, best put up with examples. Here're mine. (Good luck refuting them.)

    Inappropriate use: http://www.cplusplus.com/faq/beginners/multiple-sources/#header
    Appropriate use: http://www.cplusplus.com/faq/beginners/multiple-sources/#source

Further, I am not the one who made an issue of using namespace std; here -- you did. You have argued that using #include <cstdio> requires that you also avoid using namespace std;, for unstated (and actually unrelated) reasons of efficiency, and you stated it as if something I said validated your assertion.

Feel free to apply circular reasoning now.
and you stated it as if something I said validated your assertion.


No, I didn't. And by the way, I argued that by means that are better, using namespace std;should be removed, not that it is required to be removed
Last edited on
Feel free to apply circular reasoning now.
Wow. That didn't take long...

No, I didn't.
LOL, yes you did:

 RUNNER PRO AGARIO wrote, Feb 6, 2016 at 11:29am:
What about using namespace std. If you are correct, then that line should be removed and everything should be prefixed with std:: for efficiency.

I am correct, but that does not validate your assertion ("should").

And you're still trying to wiggle the words around, and claim, arrogantly, that you are smarter than the people who designed the language, because "by means that are better" (which you have consistently argued means #include <stdio.h> ) you can obviate using namespace std;.

Accept that you made a mistake and move on.
(Or, for more hilarity, keep trying...)


Ah, well, I've had my fun here.
Duoas, define should for me please.
also, circular reasoning is as follows:

Circular reasoning (Latin: circulus in probando, "circle in proving"; also known as circular logic) is a logical fallacy in which the reasoner begins with what they are trying to end with. The components of a circular argument are often logically valid because if the premises are true, the conclusion must be true.
Topic archived. No new replies allowed.