FloridaTech - College of Engineering
CSE1001 - LABORATORY
Author: Jamal Faik
Lab Assignment 10 : Exceptions
An Example
...function safe_division (a, b : Integer) return Integer is
......q : Integer;
...begin
......q := a / b;
...exception
......when CONTSRAINT_ERROR => return Integer'Last;
...end safe_division;
Another Example
...procedure P is
...begin
......P;
...exception
......when STORAGE_ERROR => P;
...end P;
Problem 1
Define a type Day than models the days of the week then
write a function Tomorrow that returns the following day of a
certain day. You function should not contain any control statement.
Problem 2
Rewrite the function factorial that computes the factorial of
an integer (n) according to the following rules :
- if n < 0 then factorial(x) = -1;
- if n = 0 then factorial(x) = 1;
- if n > 0 then factorial(n) = factorial(n-1) * n..However, if
factorial(n) > Integer'last then return Integer'last.
Note : No if or case statement are allowed in this function
Problem 3 : A Compression Idea
Write a program that reads a file of Naturals and ouput one
integer which we would call the signature of the file. Values other
than integers should be ignored. The mechanism of producing one
integer from a list of Naturals relies on Cantor bijection between N x N and N (where N is the set of Naturals).
Cantor(x,y) = (x+y-1)(x+y-2)/2 + y
The procedure to follow to produce the signature of a file is
the following :
Suppose your file contains the following numbers :
10 11 12 13 14 15 16 17 18 19 20
- you merge 10 and 11 by applying Cantor(10,11) = R1
- then you merge R1 with 12 by applying Cantor(R1, 12) = R2
- then you mege R2 with 13 to produce R3 and so on.
- Let Rn the number you got after mergin all the numbers. You compute
the signature of the file by merging Rn with
the number of naturals
in the file (in the example, signature = cantor(Rn, 11).
The process is (uniquely) reversible and from the signature of the
file, you can produce one unique list of numbers (composing the file)