CS 210 ­ Fundamentals of Programming I  Spring 2016 ­ In­class Exercise 9b for 3/30/2016 & 3/31/2016

advertisement
CS 210 ­ Fundamentals of Programming I Spring 2016 ­ In­class Exercise 9b for 3/30/2016 & 3/31/2016
(15 points) This exercise consists of a coding part only. The purpose of this exercise is to continue working with structs and to create a personal library. This exercise assumes the completion of the exercises from 3/23/2016 or 3/24/2016. Create a new empty CodeBlocks project. Download file ratdriver.c from the course website and save it in the project folder. Add this file to the project.
Problem Statement
Types like rational_t may be useful for more than one project. While we always can copy and paste the code
for a type from one program file to another, a better way make a type and its operation functions available to more
than one program is to create a personal library.
Assignment
Open the main.c program file from the 3/23/2016 or 3/24/2016 exercise (File­>Open). Complete the following exercises that develop a personal library for the rational number type:
1. Create a new header file (File­>New; select C/C++ header) rational.h for the rational number type that is
added to the current project. Copy the rational_t type definition and the function prototypes into this file. Add an include statement for rational.h in ratdriver.c
2. Create a new C source file rational.c for the rational number type that is added to the current project. Copy the function definitions into this file. Add includes for rational.h and stdio.h to this file.
3. Write prototypes and function definition for accessor functions get_numerator and get_denominator that receive a rational number and return the numerator or denominator, respectively, of the rational number. The prototypes go in rational.h and the function definitions go in rational.c 4. At the end of the program in ratdriver.c where indicated, test the accessor functions by using them to display the numerator and denominator of the variable x in the following format (without the quotes): "x's numerator is 3 and its denominator is 4"
5. Write a prototype and function definition for the function rational_print that receives a rational number
and displays the rational number in n/d format on the screen. As with rational2a, there should not be a newline at the end of the output. The prototype goes in rational.h and the function definition goes in rational.c
6. At the end of the program in ratdriver.c, where indicated, write code to display the value of the variable x in the following format (without the quotes): "3/4 is the value of x", using rational_print to display the value of x. (I.e., the "3/4" part of the output must be from calling the function).
03/29/2016
Page 1 of 2
D. Hwang
When you have completed the exercises from last class and today, put your name in a comment at the top of each file. Find your project folder using Windows Explorer, and zip up your ratdriver.c, rational.h, and rational.c files together into one zipfile. 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
17­IN9 as usual. The submission system will check you have submitted the correct files, compile your code, and test it. Reminder: if you get done with this exercise before the end of the class period, you are expected to work on Homework 7 and/or Programming Project 7.
(OPTIONAL) Enrichment Exercise Submit your files first, before you attempt this 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 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 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 9/12 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, r and s:
1. If r < s then swap values so that s < r
2. While s > 0 do
2.1 Save the value of s
2.2 Recompute s as the remainder of r divided by s
2.3 Set r to the saved value of s
3. r is now the greatest common divisor After implementing this function, it can be used in a couple different ways. It can be called at the end of every function that returns a new rational_t object (e.g. rational_add, etc.) or it can be called just at the end of
create_rational and all of the other functions call create_rational when a new rational_t object
is needed. Test this function 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. Likewise, creating a new rational_t object with a numerator of 9 and a denominator of 12
should result in rational number 3/4.
03/29/2016
Page 2 of 2
D. Hwang
Download