CS 214 Lab 4: Ada


Begin by using a text editor to edit the file log_table.adb. Take a moment to study it, to see how it implements the first part of our algorithm.

To complete the program, we must understand the Ada repetition statements. Ada provides one loop statement, which has three different forms:

   Statement      ::=   LoopStatement
   LoopStatement  ::=   LoopClause
                        loop
                           StatementList
                        end loop
   StatementList  ::=   Statement ; MoreStatements
   MoreStatements ::=   Statement ; MoreStatements | Ø
   LoopClause     ::=   Ø | ForPrefix | WhilePrefix 
   ForPrefix      ::=   for identifier in ReverseOption Range
   ReverseOption  ::=   reverse | Ø
   Range          ::=   Expression .. Expression
   WhilePrefix    ::=   while BooleanExpression
The Ada loop statement thus has three forms: The First Form. In the first form, control must be explicitly provided to allow the loop to terminate. This is usually accomplished with an exit-when statement:
   ExitWhenStmt  ::=   exit when (TerminationCondition)
When such a statement executes, it transfers control to the first statement following the loop statement containing it.

The Second Form. In the second form, the loop's identifier serves as a loop-control variable. This variable need not be declared -- it's type is determined by the types of the Expressions that appear in Range. This counting loop has two sub-forms: a count-up form, and a count-down form. The count-up form is the default, the count-down form is achieved by using the keyword reverse.

When control reaches a count-up loop, the following actions occur:

  1. Its identifier is initialized to the first Expression
  2. Its identifier is then compared to the value of the second Expression using the <= relation.
  3. If this relation is true, then the loop's StatementList is executed.
  4. The integer 1 is added to the value of identifier.
  5. Control then returns to (2).

When control reaches a count-down loop, the behavior differs in 4 ways:

  1. Its identifier is initialized to the second Expression.
  2. Its identifier is then compared to the value of the first Expression using the >= relation.
  3. If this relation is true, then the loop's StatementList is executed.
  4. The integer 1 is subtracted from the value of identifier.
  5. Control then returns to (2).
Note that a counting loop's Range is always given in ascending form, regardless of whether it is a count-up loop or a count-down loop.

The Third Form. The third form is a traditional while loop, that exhibits pretest behavior and continues repetition so long as its BooleanExpression evaluates to true.

Back To Our Problem. While our problem is a counting problem, the Ada counting loop is limited to counting by ones, so it will not serve our purpose. Instead, we will need to use one of the other loops, and manage the repetition ourselves. Choose either one, and add the necessary code to your program to elicit the same behavior as log_table.cpp.

To output the values in our table, we can use Ada output statements:

   OutputStmt     ::=   PutStmt | New_Line | ...
   PutStmt        ::=   Put( Expression )
   Expression     ::=   StringExpression | FloatExpression | IntExpression | ...
provided our program has the with and uses directives for the packages to perform I/O with a given type. (E.g., Ada.Text_IO for the String type, Ada.Float_Text_IO for the Float type, Ada.Integer_Text_IO for the Integer type, and so on.)

To compute the logarithm of our count, we can use the Ada log() function. The log() function is actually overloaded with two definitions:

Since we are computing base-10 logarithms, you will need to use the second form.

To use this function (or the other basic mathematical functions), our program must have the with and uses directives for the package Ada.Numerics.Elementary_Functions, so you will need to modify the beginning of the program accordingly.

Using this information, add the statements to log_table.adb to make it display a table of logarithms similar to that of log_table.cpp. Compile and test your code for correctness; then use script to create a script.ada file in which you cat your file, compile it, and show that it works correctly for the input values 1, 10, 0.5.

That concludes the Ada part of this lab.


Calvin > CS > 214 > Labs > 04 > Ada


This page maintained by Joel Adams.