Due: May 10, 2023
Points: 100
A typographical error in question 3's first example has been fixed. The answer should be 95028, not 95029.
For a given number n, let k be the least number of iterations needed to reach the number 1 (excluding the initial value). Then k is called the total stopping time of n.
For example, if n = 29, then the sequence is:
29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1and so the total stopping time of 29 is 18.
Write a program that takes as input a positive integer and prints both the sequence and the total stopping time for that integer. Prompt the user with “> ” (note the trailing space and no newline). The output should look like (user input is in red):
> 29 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 The total stopping time for 29 is 18
Call your program “collatz.c”.
You will need to use the mathematical function pow. You must also include the math library header file math.h and, when you compile, put “–lm” after the compile command to link the math library (if you forget this, the compiler will say that pow is undefined).
Here are some examples of input and output:
Number of days: 150 After 150 days, the number of deaths predicted is 95028
Number of days: 8 The number of days must be at least 18
In the latter case, the message is written to the standard error, not the standard output. Call your program “covid.c”.
|
ECS 36A, Programming & Problem Solving Version of April 26, 2023 at 12:28PM
|
You can also obtain a PDF version of this. |