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

advertisement
CS 210 ­ Fundamentals of Programming I Fall 2012 ­ In­class Exercise for 10/24/2012
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 Monday, October 29. Both exercises are due together by end of class on Monday. However, you are expected to have this part done before class on Monday.
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, respectively.
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".
● create_rational – receives two integers, the numerator and denominator, and returns a rational_t object of equivalent value that has a positive denominator.
● 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.
●
10/23/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 Monday, October 29. This part is expected to be completed before class on Monday. Reminder: if you get done with this part before the end of the class period, you are expected to work on Homework 6 or start Programming Assignment 6.
10/23/2012
Page 2 of 2
D. Hwang
Download