📄 http:^^www.cs.wisc.edu^~dsilva^cs110^c++^myc++^assg^program4^program4.html
字号:
Date: Mon, 11 Nov 1996 17:35:23 GMTServer: NCSA/1.5Content-type: text/htmlLast-modified: Mon, 21 Oct 1996 19:06:41 GMTContent-length: 7694<HTML><HEAD><TITLE>CS110: Program #4</TITLE></HEAD><BODY><CENTER><H1>Program #4 -- The Hailstone Series</H1></CENTER><H2>Due Date</H2>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.<H2>Introduction</H2><P>The January 1984 issue of <I>Scientific American</I> contains an articledescribing an interesting sequence of numbers known as the <I>HailstoneSeries</I>. The series is formed like this:<OL> <LI>Pick a positive integer. <LI>If it's odd, triple the number and add one. <LI>If it's even, divide the number by two.</OL> <P>Although the numbers bob up and down, eventually they reach a repeating"ground" state: <tt>4 2 1 4 2 1 ...</tt> This has been proven for everynumber up to about <tt>1.2E12</tt>.<H2>Problem Statement</H2><P>You will write a program to generate the <I>Hailstone Series</I> for anumber between 1 and 150 (chosen by the user). Your program will answer the following questions for each number entered by the user.<OL> <LI>How long is the series? <LI>What is the largest number in the series? <LI>What is the mean (average) of the series?</OL><H2>More information</H2><P>For the first question you should stop counting once the number<tt>1</tt> is reached. So, for example, if 8 is entered as the startingplace, then the answer to question #1 is 4 (i.e., including the initialinput, the fourth number in the sequence <tt>8 4 2 1</tt> is <tt>1</tt>).If 2 is entered then the answer is 2, since the sequence is simply <tt>21</tt>. Question #2 simply asks you to keep track of the maximum value inthe sequence. For question #3, using 8 as the start number, the averageis (8+4+2+1)/4 = 15/4 = 3.75. <EM>Note that this is not an integer.</EM><H2>Program Design Hints</H2>Your main program should work as follows:<EM> It should prompt the userto 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 USERAGAIN FOR ANOTHER VALUE IN THE CORRECT RANGE. If the user enters a valid numberyour program must display the hailstone series, the length of the series, themaximum 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 processmust go on until the user enters a '0' indicating that s/he would like to quit. </EM> <P>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. Theinner loop will control the generation of the <I>Hailstone Series</I>itself. You need to devise a way to terminate the outer loop ( having the user input a <tt>0</tt> will indicate they wish to quit). Aslong as the user enters numbers in the valid range (1 to 150), the programmust print the the hailstones series, the length of series, the maximum valueattained in the series and the mean value of all elements in the series. You will probably find it useful to definea function that computes the series, the length of the series and the mean value of the elements in the series. <P><STRONG>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.</STRONG> You might find it helpful to writea 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 thefunction prototype for the function <CODE>generateHailstoneSeries(...)</CODE> inthe 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 to4 functions.Finally, I have included a bare skeleton of my program to get you started on thejob. A <EM>sample run</EM> of the program is included below.<PRE WIDTH=80>#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 functionint getMaxValue(const int x[], int length);double computeAverage(const int x[], int length);void main(){}// Function that determines if number is evenint even(int x){ if (x % 2 == 1) return FALSE; else return TRUE;}//Skeleton definitions of additional functions TO BE WRITTEN BY YOU! </PRE><H2>Your test cases</H2>You will be required to turn in the screen output generated from a programrun in which the user looks at the <I>Hailstone Series</I> of at leastthree numbers (from 1 to 150). You must include the cases where the user incorrectlytype in a number beyond the specified range.<H2>Handing In the Assignment</H2><OL> <LI> Please turn in both an electronic copy of your source code (.cpp)and your executable program (.exe). <EM>I will be testing each programwith my test cases. If I do not receive an electronic copy of your sourcecode and executable, you will lose numerous points. In case your programdoes not compile or run correctly, you should turn in what you have to getsome credit. If your program fails on my test cases you will receive alow score.</EM> <LI> A printed copy of source code. <LI> A printed copy of your program run</OL><CENTER><H2>Items 2 and 3 must be <I>stapled</I> together.</H2></CENTER><H3>Sample Run</H3><PRE WIDTH=80> Enter number between 1 and 150(0 to quit) :151ERROR! RE-ENTER A NUMBER BETWEEN 1 and 150(0 to quit): -34ERROR! 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) :540ERROR! 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</PRE><HR><H2>Grading</H2><P>Note that this program, like program #3, is worth 25% of your grade.<UL> <LI> <B> CORRECT OUTPUT:</B> <UL> <LI> Correctly turned in output : 3 <LI> Correctly handles my test cases : 5 </UL> <LI> <B> USER INTERFACE:</B> <UL> <LI> Aesthetic display of program output: 1 </UL> <LI> <B> IMPLEMENTATION:</B> <UL> <LI> Correct use of loops and arrays: 6 <LI> Correct use of functions--parameter passing, return, calling: 6 <LI> Smart use of functions (avoiding redundancy of your code) : 2 </UL> <LI> <B> DOCUMENTATION:</B> <UL> <LI> Program description: 1 <LI> Meaningful variable names and useful section comments: 1 </UL> </UL></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -