Question
Write and test a C++ program in a fiction of C that reads a number n from the standard input (after giving a suitable prompt)
Write and test a C++ program in a fiction of C that reads a number n from the standard input (after giving a suitable prompt) and then writes the following information on the standard output:
-
the entire hailstone sequence starting at n, all on one line, with the numbers separated by spaces;
-
the length of the hailstone sequence that starts with n;
-
the largest number in the hailstone sequence that starts with n;
-
the length of the longest hailstone sequence that starts with a number from 1 to n;
-
the starting number of the longest hailstone sequence that starts with a number from 1 to n;
-
the largest number that occurs in any hailstone sequence that starts with a number from 1 to n.
The output needs to be sensible and easy to read, not just numbers. Each part should be on a separate line. 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.
The largest value in any hailstone sequence starting with a number up to 7 is 52.
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.
The largest value in any hailstone sequence starting with a number up to 1 is 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 largest value in any hailstone sequence starting with a number up to 8 is 52.
Additional Requirements
It is important that you follow the instructions. Define exactly the functions that are listed in the next section. Do not try to improve on the design described here. Do not add extra responsibilities to functions.
For this program, use loops. Do not use recursion. Use type int for all of the integers. Do not use
- arrays.
- call-by-reference,
- any features of the C++ Standard Template Library,
- default parameters
for this assignment. You can use the
All of the functions take exactly one parameter. If you write a function that does not take exactly one parameter of type int, other than a helper function that is not one of the required functions, then you will receive no credit for that function.
A Refinement Plan
Development plan |
---|
1. Create a directory (folder) to hold assignment 2. Put all of the files for this assignment in that directory. Start by getting files Makefile and dotest and put them in that directory. |
2. Use an editor to open a file. Copy and paste the template into it, and save it as hailstone.cpp. Edit the file. Add your name and the assignment number. If you will use tabs, say how far apart the tab stops are. |
3. Write a comment saying what the program will do when it is finished. After the tabs line and a blank line, write a comment that someone can read to determine
Do not try to define a hailstone sequence. Assume that the reader knows that term. Use term "hailstone sequence" to make it clear what your program does. Don't talk about "the sequence." Browsers will replace some symbols with sequences of special characters that do not belong in a program. Proofread your comment and make sure that it is readable.
|
4. Add a contract for function 'next'. Copy the following contract for function next(n) into your program. // Next(n) returns the number that follows n in a hailstone sequence. // For example, next(7) = 22 and next(8) = 4. // // Next requires n > 1, since there is no number that follows 1. If any function other than 'next' needs to get the next number in a hailstone sequence, it must use 'next' to get that next number. |
5. Write a C++ definition of 'next'. The heading for 'next' must be int next(int n) The C++ definition is called the implementation of the function. Make sure the implementation is faithful to the contract. Be sure that the body of 'next' does not change the value of n. The standards for this course require that no function change the value of a (call-by-value) parameter. (All of the parameters in this assignment are call-by-value parameters.) |
6. Write a 'main' function. Write a 'main' function that just reads an integer n and shows the value of next(n), so that you can test 'next'. Test your program on a few values to make sure that 'next' works. Make sure that your tests include an even input and an odd input so that, taken together, your tests make use of every line of code that you have written. |
7. Write a function that writes the entire hailstone sequence. Write a heading and a contract for a function that takes just an integer n and writes the entire hailstone sequence starting at n, all on one line. The heading must be void writeHailstoneSequence(int n) The contract should not explain information that is clear by looking at the heading. Don't say that it takes a parameter n of type int. Don't say that it returns a value of type int. Give useful information. |
8. Implement the function described in step 7. Make the function definition be faithful to the contract. |
9. Modify 'main'. Make 'main' no longer show next(n), but instead show the hailstone sequence starting at n. Be sure to label the hailstone sequence by what it is. Never write raw numbers. Test your program on a few different values of n. Do not move on until this part works. See testing, below, for an automated tester. |
10. Write a function that finds the length of a hailstone sequence. Write a heading and contract, then the body, of a function that takes an integer n and returns the length of the hailstone sequence starting at n. The heading must be as follows. int lengthHailstone(int n) Function lengthHailstone must not read or write anything. |
11. Modify 'main'. Make 'main' write both the hailstone sequence and the length of the hailstone sequence starting at n. Test your on a few different starting values n before moving on. If it does not work, fix it. Use the testingautomated tester. |
12. Write function that returns the largest value in a hailstone sequence. Write a heading and contract, then the body, of a function that takes exactly one parameter, an integer n, and returns the largest value in the hailstone sequence starting at n. This function must not read or write anything. Modify your 'main' function so that it also shows the largest value in the sequence. Test your program. |
13. Write a function that returns the longest hailstone sequence starting with a number from 1 to n. Write a contract, then an implementation, of a function that takes exactly one parameter, an integer n, and returns the length of the longest hailstone sequence starting at a number from 1 to n. This function must not read or write anything. Do not duplicate code to find the length of a hailstone sequence. Use your function from step 10 for that. Modify your 'main' function so that it also shows the result of the length of the longest sequence starting with a value from 1 to n. Test it. |
14. Write a function that returns the start value of the longest hailstone sequence starting with a number from 1 to n. Write a heading and contract, then the body, of a function that takes exactly one parameter, an integer n, and returns the start value of the longest hailstone sequence that starts on a value from 1 to n. This function must not read or write anything. You might be tempted to combine this with the previous function. Do not do that. Write a separate function. Modify your 'main' function so that it also shows the result of this function. Test your program. |
15. Write a function that returns the largest value that occurs in a hailstone sequence that starts with a number from 1 to n. Write a contract, then an implementation, of a function that takes exactly one parameter, an integer n, and returns the largest value that occurs in a hailstone sequence that starts with a number from 1 to n. This function must not read or write anything. Do not duplicate code to find the largest value in a hailstone sequence. Use your function from step 12. Modify your 'main' function so that it also shows the result of this function. Test it. |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started