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

advertisement
CS 210 ­ Fundamentals of Programming I Spring 2013 ­ In­class Exercise for 03/27/2013 & 03/28/2013
(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 03/20/2013 & 03/21/2013. 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 03/20/2013 & 03/21/2013 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. 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 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
4. At the end of the program in ratdriver.c, where indicated, create rational number 22/7 using function create_rational and assign the result to variable z. Afterward, write code to display "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 calling the function).
When you have completed the exercises from last week 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 18­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 Assignment 7 unless you have completed them.
03/26/2013
Page 1 of 2
D. Hwang
(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. 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/26/2013
Page 2 of 2
D. Hwang
Download