The Hailstone Sequence


Now let's make our next official program in C++. The program will be called:

hailstone.cpp

The hailstone sequence is:

If a term n is even, the next term is n/2

If a term n is odd, the next term is 3n + 1

The sequence ends when you reach the number 1.


For a "C" level grade, write a C++ program, given one positive integer input n, to display the hailstone sequence starting with n.


For a "B" level grade, also print out the number of terms including the number the user supplies and the one at the end, and the largest term in the sequence.


For an "A" level grade, include the "B" level and also include the number of times the sequence goes up and the number of times it goes down.


For example, the input may be:
40

The output would be:

4020105168421
starting number20=40/210=20/25=10/216=5*3+18=16/24=8/22=4/21=2/2


If the input was:
11

The output would be:

11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1

Note: for a "B" you would also include:
The number of terms = 15
The maximum term = 52
Note: for an "A" you would also include:
The number of times the sequence goes up = 4 The number of times the sequence goes down = 4
Try 15:
Try 52:

 

 

  PSEUDOCODE FOR BASIC hailstone.cpp:

get a positive integer called number from the user
print number followed by a comma
while (number does not equal one)
   if (number is even)
     Set number equal to number divided by two
   end if
   else
     set number equal to three times number plus one
   end if
   print number followed by a comma
end while