Section 0101, 0102, 0103 and Honors, Fall 1995

Project 3

Due: Wednesday, November 1, 1995

Objective

The objective of this assignment is to practice writing functions for a pre-defined interface.

Assignment

Your assignment is to write a set of functions to work with fractions. Since a fraction has two parts (a numerator and a denominator), we need a data structure that can hold two integers. We define a new data type called fraction using as follows:
    typedef struct {
        int top ;
        int bottom ; 
    } fraction ;
Using this declaration, we can use fraction to declare variables, parameters and function return types with type fraction. If a variable f has type fraction, then we can access the numerator using f.top and the denominator using f.bottom. This mechanism will allow us to return a fraction from a function. We will use the a normal form to store a fraction. First, only the numerator is allowed to be negative. Second, the numerator and the denominator should not have any common divisors. You must implement are the functions listed below.

Functions to implement

   fraction ReduceFraction (fraction f) ;
Given a fraction f, return the fraction in normal form. For example, 6/8 becomes 3/4. You should use Euclid's GCD algorithm from your textbook (page 196) to find the greatest common divisor of the numerator and denominator.
   fraction AddFraction (fraction f1, fraction f2) ;
   fraction SubtractFraction (fraction f1, fraction f2) ;
   fraction MultiplyFraction (fraction f1, fraction f2) ;
   fraction DivideFraction(fraction f1, fraction f2) ;
These four functions do the obvious thing: return the sum, difference, product and quotient of the two parameters. Figure out how you would do this by hand before trying to write your functions.
   bool EqualFraction(fraction f1, fraction f2) ;
If the two parameters are equal then the function returns the Boolean value TRUE. Otherwise, the function returns FALSE.
   bool LessThanFraction(fraction f1, fraction f2) ;
If the fraction stored in f1 is strictly less than the fraction stored in f2, then the function returns TRUE. Otherwise, the function returns FALSE.

Implementation Notes

There are several files that you should copy for this project. They are located in the directory:

    ~chang/pub/cs201/project3/

The files are called proj3.h, proj3.c, test3.c and main3.o. The header file proj3.h contains the type definition for the type fraction and function prototypes for the functions you have to implement. The file proj3.c contains a template for your project and the implementations for the following 4 functions to help you get started (you must keep these functions in your project):

   int Numerator (fraction f) ;
   int Denominator (fraction f) ;
These two functions return the numerator and denominator of the given fraction.
   fraction MakeFraction (int numerator, int denominator) ;
This fraction is given a numerator and a denominator and returns a fraction with the given numerator and denominator. The returned fraction is in normal form. This function uses the ReduceFraction function that you have to implement. So you can't use this function until you have implemented ReduceFraction.
   void PrintFraction (fraction f) ;
This function simply prints out the contents of the fraction.

Your assignment is to implement the functions defined above. What you turn in is a file that contains only the functions and does not include a main function. Of course, you may include a main function in your file when you test your functions, but these should be removed when you turn in your project.

You should not alter the contents of the file proj3.h in any way. The file test3.c contains a sample main program that calls some of the functions in proj3.c. You should change this program to test and debug your functions. You can compile your project and the test program using the following command:

   cc201 test3.c proj3.c

Run the resulting program and test it using difficult cases. Change the test3.c file to check your functions as you implement them one by one.

What to turn in

When you are certain that your program works correctly, compile it with main3.o:

   cc201 main3.o proj3.c

Run the program and check that it works correctly. Then run your program again and redirect the output to a file called output using the command:

   a.out >output

If your run this command more than once, UNIX will complain that the file output already exists. You should remove the old version of output before you issue this command again. Turn in your project by running the mail2chang command. Use proj3.c and output as the name of the source code and the name of the output file respectively.