Assignment5 - which can be simplified by finding the gcd of...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Department of Computer Science The University of Hong Kong CSIS1117A Computer Programming Assignment 5 Due Date: 23:59, Nov 29, 2009. You may assume all input are valid in this exercise. Write a program to do fraction arithmetic. Your program should consists of 3 functions: get_fraction() and add_fraction() and print_fraction(). struct Fraction { int integral; //integral part of the fraction int num; //numerator of the fraction int denom; //denominator of the fraction, num < denom, and they // are relatively prime, i.e. No common factors. } Fraction get_fraction() //post: return a fraction from 3 integers read from cin, in the order of //integral, num & denom. void print_fraction(Fraction data) //pre: data is a valid Fraction //post: fraction are output as integral{num/denom}, such as 1{2/3} Fraction add_fraction(Fraction a, Fraction b) //pre: a, and b are valid Fractions //post: return the sum of a and b as a valid Fraction Note that a/b + c/d = (ad+bc)/bd,
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: which can be simplified by finding the gcd of the numerator and denominator. Write the gcd function using recursion . Your program should be able to store any number of Fractions in a dynamic array. Find the sum of them using a simple loop, assuming that there are more than 2 input fractions. Remember to free the array after use. Enter number of Fractions: 5 Enter Fraction 1: 1 5 6 Enter Fraction 2: 3 2 3 Enter Fraction 3: 2 1 2 Enter Fraction 4: 0 1 6 Enter Fraction 5: 0 2 3 Their sum is 8{5/6} Notice: 1: Please just hand in source code file (.cpp). The filename should be “assignmentFive.cpp”. 2: The statement “system (&quot;PAUSE&quot;)” should be removed. The following is a non-recursive gcd function. (For you start writing your program even recursion has not been discussed. In final program please use recursion function ) int gcd(int a, int b) { while (b != 0){ int t; t = b; b = a % b; a = t; } return a; }...
View Full Document

Page1 / 2

Assignment5 - which can be simplified by finding the gcd of...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online