CSC 161 Schedule Readings Labs & Projects Homework Deadlines Resources

Functions and Pointers

Summary
You will write a program to reduce fractions using a recursive version of Euclid's algorithm.
Objectives
Compare functional and iterative approaches while reinforcing how to use pointers to return and modify values.

Problem

In the last homework, you implemented a stand-alone program to calculate the greatest common divisor (GCD) of two positive integers using an iterative (or loop-based) version of Euclid's algorithm. The GCD may also be used to reduce fractions

Recursive GCD

You may have noticed that the algorithm can be expressed in a recursive fashion as well. (Step 2 represents the base case test and value, while Step 3 represents the recursive call to Step 1's recursive case work.)

Implement Euclid's algorithm recursively with the following function.

unsigned long gcd(unsigned long m, unsigned long n)

You should only write a single function to solve this problem and no loops.

Reducing Fractions

As noted above, the GCD is needed to reduce fractions. Use your gcd function to place appropriate reduced values in the pointer arguments of the following function.

void reduce(long numerator,
            unsigned long denominator,
            long *reduced_numerator,
            unsigned long *reduced_denominator)

Note that the numerator may be signed. Your algorithm should handle negative and improper fractions appropriately.

General Notes

Grading

In addition to the general grading guidelines for evaluation, the assignment is worth 20 points.

A five point penalty will apply to any submission that includes personally identifying information anywhere other than the references/honesty file.