Due Monday, April 10


CS 2 -- Homework Assignment #1

You should first download the following library for rational number class rational.h and rational.cpp . To test this library compile it and link it to the following client program: client.cpp .

Your assignment is to modify this class by adding a public mutator Reduce() which reduces the given rational number object to simplest form. This is accomplished by dividing both the numerator and denominator by their greatest common divisor (GCD). The following function implements Euclid's GCD algorithm euclid.cpp.

You should include code for the public member functions for Subtraction, Inverse (Reciprical), and Division of Rational objects. Finally you should write a client program which takes as input a positive integer N and computes the Harmonic Number H(N)= 1+ 1/2 + 1/3 + 1/4 + ... + 1/N. Your program should begin by demonstating overloaded operators for subtraction and division (- and /). After this the program should prompt the user to enter an integer N, and then it should compute the Harmonic Number H(N). Your program should include two procedures, one which computes H(N) without using the Reduce() functions, and another which applies Reduce() after each addition. The program should continue to prompt the user and compute other Harmonic numbers until the user is finished.

Run experiments to determine how well these procedures perform. Determine if there is a value of N for which the first procedure fails due to overflow, but the second is sucessful. Include your findings in a comment at the beginning of your code. Follow good programming style with clear documentation.

To submit your homework you should create a tarfile called prog1.tar which contains all the required source code and a makefile needed for successful compilation. You will e-mail this tar file to the TA by Monday April 10. A hard copy of your program should be submitted at the beginning of class on the due date.