Question: MUST BE PEP/9 Fall 2018 Comp 162 Peter Smith Program 4: Collatz conjecture Due date: End of lab on Friday, October 26, 2018 Collatz conjecture
MUST BE PEP/9
Fall 2018 Comp 162 Peter Smith Program 4: Collatz conjecture Due date: End of lab on Friday, October 26, 2018 Collatz conjecture is that if the following operation is applied repeatedly to a positive integer (N) it will eventually reach 1 If N is even, N=N/2 otherwise N=N*3+1 For example, if we start with 7, we reach 1 in 16 steps 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 Write and test a Pep/9 translation of the following C program. Note that variable number is unsigned so has range 0..65535. The usage of number is highlighted. #include int main() { int N, count; unsigned number; scanf(%d%,&N); while (N != 0) { number = N; count = 0; while (number != 1) { if (number%2==0) number = number / 2; else number = number * 3 + 1; count ++; } printf(%d steps ,count); scanf(%d,&N); } printf(Goodbye ); } Your program should check for overflow where appropriate. You can check your results at http://www.nitrxgen.net/collatz/ If you would like to output intermediate values as they are calculated, you can use the udeco subroutine available on the course home page. It prints a 16bit unsigned number in decimal. By the due date, turn in (i) your source code and (ii) results of testing it. Identify any cases where the program output differs from what you expected. Grading Correctness 50 points Testing 25 points Readability 25 points. Example program run N: 16 4 steps N: 7 16 steps N: 16384 14 steps N: 32768 15 steps N: 123 46 steps N: 12003 50 steps N: 12001 Overflow N: 40 8 steps N: 0 Goodbye
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
