

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Exam; Class: Programming Fundamentals II; Subject: Computer Science - CSCI; University: Texas A & M University-Commerce; Term: Spring 2005;
Typology: Exams
1 / 3
This page cannot be seen from the preview
Don't miss anything!
CSCI 152 Program 5 Spring 2005 Due: Friday, 1 Apr
Emphasis on: Classes
You are to create and test a class to represent a positive fraction (with integer numerator and denominator). The numerator and denominator should not be directly accessible by a client of the class.
Write class functions to provide the following operations:
Rules for fraction operations: a / b + c / d = (ad + bc) / bd a / b - c / d = (ad - bc) / bd a / b * c / d = ac / bd a / b / c / d = ad / bc a / b == c / d if and only if ad == bc a / b < c / d if and only if ad < bc a / b > c / d if and only if ad > bc
The client application should demonstrate calling the class functions so you can test to be sure your functions are working correctly.
Requirements:
Extra-Credit Opportunity Allow for negative fractions to be input (there will be a leading sign, like -1/2) or calculated. You can represent a sign either as a separate character field or by storing the numerator value as a signed number.
You may use the following two functions which will reduce a fraction to its lowest terms. In order to satisfy requirement #2 above, you should reduce the resulting fraction after doing the add, subtract, multiply and divide. You might also want to reduce the fraction itself its value is set (operation #2) or created (operation #1), to make sure that any given fraction is reduced/displayed/represented as it most reduced and simplest form.
Add these prototypes to your class specification:
void Reduce (); int GCD (int x, int y);
and add these function bodies to your class implementation:
// PRE: parameter has values assigned to its components // POST: The value returned will be the parameter fraction reduced // to its lowest terms. For example, for parameters 1/2, 4/8, 3/6, // and 5/10 the fraction 1/2 will be returned.
void Fraction :: Reduce () { int divisor;
divisor = GCD(num, den); // find greatest common divisor num = num / divisor; den = den / divisor; }
// PRE: n and d have values assigned // POST: returns the greatest common divisor (the largest integer // which can be divided evenly into both n and d). For example, if // n is 9 and d is 12 (for the fraction 9/12), the greatest common // divisor is 3 because 3 is the largest integer that divides evenly // into both 9 and 12. The greatest common divisor of 8/12 is 4.
int Fraction :: GCD (int n, int d) { if (d == 0) return n;