CS 210 ­ Fundamentals of Programming I  Fall 2012 ­ In­class Exercise for 10/29/2012

advertisement
CS 210 ­ Fundamentals of Programming I Fall 2012 ­ In­class Exercise for 10/29/2012
(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 10/24/2012. 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 10/24//2012 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 the extern keyword at the beginning of each function prototype.
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. Some additional specifications for the operations from last class:
●
create_rational – receives two integers, the numerator and denominator, and returns a rational_t object of equivalent value that has a positive denominator. This function should check if the denominator is 0, and if so, it should display the error message "Error: division by 0. Returning 0/1." (without the quotes) and return a result representing rational number 0/1.
●
create_divide – receives two rational_t objects and returns a result rational_t object that is the quotient of the parameters that has a positive denominator. E.g., for 3/4 and ­5/2, the result would be ­6/20. This function should check if the numerator of the second rational_t object is 0, and if so, it should display the error message "Error: division by 0. Returning 0/1." (without the quotes) and return a result representing rational number 0/1.
3. Add an include statement for rational.h in ratdriver.c
4. 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. The prototype goes in rational.h and the definition goes in rational.c
10/27/2012
Page 1 of 2
D. Hwang
5. At the end of the program in ratdriver.c, create rational number 22/7 using function create_rational and assign the result to variable z. Afterward, write code, where indicated, to print out "22/7 is the value of z." (without the quotes) using rational_print to display the value of z. (I.e., the 22/7 part of the output must be from the function).
When you have completed the exercises from last Wednesday 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. 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 16­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 Programming Assignment 6 unless you have completed it.
(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:
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.
10/27/2012
Page 2 of 2
D. Hwang
Download