CS12

Assignment 1

Due: 11:59 PM Thursday April 27

In this assignment you will be using recursion. Make sure to remember the two important steps when dealing with recursive problems:

For this assignment this will not be too hard since I will give you most of the information that you need to do (at least for the first problem). For the other parts of the assignment you will have to think of what the base case and recursion function is. Remember this is an INDIVIDUAL effort. Avoid at all costs sharing your source code or even letting anyone see it. The penalties for plagiarism are harsh so get it out of your mind early on.

Part 1

Write a recursive function to compute and return the greatest common divisor (GCD) of two integers. You may assume both integers are positive. The GCD of two numbers is defined to be:

If x is divisible by y:GCD(x,y)=y
Otherwise:GCD(x,y)=GCD(y, remainder of x divided by y)

Part 2

Write a recursive function to print the digits of an integer in reverse. For example, if the number was 12345, your function should print 54321. You may assume the integer is positive. The prototype of your function should be:

         void print_reverse(int);

Part 3

Write a recursive function to multiply two numbers. The multiplication '*' operator should not appear anywhere in your function. You may assume that the parameters are positive integers. The prototype for the function should be:

         int multiply(int,int);

Part 4


Write a simple main program that allows the user to test and verify that each of the above functions work correctly.


Bonus

Do the Towers of Hanoi problem from the Deitel and Deitel book. This problem is problem 3.42 on page 217.

You do not have to do this bonus problem, and doing it will not guarantee you any more points on the assignment, but if you are serious about the class you should do it. If you can do this problem correctly without looking up the answer somewhere then it probably means you have a good grasp of recursion and will do well in the class.