CS 210 ­ Fundamentals of Programming I  Spring 2016 ­ In­class Exercise 9a for 3/23/2016 & 3/24/2016

advertisement
CS 210 ­ Fundamentals of Programming I Spring 2016 ­ In­class Exercise 9a for 3/23/2016 & 3/24/2016
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 B will be given out on Wednesday/Thursday, March 30/31. Both exercises are due together by end of class on March 30/31.
Problem Statement
A rational number is a number that can be expressed as an integer or the quotient (ratio) 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. This function also 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.
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.
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.
03/23/2016
Page 1 of 2
D. Hwang
rational_divide – receives two 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.
● 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 week after Practical Exam 2 on Wednesday/Thursday, March 30/31. Have this part completed before class next week. Reminder: if you get done with this part before the end of the class period, you are expected to work on Project 6 or Homework 7.
03/23/2016
Page 2 of 2
D. Hwang
Download