CSCI 3200 - Fall 2023 - Project 3

Due by 11:59pm on Friday, December 1, in my email inbox. Send your program as an attachment.

The late penalty is 5% per hour.

Using the language you selected, write a program to solve the problem below. The precise input and output format is specified (including filenames), and your output must match byte-for-byte. Your program must consist of one source file called "round.zzz", where zzz is an appropriate extension for your language.

Grading will be on a 100-point scale, based on an assortment of test cases I have prepared.

Rounding Rational Numbers (using Farey sequences), with thanks to Joanna Aguilera

Rational numbers are numbers that are ratios of two integers. In other words, if r is a rational number then there are two integers p and q (with q ≠ 0) such that r = p/q. If p and q are chosen so that they have no common factors (i.e. their greatest common divisor is 1; gcd(p, q) = 1) then we say that the ratio is in lowest terms. For example 6/13, 27/4, and 24/125 are all rational numbers in lowest terms.

There are times when a ratio in lowest terms consists of unruly or awkward integers that make it difficult to quickly or easily understand the relative value of the rational number they represent. Often these numbers are very close to other rational numbers that are easier to interpret because in lowest form they consist of smaller integers. For example, 6/13 is close to 1/2, 27/4 is close to 7/1, and 24/125 is close to 1/5.

Wim Lewis has published a web page that details an algorithm for finding the closest rational number to a given p/q, such that both the integers in that ratio are at most some value N.

Write a program that includes an implementation of this algorithm, and applies it to a list of rational numbers read from a text file.

Input: Each line of the input file contains three positive integers (except the last line, which contains a single 0). The first number is p, the second number is q, and the third number is N.

Output:For each line in the input file, display the rational number closest to it within the given bound.

Example Input: Example Output:
58 129 10
58 129 7
25 56 10
24 125 25
24 125 15
13 41 10
0
4/9
3/7
4/9
4/21
1/5
1/3

Rules