CS 210 ­ Fundamentals of Programming I  Fall 2011 ­ In­class Exercise for 10/31/2011

advertisement
CS 210 ­ Fundamentals of Programming I Fall 2011 ­ In­class Exercise for 10/31/2011
(10 points) This exercise consists of a coding part 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, November 2. Both exercises are due together by end of class on Wednesday, November 2. However, you are expected to have this part done before class on Wednesday.
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:
Operator
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
In addition, n1/d1 = n2/d2 when n1 *d2 = n2*d1. From this relationship, the other equality and relational operations can be defined.
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:
rational_print – receives a rational_t object and displays it on the screen in format "num/denom" (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.
●
10/31/2011
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 1/0." 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 on Wednesday, November 2. This part is expected to be completed before class on Wednesday. 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.
10/31/2011
Page 2 of 2
D. Hwang
Download