UMBC CMSC201, Computer Science I, Spring 1994 Sections 0101, 0102 and Honors

Project 3

Due: April 14, 1994

Click here for the latest news and hints.

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 consists of two numbers (a numerator and a denominator), you are provided with some functions that work with pairs of numbers. These functions work with a new type called pair which holds a pair of integers. Using this new type, you can declare variables of type pair, pass a pair variable as a parameter to a function, and return a pair value from a function. The functions provided to you are:

int FirstPart (pair p) ;
This function returns the first integer in the pair value p.

int SecondPart (pair p) ;
This function returns the second integer in the pair value p.

pair MakePair (int one, int two) ;
This function makes a pair value whose first part is the value contained in one and second part is the value contained in two.
The functions above are provided in the file pair.o which you can find in the directory:
 ~chang/pub/cs201
You should copy this file as well as the files main3.o and proj3.h to your own directory. The file main3.o is a driver program that calls the functions that you will implement. The file proj3.h contains the predefined interface between main3.o, pair.o and the functions that you implement. You may not change any of these files. Your assignment is to implement the following functions.

int Numerator(pair p) ;
Return the numerator of the fraction stored in the pair p.

int Denominator(pair p) ;
Return the denominator of the fraction stored in the pair p.

pair MakeFraction(int numer, int denom) ;
Make a fraction with numerator numer and denominator denom.

pair ReduceFraction (pair p) ;
Given a fraction stored in p, reduce the fraction to the normal form by dividing the numerator and denominator by their greatest common divisor. For example, 6/8 becomes 3/4. You should use Euclid's GCD algorithm for this function. The function then returns the reduced fraction.

void PrintFraction (pair p) ;
Print out the numerator and denominator of the fraction p. We need this since printf does not print out pairs. The output is sent directly to the screen, so this function does not return any values. The output should be of the form numerator/denominator; for example, 3/4.

pair AddFraction (pair p, pair q) ;
Given two fractions, return the sum of the fractions. The result should be put in normal form, using ReduceFraction.

pair SubtractFraction (pair p, pair q) ;
Given two fractions p and q, return the result of p - q. The result should be put in normal form, using ReduceFraction.

pair MultiplyFraction (pair p, pair q) ;
Given two fractions, return the product of the fractions. The result should be put in normal form, using ReduceFraction.

pair DivideFraction(pair p, pair q) ;
Given two fractions p and q, return the result of p divided by q. The result should be put in normal form, using ReduceFraction.

Implementation Notes:

Your final product is a set of functions. To compile your functions (saved in the file proj3.c) with the driver program in main3.o and the pair functions pair.o, use the UNIX command
     % cc201 proj3.c main3.o pair.o
   
The output of the complete program should look like:
    Testing: 2, 3, 3, 4
    Reduce: 2/3 = 2/3
    Reduce: 3/4 = 3/4
    (2/3) + (3/4) = 17/12
    (2/3) - (3/4) = -1/12
    (2/3) * (3/4) = 1/2
    (2/3) / (3/4) = 8/9

    Testing: 3, 4, 1, 8
    Reduce: 3/4 = 3/4
    Reduce: 1/8 = 1/8
    (3/4) + (1/8) = 7/8
    (3/4) - (1/8) = 5/8
    (3/4) * (1/8) = 3/32
    (3/4) / (1/8) = 6/1
    .
    .
    .

Warning:

Take heed of these warnings.

What to turn in:

When you are done, use the script command (see Project 2 description) to record the output of your program. Then, send the typescript file along with the file containing your functions to chang@gl.umbc.edu. Do not send a.out, main3.o, or any other files. Do not send your files in MIME format, your project will be returned unread.
Last Modified: June 23, 1994

Richard Chang, chang@gl.umbc.edu