Goals

Checklist

  • Use the submit command: /u/cs126/bin/submit 3 readme RAT.h RAT.c RATbetter.c e.c. Do not use different file names. Ask a lab TA how to create directories if you're unsure. Do not submit a.out.

  • In precept we will discuss the basic client, interface, implementation approach. The goal of this assignment is to create a library (much like stdio.h) to do rational arithmetic.

  • readme
  • name, precept number
  • description of problems encountered and high level description of code
  • be sure to include an English description of your "better" method for adding and multiplying rationals which staves off overflow
  • output of e.c with RAT.c (print out 15 terms - in your readme file indicate which terms are garbage created from overflow)
  • output of e.c with RATbetter.c (print out 15 terms - indicate which ones are garbage created from overflow)
  • detail any help (if any) that you received from lab TA's
  • RAT.h
  • just type exactly as is
  • RAT.h is the interface - it provides a list of the functions that your client program can use (of course, you will have to implement these functions in RAT.c)
  • RAT.c
  • implement naive version of RATadd, RATmul, RATshow, RATinit
  • use sample client program RATtest.c to test your code
  • e.c
  • use library of rational functions that you created to compute e using the Taylor expansion 1/0! + 1/1! + 1/2! + 1/3! + . . .
  • you may want to write a factorial function - a good opportunity to practice coding a recursive function (it can also be done with a traditional while or for loop)
  • it is possible to get 13 terms using a well-written RATbetter.c implementation
  • the largest integer that can be stored as an int on the arizona machine flagstaff is 2,147,483,647; there is no way you can possibly store the denominator of the 15th term of e, since it would require too many digits
  • RATbetter.c
  • RATbetter.c replaces RAT.c, so use the naive version as a starting point
  • implement improved versions of RATadd, RATmul, and RATinit
  • before you begin coding, do some arithmetic with fractions using pencil and paper and try to isolate the techniques you use to simplify fractions (some good examples are in the RATtest.c file)
  • some people find it useful to write a least common multiple (a.k.a, least common denominator) function for use with RATadd - this can be accomplished using Euclid's gcd function discussed in precept
  • don't forget to improve RATmul, even if e.c doesn't really benefit from doing this - other client program (including RATtest.c) will
  • can use sample client program RATtest.c to test your code - with an ideal RATbetter.c implementation, no overflow will occur
  • pi.c
  • there are many ways to compute pi, but the continued fraction expansion given in the course packet appears to be the most useful for getting a good rational approximation
  • compiling
  • type lcc RAT.c e.c or lcc RATbetter.c e.c - the whole benefit of client, interface, implementation is that you don't need to change the client program e.c at all
  • Additional Sources

  • For interesting reading about computing the value of pi, check out this website or this one.