[an error occurred while processing this directive]


CMSC201
Programming
Project Three

Rational Arithmetic
Computing with fractions

out Thursday 10/10/96
due Friday 10/25/96 at Midnight

Introduction

The objective of this assignment is to practice writing functions for a pre-defined interface and to give you an initial exposure to the concept of a C structure .

Your assignment is to write a set of functions to work with fractions or rational numbers. Recall that a rational number is defined as any number that can be expressed as a ratio of two integers. This is just what we commonly call a fraction.

Representing a fraction

Since a fraction has two parts (a numerator and a denominator), we will need a new kind of object to represent a fraction -- a data structure that can hold two integers. We will study data structures in more detail later in the semester. For this assignment, we will provide a very brief introduction in lecture on how to declare and use very simple data structures.

To represent a rational number or fraction, we will define a new data type called fraction which has two parts, a top which is an integer and a bottom which is an integer.

    typedef struct {
        int top ;
        int bottom ; 
    } fraction ;
Using this declaration, we can use the type 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 the expression f.top and the denominator using the expression f.bottom. This mechanism will allow us to return a fraction from a function. Here is a simple example of how we might use variables of type fraction in some code:
  ...
  fraction oneThird, oneHalf, oneSixth;

  oneHalf.top = 1;
  oneHalf.bottom = 2;

  oneThird.top = 1;
  oneThird.bottom = 3;

  oneSixth = multiplyFraction(oneThird, oneHalf);
  ...

We will use the a "canonical form" or "normal form" to store a fraction. All this means is that we will always follow certain conventions for representing fractions. First, only the numerator is allowed to be negative. Second, the numerator and the denominator should not have any common divisors.

Functions to implement

You must implement the following functions which operate on fractions and return fractions.
   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 (see out text, 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.

An example

As an example, consider the following definition of function SquareFraction.
     fraction SquareFraction(fraction f)
     {
         return(ReduceFraction(MultiplyFraction(f,f)));
     }
Is the call to ReduceFraction really necessary in this case?

Implementation Notes

There are several files that you should copy for this project. They will be located in the directory:
    ~finin/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 proj3.c contains a template for your project and the implementations for the following four 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.

Some comments

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 using submit. Use proj3.c and output as the name of the source code and the name of the output file, respectively. [an error occurred while processing this directive]