University of Pittsburgh
Summer 2003
CS0007:  Introduction to Computer Programming
Project 2: The Hailstone Series
PROJECT INFORMATION
DUE DATE
Thursday, July 3, midnight
VALUE
50 pts
BACKGROUND READING
Chapters 6 and 7

Read the entire assignment before attempting this project.
 

INTRODUCTION

The January 1984 issue of Scientific American contains an article describing an interesting sequence of numbers known as the Hailstone Series. The series is formed like this:
  1. Pick a positive integer. 
  2. If it's odd, triple the number and add one. 
  3. If it's even, divide the number by two. 
  4. Go back to 2.
Although the numbers bob up and down, eventually they reach a repeating "ground" state: 4 2 1 4 2 1 ... This has been proven for every number up to about 1.2E12.

Try this by hand for a few small numbers and get the idea (note how YOU go into a loop! For example, start with 9 and write out the sequence. I'll even give you some space: 
 
 
 
 
 
 
 
 

 

WHAT TO DO

You will write a program to generate the Hailstone Series for initial values entered by the user. Your program should answer the following questions after the ground state has been reached: 
  1. How many items are in the sequence? 
  2. What is the largest number the sequence reaches along the way?
For the first question you should stop counting once any member of the sequence 4 2 1 is reached, and it should include the initial value. If a member of the ground state is entered as the initial value, then the length of the sequence is just 1. 

For example, if 8 is entered as the initial value, then the answer to question #1 is 2 (i.e., it stopped counting when 4 was encountered). Question #2 simply asks you to keep track of the maximum value in the sequence, which is 8 in this small example.

Look above at the sequence you made with the starting value of 9, and answer the two questions. You should find that there are 18 items in the list, with 52 being the maximum. If you didn't, go back and try it again. If you can't do these sequences on paper, you'll have a dill of a pickle of a time writing a program to do it.
 

OUTPUT

In a nutshell, your program should begin by asking for a positive integer, and then it should print out the answers to the questions above.
 

 

PROGRAM REQUIREMENTS

Just a few things:
  • use good style (indentation, structure, appropriate variable names, and comments)
  • your final program should not print out the entire sequence (some of them are very long)
    • although you might want to while you are working on the program
    • if you want, you can ask the user if s/he wants to see the sequence, then print only if a yes was given.
  • you should error check and guarantee the user has entered a positive value (do not worry about malformed input, however - assume an integer will be entered) - see assignment 1 for the pseudocode for getting valid input.
  • Remember:  think about the PROBLEM FIRST - analyze it in terms of what needs to be done and how to do it.  After this, then decide how best to break THIS down using functions and procedures.  It's up to you in this assignment how to do this, but I would suspect you would need at least one function and a few procedures.  If you want to talk over your analysis with me, please stop by.
 
TURNING THE ASSIGNMENT IN

As usual, copy your source and executable files into the proj2 handin directory.
 
Last Updated: 6/24/03 by H. Chad Lane, hcl@cs.pitt.edu
© 2000-2001 University of Pittsburgh