CS100M Spring 2006
Project 2
Due Thursday 2/16 at 6pm

Submit your files on-line in CMS before the project deadline. See the CMS link on the course webpage for instructions on using CMS. Both correctness and good programming style contribute to your project score.

You must work either on your own or with one partner. You may discuss background issues and general solution strategies with others, but the project you submit must be the work of just you (and your partner). If you work with a partner, you and your partner must register as a group in CMS and submit your work as a group.

Objectives

Completing this project will help you learn about loops, selection statements, and developing algorithms. Compared to Project 1, this project is more open-ended. Any constants that you use should be named (as variables) and defined in your programs, and any assumptions you make should be stated using comments.

1. Printing a Calendar

Write a program that prints a one-month calendar. Your program should request (a) a number of days and (b) a starting day-of-the-week. Your program should then print the corresponding one-month calendar. Here's an example (user input is shown in italics):

Number of days: 31
Starting day-of-the-week (1=Sun, 7=Sat): 3

        1  2  3  4  5
  6  7  8  9 10 11 12
 13 14 15 16 17 18 19
 20 21 22 23 24 25 26
 27 28 29 30 31
Hint: Use a for-loop to count from 1 to n where n is the number of days. Use an if-statement within the loop to test whether the current day is the last day of the week; if it is, print a new-line character (\n).

2. Testing the Collatz Conjecture

In 1937, Lothar Collatz conjectured that iterating the following function will always lead to the value 1 regardless of which positive integer it starts with.

f(n) =      n/2    if n is even
3n + 1    if n is odd

The idea is that for any positive integer n, we can form a sequence by computing n, f(n), f(f(n)), f(f(f(n))), etc. The conjecture is that no matter where we start, we always find the value one somewhere in this sequence. This conjecture is still unresolved (although testing has been used to show it holds for integers up to a bound larger than 1016). Here are the sequences for a few small integers:

1
2 -> 1
3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
4 -> 2 -> 1

Write a program that requests a starting number from the user and then prints the Collatz sequence starting from that number, quitting when 1 is reached. Use a while-loop. You can print one number per line if you wish. Your program should also report the length of the sequence; for example, the sequence above that starts at 3 has length eight; the one that starts at 4 has length three.  You might want to try your program with 27 as the starting number (the resulting sequence is of length greater than 100).

3. How Many Digits?

Write a program that reports the number of digits in a nonnegative integer.  Your program should request a nonnegative integer from the user and then report the number of digits in the integer.  Solve the problem using a while-loop and integer arithmetic (in other words, don't treat the input as a string and don't make use of the log10 built-in function).

Your program should have an outer loop so that the user can try several integers. After each integer is specified, your program should print a message that reports the integer and its number of digits.  This outer loop should exit when a negative number or a non-integer is tried.

Examples: 43 has two digits; 43210 has 5 digits.  Be careful of zero; 0 has one digit.

Strategy hint: First write the program so that it works for a single input. Once you have that program working, modify it by adding the outer loop so that it can take several inputs. (You may want to take advantage of the fact that the Matlab Editor allows you to indent a whole set of lines at once; see the Text menu.)