Thursday, August 16, 2012

Exercise in Enumeration Types

In our next Outside Assignment, we ask you to write a function.  The function is completely specified by these two lines of Ada:

```   type Triangle is (Equilateral, Isosceles, Scalene,
Not_A_Triangle);
function Tritype (Len1, Len2, Len3 : in Integer)
return Triangle;
```

From these two lines, it's obvious that your function must decide whether three integers, representing the lengths of the sides of a triangle, in fact represent an equilateral triangle, an isosceles triangle, a scalene triangle, or no triangle at all.  (An equilateral triangle has three equal sides; an isosceles triangle has only two equal sides.  A scalene triangle has sides of three different lengths, and the integers -1, 0, 5 represent no triangle.)

A test driver for your function is already written, and is in the file TRI_TEST.ADA.  A listing starts on page 10 of your printed course notes.  You need not understand the test driver.  It's sufficient to know that if your function passes all the tests, the message "Congratulations, you completed the assignment!" is shown.  For any failed tests, the test driver displays the test case, the answer from your function, and the correct answer.  This will help you in debugging your function.

The test driver, which is the main program, contains these declarations:

```   type Triangle is (Equilateral, Isosceles, Scalene,
Not_A_Triangle);
function Tritype (Len1, Len2, Len3 : in Integer)
return Triangle is separate;
```

Most compilers will allow you to edit and recompile your function as often as you like, without having to recompile the test driver.  Since type Triangle is defined in the calling program, you should not define it in your function.

To get you started, a dummy solution is given in TRITYPE.DUM.  You can copy it and edit the copy to become your real solution.  It looks like this:

```   -- Dummy solution for Outside Assignment 2
-- Edit to become your real solution.   separate (Tritest)
function Tritype(Len1, Len2, Len3 : in Integer)
return Triangle is
begin     return Not_A_Triangle;
end Tritype;

```

When you do edit the function, you'll probably want to remove or change the first two comment lines.  Don't change the lines highlighted above.  You may want to create a new variable of type Triangle, and return that variable instead of the constant Not_A_Triangle at the end of your function:

```   separate (Tritest)
function Tritype(Len1, Len2, Len3 : in Integer)
return Triangle is
begin
if ... end if;
end Tritype;
```
However, that's not the only way to solve the problem, just a suggestion.

Here are the steps to follow for Outside Assignment 2.  They're also in your printed course notes on page 12:

1. Copy TRITYPE.DUM to TRITYPE.ADA to make a copy of the dummy solution.  Also, compile the test driver TRI_TEST.ADA.  You need do this step only once.
2. Edit TRITYPE.ADA to become your real solution.  You may skip this step the first time through, to see error messages from the test driver.
3. Compile your solution TRITYPE.ADA.  If the compiler finds errors, go back to step 2.
4. Link with the name of the main program Tritest.  Then execute.  If the test driver displays error messages, go back to step 2.
5. When the message "Congratulations, you completed the assignment!" is displayed, you'll have a chance to compare your solution with ours.

Remember that a function can't change the values of its parameters (in this case, Len1, Len2, and Len3), because function parameters are always of mode in.

This assignment should be a simple exercise in enumeration types and the control constructs.  Our solution easily fits on one screen.  If you find your solution getting long and difficult, you should probably think through the problem again.  See if there's a simple way to test for the four possible answers.