CSci 230: Computing systems organization
Home Syllabus Readings Assignments Tests

printable version

Test 1

[1] [2] [3] [4] [5] [6] [7] [8]

Problem X1.1.

[12 pts] The hailstone sequence is defined by the following loop.

while n ≠ 1:
    if n is odd:
        n ← 3 n + 1
    else:
        nn / 2

Starting with 7, this loop iterates n through a sequence of 17 numbers:

7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1.

If we started with 5, the sequence would contain just six numbers — 5, 16, 8, 4, 2, 1.

Complete the following function so that it returns how many numbers are in the hailstone sequence starting from its parameter; thus, hailstone(7) should return 17 while hailstone(5) returns 6.

int hailstone(int start) {