Assigned: | Wednesday, September 2 |
Due: | Tuesday, September 15, 11:59pm |
This assignment is intended to familiarize you with C++, loops, functions and contracts.
It is important that you follow the instructions. Do not try to improve on the design described here. Read the entire assignment before you start working on it.
Given any positive integer n, the hailstone sequence starting at n is obtained as follows. You write a sequence of numbers, one after another. Start by writing n. If n is even, then the next number is n/2. If n is odd, then the next number is 3n + 1. Continue in this way until you write the number 1.
For example, if you start at 7, then the next number is 22 (3 times 7 plus 1). The next number after 22 is 11. The hailstone sequence starting at 7 is
[7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1],
and it contains 17 numbers. The hailstone sequence starting at 6 is [6, 3, 10, 5, 16, 8, 4, 2, 1], and the hailstone sequence starting at 1 is [1].
The functional requirements tell what the program is supposed to do when you run it.
Write a C++ program that reads a number n from the user (after giving a suitable prompt) and then tells the user the following information:
The output needs to be sensible and easy to read, not just numbers.
For example, a session with this program might look as follows.
Parts in black are written by the program.
Parts in blue are typed by the user.
What number shall I start with? 7
The hailstone sequence starting at 7 is:
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
The length of the sequence is 17.
The largest number in the sequence is 52.
The longest hailstone sequence starting with a number up to 7 has length 17
The longest hailstone sequence starting with a number up to 7 begins with 7
Here is another example.
What number shall I start with? 1
The hailstone sequence starting at 1 is
1
The length of the sequence is 1.
The largest number in the sequence is 1.
The longest hailstone sequence starting with a number up to 1 has length 1
The longest hailstone sequence starting with a number up to 1 begins with 1
And here is another.
What number shall I start with? 8
The hailstone sequence starting at 8 is
8 4 2 1
The length of the sequence is 4.
The largest number in the sequence is 8.
The longest hailstone sequence starting with a number up to 8 has length 17
The longest hailstone sequence starting with a number up to 8 begins with 7
The nonfunctional requirements are additional requirements on how the program must be written, documented, etc., that are not directly concerned with what the program does when you run it.
The program is required to follow the coding standards for this course, which include the following.
You are required to start from the template that is provided. Be sure to put in your own name and the assignment number.
The program must use the correct file format and file name. Be sure to indent well.
Every function is required to have a clear, concise and precise contract.
Use margin comments sparingly.
Each function can have at most one loop in its body.
A function body must not change the value of a call-by-value parameter. Pay attention to this one. In the past, many students have violated this requirement.
Make sure that each function does its whole job., not just part of it job.
Avoid code duplication.
End the last line of output.
Do not use global or static variables. This is another one to watch out for. Using global or static variables will cost you a very large number of points.
Every body of an if-statement, loop, etc. must be a compound statement.
Do not use redundant tests in if-statements.
If code is only performed at the end of the last iteration of a loop, then it should be written after the loop, not inside the loop.
The design of a program includes issues such as which functions it includes, how those functions are organized into modules, and what each function and module accomplishes. It normally does not include details of how each function works.
This section both describes the required design and suggests how to write the program. You are required to follow the design.
For this program, use loops. Do not use recursion. Use type int for all of the integers.
Create a directory (folder) to hold assignment 1. Put all of the files for this assignment in that directory.
Create a file called hailstone.cpp. Copy and paste the template into it. Edit the template. If you will use tabs, say where the tab stops are. (If you don't know, type a line of about 8 x's. Then, in the line beneath that line, type a tab. How many x's were skipped over?)
Write a contract for a function that takes an integer value n and returns the number that follows n in the hailstone sequence. If you call this function next, then next(7) = 22 and next(22) = 11.
Since there is no number that follows 1 in the hailstone sequence, this function requires its parameter n to be greater than 1. State that in the contract. You program must never compute next(1).
Any time you want to get the next number in the sequence, you must use this function.
Now write a C++ definition of the function that you have described in the preceding item. The C++ definition is called the implementation of the function.
Write a main function that reads an integer n and shows the value of next(n). Test it on a few values to make sure that the next function works. Make sure that your tests, taken together, make use of every line that you have written.
Write a contract, then an implementation, of a function that takes an integer n and writes the entire hailstone sequence starting at n. This function has a void return type.
Modify your main function to so that it no longer shows next(n), but shows the hailstone sequence starting at n. Test it.
Write a contract, then an implementation, of a function that takes an integer n and returns the length of the hailstone sequence starting at n. This function must not write anything.
Modify your main function to so that it shows both the hailstone sequence and the length of the hailstone sequence starting at n Test it.
Write a contract, then an implementation, of a function that takes an integer n and returns the largest value in the hailstone sequence starting at n. This function must not write anything.
Modify your main function to so that it also shows the result of this function. Test it.
Write a contract, then an implementation, of a function that takes an integer n and returns the length of the longest hailstone sequence starting at a number from 1 to n. This function must not write anything.
Modify your main function to so that it also shows the result of this function. Test it.
Write a contract, then an implementation, of a function that takes an integer n and returns the start value k, from 1 to n, that has the longest length. This function must not write anything.
Modify your main function to so that it also shows the result of this function. Test it.
Submit your work.
Get file Makefile and put it in the same directory as your program. Then, use the following commands.
You must submit your program using the following method. Email submissions will not be accepted. An excuse that you do not know how to use Linux will not be accepted.
To turn in assignment 1, change
your directory to the one that holds assignment 1 and do the
following command.
~abrahamsonk/3300/bin/submit 1 hailstone.cpp
After submitting, you should receive confirmation that the
submission was successful. If you do not receive confirmation,
assume that the submission did not work.
Command
~abrahamsonk/3300/bin/submit 1
will show you what you have submitted for
assignment 1.
You can do repeated submissions. New submissions will replace old ones.
Late submissions will be accepted for one day after the due date. If you miss a late submission deadline by a microsecond, your work will not be accepted.