Lab Fraction Calculator - Programming I | COMP 150, Lab Reports of Computer Science

Material Type: Lab; Class: Programming I; Subject: Computer Science; University: Harding University; Term: Unknown 1989;

Typology: Lab Reports

Pre 2010

Uploaded on 08/16/2009

koofers-user-gp3
koofers-user-gp3 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Comp 150 / 170 Lab: Fraction Calculator Page 1 of 6
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 show 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. Pages 4 & 5 contain flowcharts for the
GCD (Greatest Common Divisor) and LCM (Least Common Multiple) algorithms. Page 6 contains the code for the main
function of fraction.cpp that is available at \\cs1\Classes\Comp170\fraction.cpp. Copy this file to your computer so that you
can complete the program.
This lab will be due at the beginning of the next class period. Complete the four functions and submit your working program
to Easel (http://cs.harding.edu/easel/). Make sure your name and date are documented at the top of the program.
pf3
pf4
pf5

Partial preview of the text

Download Lab Fraction Calculator - Programming I | COMP 150 and more Lab Reports Computer Science in PDF only on Docsity!

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/ 2/4 + 3/4 = 5/

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 show 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. Pages 4 & 5 contain flowcharts for the

GCD (Greatest Common Divisor) and LCM (Least Common Multiple) algorithms. Page 6 contains the code for the main

function of fraction.cpp that is available at \cs1\Classes\Comp170\fraction.cpp. Copy this file to your computer so that you

can complete the program.

This lab will be due at the beginning of the next class period. Complete the four functions and submit your working program

to Easel (http://cs.harding.edu/easel/). Make sure your name and date are documented at the top of the program.

main

ReduceFraction CommonDenominator

GreatestCommonDivisor LeastCommonMultiple

GreatestCommonDivisor

x

y

Greatest Common Divisor of x and y

LeastCommonMultiple

x

y

Least Common Multiple of x and y

ReduceFraction ReduceFraction

Numerator

Denominator

Reduced Numerator

Reduced Denominator

CommonDenominator

Numerator 1

New Numerator 1

Denominator 1

New Denominator 1

Numerator 2

New Numerator 2

Denominator 2

New Denominator 2

CommonDenominator

1/2 and 2/3 in

3/6 and 4/6 out

1 3 2 6 2 4 3 6

Structure Chart

Function Diagrams

Start LCM of X & Y

Is X > Y

C = X C = Y

Ans = 0

Is Ans=0?

Are R1 and R2 both 0?

C = C + 1 Ans = C

Return Ans

Stop

yes no

no

yes

no yes

R1 = remainder of C

divided by X

R2 = remainder of C

divided by Y

Start GCD of X & Y

Is t = 0

x = y

y = t

return y

t = remainder of

x divided by y

Stop

yes

no

t = remainder of

x divided by y