# Download CS 210 ­ Fundamentals of Programming I  Spring 2013 ­ In­class Exercise 9 for 03/20/2013 &amp; 03/21/2013

Survey

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts

Class (computer programming) wikipedia, lookup

Design Patterns wikipedia, lookup

C Sharp (programming language) wikipedia, lookup

C syntax wikipedia, lookup

Object-oriented programming wikipedia, lookup

C Sharp syntax wikipedia, lookup

C++ wikipedia, lookup

System of polynomial equations wikipedia, lookup

Transcript
```CS 210 ­ Fundamentals of Programming I Spring 2013 ­ In­class Exercise 9 for 03/20/2013 & 03/21/2013
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 27/28 (after Practical Exam 2). Both exercises are due together by end of class on Monday. However, you are expected to have this part done before class on 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, 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.
●
03/19/2013
Page 1 of 2
D. Hwang
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.
● 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 Wednesday/Thursday, March 27/28. This part is expected to be completed before class on 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 6, if you have not already completed it.
03/19/2013
Page 2 of 2
D. Hwang
```
Related documents