CS 210 ­ Fundamentals of Programming I  Spring 2012 ­ In­class Exercise for 03/28/2012 & 03/29/2012

advertisement
CS 210 ­ Fundamentals of Programming I Spring 2012 ­ In­class Exercise for 03/28/2012 & 03/29/2012
(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 03/21/2012 & 03/22/2012.
Assignment
Open the in­class exercise project from 03/21/2012 & 03/22/2012. 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 a prototype and function definition for the following function. The prototype goes in rational.h and the definition 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. This function should check that the denominator parameter is not 0. If it is, the function should display the error message "Error: denominator is 0. Returning 0/1." and return a result representing rational number 0/1. (This is the same idea as is done in rational_divide.
● Change the initialization of variables x and y from using explicit initialization to calling make_rational. ●
When you have completed the exercises from last Wednesday/Thursday 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. To zip multiple files together, select each file using Ctrl­Left­Click (this will form a selected group), then Right­
Click to zip as usual. Submit under assignment 18­IN9 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 7 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 03/27/2012
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 single rational_t object. This function reduces the rational number parameter so that: ● The denominator is non­negative. 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 as an argument before returning in the functions that create a 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.
03/27/2012
Page 2 of 2
D. Hwang
Download