Date: Mon, 11 Nov 1996 17:35:23 GMT Server: NCSA/1.5 Content-type: text/html Last-modified: Mon, 21 Oct 1996 19:06:41 GMT Content-length: 7694 CS110: Program #4

Program #4 -- The Hailstone Series

Due Date

This final assignment is due on Thursday, October 24. It will be accepted on Friday, October 25 until 5PM but no later. This assignment is tricky and so you may want to get started on it as soon as possible.

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.

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.

Problem Statement

You will write a program to generate the Hailstone Series for a number between 1 and 150 (chosen by the user). Your program will answer the following questions for each number entered by the user.

  1. How long is the series?
  2. What is the largest number in the series?
  3. What is the mean (average) of the series?

More information

For the first question you should stop counting once the number 1 is reached. So, for example, if 8 is entered as the starting place, then the answer to question #1 is 4 (i.e., including the initial input, the fourth number in the sequence 8 4 2 1 is 1). If 2 is entered then the answer is 2, since the sequence is simply 2 1. Question #2 simply asks you to keep track of the maximum value in the sequence. For question #3, using 8 as the start number, the average is (8+4+2+1)/4 = 15/4 = 3.75. Note that this is not an integer.

Program Design Hints

Your main program should work as follows: It should prompt the user to enter a number ( 0 to quit) between 1 and 150. If the user enters a number outside this range, the program MUST INDICATE AN ERROR AND PROMPT THE USER AGAIN FOR ANOTHER VALUE IN THE CORRECT RANGE. If the user enters a valid number your program must display the hailstone series, the length of the series, the maximum value attained within the series and the mean value of all elements in the series. Then it must prompt the user for the next number. This process must go on until the user enters a '0' indicating that s/he would like to quit.

For the first part of the program, you will need two nested loops. The outer loop will repeatedly ask the user for new initial values. The inner loop will control the generation of the Hailstone Series itself. You need to devise a way to terminate the outer loop ( having the user input a 0 will indicate they wish to quit). As long as the user enters numbers in the valid range (1 to 150), the program must print the the hailstones series, the length of series, the maximum value attained in the series and the mean value of all elements in the series. You will probably find it useful to define a function that computes the series, the length of the series and the mean value of the elements in the series.

You must use arrays to store the elements of the hailstone series. Your program must contain several functions to solve this problem. You will receive almost no credit if you do not use functions. You might find it helpful to write a function that actually takes in a number as input and returns and the hailstone series in an array along with the length of the series (hint : Look at the function prototype for the function generateHailstoneSeries(...) in the skeleton below). You have a good opportunity to write several small functions to do simple tasks like computing the maximum value in the hailstone series array, computing the mean ...... Your final program will probably contain at least 3 to 4 functions. Finally, I have included a bare skeleton of my program to get you started on the job. A sample run of the program is included below.

#include <iostream.h>



const int TRUE = 1;
const int FALSE = 0;
const int MAX_LEN  = 200;



//Function prototypes of some functions you might want to write!
void generateHailstoneSeries(int number, int series[], int& length);
int even(int x); //prototype of already written function
int getMaxValue(const int x[], int length);
double computeAverage(const int x[], int length);

void main()
{


}




// Function that determines if number is even
int even(int x)
{
  if (x % 2 == 1)
    return FALSE;
  else
    return TRUE;
}

//Skeleton definitions of additional functions TO BE WRITTEN BY YOU! 


Your test cases

You will be required to turn in the screen output generated from a program run in which the user looks at the Hailstone Series of at least three numbers (from 1 to 150). You must include the cases where the user incorrectly type in a number beyond the specified range.

Handing In the Assignment

  1. Please turn in both an electronic copy of your source code (.cpp) and your executable program (.exe). I will be testing each program with my test cases. If I do not receive an electronic copy of your source code and executable, you will lose numerous points. In case your program does not compile or run correctly, you should turn in what you have to get some credit. If your program fails on my test cases you will receive a low score.
  2. A printed copy of source code.
  3. A printed copy of your program run

Items 2 and 3 must be stapled together.

Sample Run


 Enter number between 1 and 150(0 to quit) :151
ERROR! RE-ENTER A NUMBER BETWEEN 1 and 150(0 to quit): -34
ERROR! RE-ENTER A NUMBER BETWEEN 1 and 150(0 to quit): 149
 The series is : 149 448 224 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 
 The length of the series is :24
 The maximum value attained in the series is :448
 The mean value is : 54.9583

 Enter number between 1 and 150(0 to quit) :25
 The series is : 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 
 The length of the series is :24
 The maximum value attained in the series is :88
 The mean value is : 27.4167

 Enter number between 1 and 150(0 to quit) :12
 The series is : 12 6 3 10 5 16 8 4 2 1 
 The length of the series is :10
 The maximum value attained in the series is :16
 The mean value is : 6.7

 Enter number between 1 and 150(0 to quit) :540
ERROR! RE-ENTER A NUMBER BETWEEN 1 and 150(0 to quit): 8
 The series is : 8 4 2 1 
 The length of the series is :4
 The maximum value attained in the series is :8
 The mean value is : 3.75

 Enter number between 1 and 150(0 to quit) :0


Grading

Note that this program, like program #3, is worth 25% of your grade.