CS 1320 (Principles of Algorithm Design I):
Homework #10 (optional)

Assigned:
April 27, 2000.
Due:
May 4, 2000, by noon. Not accepted late.
Credit:
Up to 40 points (10 points per problem). This assignment is optional, so you can do as many or few of these problems as you like. Any points you earn on this assignment will count as extra credit; i.e., they will be added to your "points earned" total before computing your final grade.

Instructions

Write a recursive C++ function to solve each of the problems below. To get full credit, your function must be recursive. For this assignment, I am providing a main program to test each function; the main program obtains input from the user and calls the function. All you need to do is "fill in the blanks" of the provided code. To obtain the provided code, locate the desired program with your browser by following the links below. You can then use one of two methods to save it into a file: It may also be possible to get the code into a file by cutting and pasting from the browser window.

Problems

Multiplication and division

Suppose you have been unfortunate enough to buy a computer whose processing chip has a bug in its multiplication and division functions, such that if you write in a program
    cout << 2 * 3 << ", " << 6 / 3 << endl;
you might not get the expected result "6, 2".

A mathematician friend reminds you that you can perform multiplication via repeated addition, and division by repeated subtraction, and provides the following recursive definitions:

    n * m =
        0, if n = 0
	m + (n-1) * m, if n > 0

    n / m =
        0, if n < m
        1 + (n-m) / m, if n >= m

Use this definition to write a recursive C++ functions to multiply two non-negative integers without using the C++ "*" operator, and a recursive C++ function to divide a non-negative integer by a positive integer without using the C++ "/" operator. Start with incomplete programs multiply.cc and divide.cc and replace the comments beginning "ADD YOUR FUNCTION HERE" with your functions.

Counting occurrences of an element in an array

Write a recursive C++ function that, given an array of integers and another integer X, returns how many times X occurs in the array. Start with the incomplete program count_in_array.cc and replace the comments beginning "ADD YOUR FUNCTION HERE" with your function.

Here are some example executions of this program. (What the user types is in boldface.)

    $ a.out
    Enter 6 numbers for array:
    2 4 2 2 3 1
    Enter number to search for:
    5 
    5 occurs 0 times in the array

    $ a.out
    Enter 6 numbers for array:
    2 4 2 2 3 2
    Enter number to search for:
    2
    2 occurs 4 times in the array

Converting text to uppercase

Write a recursive C++ function that, given an array of characters containing a null-terminated string, converts all the characters in the array to uppercase. Start with the incomplete program capitalize.cc and replace the comments beginning "ADD YOUR FUNCTION HERE" with your function.

Here are some example executions of this program. (What the user types is in boldface.)

    $ a.out
    Enter a line of text:
    abcd xyzw hello!
    In uppercase, that line is:
    ABCD XYZW HELLO!

    $ a.out
    Enter a line of text:
    1234 testing TESTING 5678
    In uppercase, that line is:
    1234 TESTING TESTING 5678

Finding the greatest common divisor

Write a recursive C++ function that finds the greatest common divisor of two positive integers (the largest number that evenly divides both), using the algorithm described on page 710 of the textbook. Start with the incomplete program gcd.cc and replace the comments beginning "ADD YOUR FUNCTION HERE" with your function.

What to turn in

Submit your source code as described in the guidelines for programming assignments. For this assignment: