please focus on the additional requirement for the program Write and test a C++
ID: 3753376 • Letter: P
Question
please focus on the additional requirement for the program
Write and test a C++ program 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 output needs to be sensible and easy to read, not just numbers. Each piece of information 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.
Here is another example.
And here is another.
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.call-by-reference for this assignment. Do not use any features of the C++ Standard Template Library.
All of the functions take exactly one parameter. If you write a function that does not take exactly one parameter, other than a helper function that is not one of the required functions, then you will receive no credit for that function
Do not use arrays in this assignment. Do not use default parameters.
A program that wholly ignores the instructions will receive a grade of 0, even it it works correctly.
A Refinement Plan
1. Create a directory (folder) to hold assignment 2.
Put all of the files for this assignment 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. (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? Just write a number.)
3. Write a comment telling what the program will do when you are finished writing it.
Say what the input is and give an example of the output. Avoid talking about how the program works.
4. Write a heading and contract for a function next(n).
This function must take exactly one integer n and return the number that follows n in a hailstone sequence. Follow the guidelines for function contracts.
Examples of what 'next' does are: next(7) = 22 and next(22) = 11.
Since there is no number that follows 1 in a hailstone sequence, 'next' requires its parameter n to be greater than 1. State that in the contract. Your program must never compute next(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 C++ definition is called the implementation of the function. Make sure the implementation is faithful to the contract.
6. Write a main function that reads an integer n and shows the value of next(n).
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 heading and a contract for a function that takes an integer n and writes the entire hailstone sequence starting at n.
This function must take exactly one parameter of type int and it must have a void return-type. It must write the entire sequence on one line, with the numbers separated by spaces. Say so in the contract.
Follow the guidelines for contracts.
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 show the hailstone sequence starting at n.
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 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.
This function must take exactly one parameter of type int and it must return a value of type int. This function 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.
12. Write a heading and contract, then the body, of a function that takes an integer n and returns the largest value in the hailstone sequence starting at n.
This function must take exactly one parameter of type int and it must return a value of type int. 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 on a few different start values before moving on.
13. 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 take exactly one parameter of type int and it must return a result of type int. 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 this function. Test it on a few different start values before moving on.
14. Write a heading and contract, then the body, of a function that takes 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 take exactly one parameter of type int and it must return a result of type int. 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 on a few different start values.
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.call-by-reference for this assignment. Do not use any features of the C++ Standard Template Library.
All of the functions take exactly one parameter. If you write a function that does not take exactly one parameter, other than a helper function that is not one of the required functions, then you will receive no credit for that function
Do not use arrays in this assignment. Do not use default parameters.
A program that wholly ignores the instructions will receive a grade of 0, even it it works correctly.
Development plan1. Create a directory (folder) to hold assignment 2.
Put all of the files for this assignment 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. (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? Just write a number.)
3. Write a comment telling what the program will do when you are finished writing it.
Say what the input is and give an example of the output. Avoid talking about how the program works.
4. Write a heading and contract for a function next(n).
This function must take exactly one integer n and return the number that follows n in a hailstone sequence. Follow the guidelines for function contracts.
Examples of what 'next' does are: next(7) = 22 and next(22) = 11.
Since there is no number that follows 1 in a hailstone sequence, 'next' requires its parameter n to be greater than 1. State that in the contract. Your program must never compute next(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 C++ definition is called the implementation of the function. Make sure the implementation is faithful to the contract.
6. Write a main function that reads an integer n and shows the value of next(n).
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 heading and a contract for a function that takes an integer n and writes the entire hailstone sequence starting at n.
This function must take exactly one parameter of type int and it must have a void return-type. It must write the entire sequence on one line, with the numbers separated by spaces. Say so in the contract.
Follow the guidelines for contracts.
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 show the hailstone sequence starting at n.
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 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.
This function must take exactly one parameter of type int and it must return a value of type int. This function 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.
12. Write a heading and contract, then the body, of a function that takes an integer n and returns the largest value in the hailstone sequence starting at n.
This function must take exactly one parameter of type int and it must return a value of type int. 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 on a few different start values before moving on.
13. 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 take exactly one parameter of type int and it must return a result of type int. 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 this function. Test it on a few different start values before moving on.
14. Write a heading and contract, then the body, of a function that takes 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 take exactly one parameter of type int and it must return a result of type int. 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 on a few different start values.
Explanation / Answer
/*
* Problem: Generating HailStoneSeq for the given integer
* with some required print statements.
* Note: Code is written by following C++ coding standards
*
*/
// Including header files
// iostream: contains program input and output functions like:
// std::cout: to print on to console
// std::cin : to take input from keyboard
// std::endl: to print endline to console
#include <iostream>
/*
* Function : printHailstoneSeq(n)
* Parameters: n (integer type)
* Returns : nothing (void)
* Operation : Generates hailstone sequence for the given 'n' and
* with some extra operations for printing required output
*/
void printHailstoneSeq(int n)
{
std::cout << "The hailstone sequence starting at " << n <<" is:" << std::endl;
int len_of_seq = 1; // stores length of hailstone sequence
int largest_num = 1; // stores largest number in hailstone sequence
int temp_n = n; // stores input n integer for temporary use
if (n == 1) // if n = 1, then just 1 is the hailstone sequence
{
std::cout << n << std::endl;
}
else // if n != 1
{
std::cout << n << " ";
largest_num = n;
// While loop to generate hailstone sequence
// it will run upto 'n' value becomes 1 and
// prints the hailstone sequence
while(n != 1)
{
if((n % 2) == 0) // if n is even
{
n = (n / 2);
std::cout << n;
}
else // if n is odd
{
n = (3 * n) + 1;
std::cout << n;
}
std::cout<<" ";
len_of_seq++; // incrementing length of sequence
// if condition to find out largest number in hailstone sequence
if (largest_num < n)
{
largest_num = n;
}
}
std::cout << std::endl;
}
std::cout << "The length of the sequence is " << len_of_seq << "." << std::endl;
std::cout << "The largest number in the sequence is " << largest_num << "." << std::endl;
std::cout << "The longest hailstone sequence starting with a number up to " << temp_n << " has length " << len_of_seq << "." << std::endl;
std::cout << "The longest hailstone sequence starting with a number up to " << temp_n << " begins with " << temp_n << "." << std::endl;
}
int main()
{
// Integer number to store given input number
int n;
std::cout << "What number shall I start with? ";
std::cin >> n;
// Function call to generate hailstone sequence for given n with
// some required print statements
if (n <= 0)
{
std::cout << "Please provide input to be in the range [1 .. Inf]" << std::endl;
return -1;
}
printHailstoneSeq(n);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.