## PIC 10B

(Sec. 1a-1b)

C++ Programming and Problem Solving

## Instructor: Ivo Dinov, Asst. Prof.Neurology, Mathematics, Statistics

 http://www.math.ucla.edu/~dinov/10b.1.011/

Due Date:

# Tuesday, 8:00 PM, Aug. 14, 2001

Please, submit your homework electronically using the "submit" system. Please try to follow the "good programming" conventions we discussed in class. Write modular code and include all of your functions in the same CPP file as your main.

• (HW_6_1.cpp/exe) Write a program that defines a class DivideByZeroException to be used in exception handling for throwing an exception on a division by zero. You need to provide a default and a non-trivial (string parameter) constructor for DivideByZeroException, as well as a private member message and public set(string) and what() member functions which allow the end user to manipulate the message reported. Inside your driver main() have a function call to a method quotient which throws an exception (object) of type DivideByZeroException upon attempt to compute the quotient, when the divisor is equal to zero. The user is prompted to enter a pair of numbers, until an exit (Q/X) is requested, and the quotient of the two numbers is reported provided it's well defined (divisor!=0). Note, quotient should be a template function accepting all primitive types of numbers.

Sample Run:
%> This program illustrates generating, throwing and catching an exception of type DivideByZeroException.
%> Enter two numbers and we'll report the quotient, (Q/X) to exit:
%> 100.0 10
%> The quotient is: 10.0
%> Enter two numbers and we'll report the quotient, (Q/X) to exit:
%> 100.0 0
%> Exception occurred: attempted to divide by zero ...
%> Enter two numbers and we'll report the quotient, (Q/X) to exit:
%> 33 9
%> The quotient is: 3.66667
%> Enter two numbers and we'll report the quotient, (Q/X) to exit:
%> x
%> Bye!

\Ivo D. Dinov, Ph.D., Department of Neurology and Program in Computing, UCLA School of Medicine/