III. Friday problems with C modules A. rational numbers ------------------------------------------ FOR YOU TO DO Implement Rational Numbers, as specified by the following header file. // rational.h #ifndef RATIONAL_H #define RATIONAL_H 1 #include typedef int *ratl; /* requires: denom != 0; * ensures: result is a fresh rational number whose * numerator is num and denominator is denom */ extern ratl rmake(int num, int denom); // ensures: result is the numerator of r extern int numerator(ratl r); // ensures: result is the denominator of r extern int denominator(ratl r); // ensures: result is the sum of x and y extern ratl radd(ratl x, ratl y); // ensures: result is the arithmetic inverse of r extern ratl rnegate(ratl r); // ensures: result is the product of x and y extern ratl rmult(ratl x, ratl y); // requires: the numerator of r is not 0 // ensures: result is the multiplicative inverse of r extern ratl rinverse(ratl r); // ensures: result is true just when r1 and r2 are mathematically equal extern bool requals(ratl r1, ratl r2); #endif ------------------------------------------ ------------------------------------------ DESIGN DECISIONS - How to represent rationals? - How does the representation relate to the mathematical rationals? - Any other possibilities? ------------------------------------------ ------------------------------------------ IMPLEMENTING RMAKE #include "rational.h" #define ELEMS 2 ratl rmake(int num, int denom) { ------------------------------------------ How can we return our representation for a rational? ------------------------------------------ FOR YOU TO DO Implement the following functions: // ensures: result is the numerator of r extern int numerator(ratl r); // ensures: result is the denominator of r extern int denominator(ratl r); // ensures: result is the sum of x and y extern ratl radd(ratl x, ratl y); // ensures: result is the arithmetic inverse of r extern ratl rnegate(ratl r); // ensures: result is the product of x and y extern ratl rmult(ratl x, ratl y); // requires: the numerator of r is not 0 // ensures: result is the multiplicative inverse of r extern ratl rinverse(ratl r); // ensures: result is true just when r1 and r2 are mathematically equal extern bool requals(ratl r1, ratl r2); ------------------------------------------ B. complex numbers