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
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:
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.
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.
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.
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!
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
Note that this program, like program #3, is worth 25% of your grade.