Fraction Calculator
Functions Lab
Comp 150 / 170
The goal of this lab is to give you more practice in using functions that return values through the return statement and through the parameter-list. You will complete a program that implements a simple fraction calculator. When the program is completed it should run according to the example below.
Add, subtract, multiply & divide – positive
fractions only
Enter ‘0/0 + 0/0’ to quit.
>1/2 + 3/4
2/4 + 3/4 = 5/4
>2/16 + 29/32
4/32 + 29/32 = 33/32
> 1/7 + 1/5
5/35 + 7/35 = 12/35
>1/2 – 1/3
3/6 – 2/6 = 1/6
>200/100 * 25/50
2/1 * 1/2 = 1/1
>1/2 * 3/4
1/2 * 3/4 = 3/8
>1/2 / 3/4
1/2 / 3/4 = 2/3
>0/0 + 0/0
Note that the program will add, subtract, multiply, and divide positive fractions only. For subtraction, the lager fraction must be given first so that the answer will be positive also. All answers will be fractions in lowest terms. For purposes of simplifying this assignment, a whole number such as 5 will be written as 5/1.
On the second page is a structure chart that shows how the functions that you will write will be called by one another. Also given are some diagrams that illustrate what each of the four missing functions is to do. On the third page is a brief description for each function that restates in English what the diagrams are saying. Page 4 contains flowcharts for the GCD (Greatest Common Divisor) and LCM (Least Common Multiple) algorithms. Start with FRACTION.CPP that is available on the course calendar. Copy the contents of this file to yours to begin.
This lab will be due at the beginning of the next class period. Complete the four functions and turn in the source code on easel. Make sure your name and date are documented at the top of the program.

The GreatestCommonDivisor function receives two integers from the calling program and returns a single integer that is their GCD. For example:
X = GreatestCommonDivisor(30, 42);
Will result in a value of 6 for X. It is assumed that all integers are positive and are within the normal range for the int data type.
The LeastCommonMultiple function receives two integers form the calling program and returns a single integer that is their least common multiple. For example:
X = LeastCommonMultiple(10, 15);
Will result in a value of 30 for X. It is assumed that all integers are positive and are within the normal range for the int data type.
The ReduceFraction
function receives two integers from the calling program, and it is assumed that
the first integer is the numerator and that the second integer is the denominator
of the fraction. The Reduce function
calls the GreatestCommonDivisor
function and uses the information returned to alter both the numerator and
denominator so that the values sent back to the calling program are an
equivalent fraction to the one received, but in lowest terms. For example:
X = 2;
Y = 4;
ReduceFraction(X, Y);
Will result in X being changed to 1 and Y being changed to 2 since the original data represents 2/4 and the lowest terms version of 2/4 is 1/2.
The MakeDenominatorsCommon function receives four integers from the calling program, and it is assumed that the first two integers represent the numerator and denominator of one fraction and that the second two integers represent the numerator and denominator of a second fraction. This function will call the LeastCommonMultiple function to find a common denominator and then alter both fractions so that the values returned to the calling program are equivalent fractions with a common denominator. For example:
Numerator1 = 1;
Numerator2 = 2;
Denominator1 = 2;
Denominator2 = 3;
MakeDenominatorsCommon
(Numerator1, Denominator1, Numerator2, Denominator2);
Will result in Numerator1 being changed to 3 and Denominator1 being changed to 6. It will also result in Numerator2 being changed to 4 and Denominator2 being changed to 6. This is due to the fact that the original data represents 1/2 and 2/3 and the commonly denominated equivalents are 3/6 and 4/6.
