|
CMSC 104
Sections 103/105/401
Programming Project Two
The 3x + 1 Conjecture
Assigned: Monday 4/5/99
Due: Before Midnight Thursday 4/15/99
|
The Objective
the purpose of this project is to give you practice
writing C p rogams with loops and relational operators.
The Background
A interesting problem in the field of mathematics known as number
theory is the "3x + 1" or "Collatz" conjecture. The conjecture is this:
Starting with any positive integer X, do the following:
- if X is even, divide it by 2 yielding X / 2
- if X is odd, multiply it by 3 and add 1, yielding 3X + 1
and you'll eventually end up at 1! This conjecture has never been
proven (you can become famous if you prove it), but has been verified
for all positive integers up to 56,000,000,000,000.
The Task
Your assignment is to write a program that inputs a positive integer from
the user, and follows the Collatz sequence above:
- If the integer is even, halve it
- If the integer is odd, multiply by 3 and add one
- Continue this process until the value is 1, printing the new value each time
- Finally, print out the number of these operations you performed
Be sure that your program is ROBUST. Your program
must guard against invalid input (e.g. non-positive integers) and reprompt
the user. See sample output below.
Be sure that your program has a file header comment that includes
- the name of the file
- your name
- the date the file was written
- a description of your program
- your algorithm or pseudo-code to solve the problem
Your program must adhere to the
coding standards and
indentation styles discussed in class.
Be sure your project is well commented.
See course syllabus for project grading guidelines and
university policies concerning sharing of code.
Sample Output
retriever[102] a.out
Please enter a positive integer: -5
Please enter a positive integer: 0
Please enter a positive integer: 9
Initial value is 9
Next value is 28
Next value is 14
Next value is 7
Next value is 22
Next value is 11
Next value is 34
Next value is 17
Next value is 52
Next value is 26
Next value is 13
Next value is 40
Next value is 20
Next value is 10
Next value is 5
Next value is 16
Next value is 8
Next value is 4
Next value is 2
Next value is 1
This process took 19 steps.
retriever[103]
Although your output need not be identical to the above,
all information must be present.
10-point Extra Credit Opportunity
For 10 points of extra credit, enhance your program to allow
the user to enter a new positive integer and have your program
run again. You should continue to accept new integers from the user
until the user enters zero. Negative numbers should still be rejected
by your program.
For example, your new prompt might be
Please enter a positive integer (0 to quit):
After your program produces output, prompt again. If the new input
is positive, run your program again. If the new input is 0, exit
Submitting the Program
Your C source code file for this project MUST be called proj2.c.
To submit your project, type the following at the Unix prompt:
submit cs104-103 proj2 proj2.c
To verify that your project was submitted, you can execute the
following command at the Unix prompt. It will show all files that
you submitted in a format similar to the Unix 'ls' command.
submitls cs104-103 proj2