Write a recursive function that: Accepts as input: • An integer for n • Another integer to specify the limit of the number of terms. If the limit is reached, the sequence should stop. Then, the program should report to the console: • Output each step of the sequence given the input n as a starting point, in a formatted table. The table should have two columns:

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

C++

A hailstone sequence is a series of numbers that can be generated with an algorithm that takes an input number n.
Here is one such sequence:
If n is 1, the sequence stops.
If n is even, the next number is n / 2.
Else, n is odd and the next number is 3n + 1.
For example, if we start with n = 10, the sequence is:
10, 5, 16, 8, 4, 2, 1
Starting with n = 17, the sequence is a bit longer:
17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1
The sequences resemble a "hailstone" because they bounce up and down a bunch of times before eventually hitting the ground, similar to a way a hailstone is formed in the atmosphere.
One interesting aspect of this sequence: it appears that for given any number, the sequence will always reach 1 (and stop). Some numbers take many steps to reach 1.
Assignment
Write a recursive function that:
Accepts as input:
• An integer for n
• Another integer to specify the limit of the number of terms. If the limit is reached, the sequence should stop.
Then, the program should report to the console:
• Output each step of the sequence given the input n as a starting point, in a formatted table. The table should have two columns:
• The count of the term (starting with 1, going up as 2, 3, 4, etc.)
• The value of that term
• After the table, output a line stating if the sequence was aborted early because of the limit
Output the highest number reached
If the number of terms output reaches the limit, the program should stop printing any more numbers at that point.
Transcribed Image Text:A hailstone sequence is a series of numbers that can be generated with an algorithm that takes an input number n. Here is one such sequence: If n is 1, the sequence stops. If n is even, the next number is n / 2. Else, n is odd and the next number is 3n + 1. For example, if we start with n = 10, the sequence is: 10, 5, 16, 8, 4, 2, 1 Starting with n = 17, the sequence is a bit longer: 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 The sequences resemble a "hailstone" because they bounce up and down a bunch of times before eventually hitting the ground, similar to a way a hailstone is formed in the atmosphere. One interesting aspect of this sequence: it appears that for given any number, the sequence will always reach 1 (and stop). Some numbers take many steps to reach 1. Assignment Write a recursive function that: Accepts as input: • An integer for n • Another integer to specify the limit of the number of terms. If the limit is reached, the sequence should stop. Then, the program should report to the console: • Output each step of the sequence given the input n as a starting point, in a formatted table. The table should have two columns: • The count of the term (starting with 1, going up as 2, 3, 4, etc.) • The value of that term • After the table, output a line stating if the sequence was aborted early because of the limit Output the highest number reached If the number of terms output reaches the limit, the program should stop printing any more numbers at that point.
Example Output (two separate runs)
Number: -8
Invalid number, please try again.
Number: 11
Limit of terms: 6
Term Number
Value
1
34
3
17
52
26
13
Sequence was aborted early.
Highest number reached: 52
Number: 12
Limit of terms: 30
Term Number
Value
12
6
3
10
16
8.
4
9
10
Highest number reached: 16
Transcribed Image Text:Example Output (two separate runs) Number: -8 Invalid number, please try again. Number: 11 Limit of terms: 6 Term Number Value 1 34 3 17 52 26 13 Sequence was aborted early. Highest number reached: 52 Number: 12 Limit of terms: 30 Term Number Value 12 6 3 10 16 8. 4 9 10 Highest number reached: 16
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY