CS 210 ­ Fundamentals of Programming I  Spring 2011 ­ In­class Exercise for 03/21/2012 & 03/22/2012

advertisement
CS 210 ­ Fundamentals of Programming I Spring 2011 ­ In­class Exercise for 03/21/2012 & 03/22/2012
(10 points) This assignment is the first part of a 2­day in­class exercise, consisting of coding parts only. The purpose of this exercise is to work with structs. Create a new console project for this exercise. Part II will be given out on Wednesday/Thursday, March 28/29. Both exercises are due together by end of class next Wednesday/Thursday, March 28/29. However, you are expected to have this part done before class next Wednesday/Thursday.
Problem Statement
A rational number is a number that can be expressed as an integer or the quotient of an integer divided by a nonzero integer. Expressed as n/d, n is called the numerator and d is called the denominator. Results of the binary arithmetic operations on rational numbers represented by n1/d1 and n2/d2 are as follows:
Operation
Result
n1/d1 + n2/d2
(n1*d2 + n2*d1) / d1*d2
n1/d1 – n2/d2
((n1*d2 ­ n2*d1) / d1*d2
n1/d1 * n2/d2
n1*n2 / d1*d2
n1/d1 / n2/d2
n1*d2 / n2*d1, where n2 not equal to 0
n1/d1 = n2/d2
true when n1 *d2 = n2*d1, false otherwise
Assignment
Complete the following exercises that develop a type and operations to model a rational number:
1. Define a type rational_t that is a struct with two integer fields, num and denom, that represent the numerator and denominator for a rational number.
2. In the main program, declare rational_t variables, x, y, and z. Initialize x so that it represents the rational number 3/4. Initialize y so that it represents the rational number ­5/2.
3. Write prototypes and function definitions for the following functions:
rational2a – receives a rational_t object 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. E.g. "3/4" or "­5/2".
● rational_add – receives two rational_t objects and returns a result rational_t object that is the sum of the parameters. E.g. for 3/4 and ­5/2, the result would be ­14/8.
● rational_subtract – receives two rational_t objects and returns a result rational_t object that is the difference of the parameters. E.g. for 3/4 and ­5/2, the result would be 26/8.
●
03/20/2012
Page 1 of 2
D. Hwang
rational_multiply – receives two receives two rational_t objects and returns a result rational_t object that is the product of the parameters. E.g. for 3/4 and ­5/2, the result would be ­15/8.
● rational_divide – receives two receives two rational_t objects and returns a result rational_t object that is the quotient of the parameters. 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." and return a result representing rational number 0/1.
● rational_equal – receives two rational_t objects and returns true if the two parameters represent the same rational number; false otherwise. E.g., for 3/4 and ­5/2, the result would be false, and for 3/4 and 9/12, the result would be true.
●
4. 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.
This exercise will be continued next Wednesday/Thursday, March 28/29. This part is expected to be completed before class next Wednesday/Thursday. Reminder: if you get done with this part before the end of the class period, you are expected to work on Programming Assignment 5 unless you have completed it.
03/20/2012
Page 2 of 2
D. Hwang
Download