CS 210 ­ Fundamentals of Programming I  Fall 2011 ­ In­class Exercise for 11/02/2011

advertisement
CS 210 ­ Fundamentals of Programming I Fall 2011 ­ In­class Exercise for 11/02/2011
(10 points) This exercise consists of a coding part only. The purpose of this exercise is to continuing work with structs and to create a personal library. This exercise assumes the completion of the exercises from 10/31/2011.
Assignment
Open the in­class exercise project from 10/31/2011. Complete the following exercises that develop a personal library for the rational number type:
1. Create a new header file rational.h for the rational number type that is added to the current project. Move the rational_t type definition and the function prototypes into this file. Add the extern keyword at the beginning of each function prototype.
2. In the main program, include the rational.h header file (in place of the typedef and prototypes).
3. Create a new C source file rational.c for the rational number type that is added to the current project. Move the function definitions into this file. Add includes for rational.h and stdio.h to this file.
4. Write prototypes and function definitions for the following functions. The prototypes go in rational.h and the definitions go in rational.c
make_rational – receives two integer values representing the numerator and denominator components of a rational number and returns a rational_t result with those component values.
● rational2a – receives a rational_t objects and passes back a string containing the string representation of the rational number. The format of the string representation must be n/d with no spaces ●
5. In the main program, write code to test these functions. It is suggested that you write the test for each function immediately after writing the function.
When you have completed the exercises from 10/31 and today, put your name in a comment at the top of each file. Zip up your main.c, rational.h and rational.c files and submit under assignment 17­IN9 as usual. To zip multiple files together, select all files using Ctrl­Left­Click, then Right­Click to zip as usual. The submission system will only check you have submitted the correct files and compile your code. It will not run the code. Reminder: if you get done with this exercise before the end of the class period, you are expected to work on Programming Assignment 5 unless you have completed it.
(OPTIONAL) Enrichment Exercise As rational number arithmetic is done, the resulting rational numbers have larger and larger denominators. A true rational number type would reduce the results. We can do this by writing a 11/02/2011
Page 1 of 2
D. Hwang
function that will reduce a rational_t object and then call this function before returning the rational_t results. Here is a specification for this function:
rational_reduce – receives and passes back a rational_t object. This function reduces the rational number parameter so that: ● The denominator is greater than 0. For example, a rational number with value 3/­4 will be reduced to ­3/4, and a rational number with value ­3/­4 will be reduced to 3/4.
● The ratio is in reduced form. That is, the greatest common divisor of the numerator and denominator is 1. This can be achieved by dividing both the numerator and denominator by the greatest common divisor. For example, a rational number with value 6/8 will be reduced to 3/4. Note this means that all integer values (including 0) have a denominator of 1.
Here is an algorithm for computing the greatest common divisor of two positive integers:
1. If m < n then swap values so that n < m
2. While n > 0 do
2.1 Save the value of n
2.2 Recompute n as the remainder of m divided by n
2.3 Set m to the saved value of n
3. m is now the greatest common divisor Implement this function, then call it with the result rational_t object before returning in the functions that create new rational_t object (e.g. rational_add, etc.). Test it in the main program. E.g. the sum of 3/4 and ­5/2 before reducing is ­14/8. After reducing, the result will be ­7/4.
11/02/2011
Page 2 of 2
D. Hwang
Download