CS 214 Lab 1: Ada


In this part of the exercise, we will write our first Ada program that will solve the same area-of-a-circle problem as before.

Editing Ada Programs

In your 214/labs/01 folder, use a text editor (e.g., vim) to create a file named circle_area.adb containing the program below (again, personalizing its opening documentation). By convention, the name of our Ada source files will end in the .adb suffix.

-- circle_area.adb computes the area of a circle.
--
-- Input: The radius of the circle.
-- Precondition: The radius is a positive number.
-- Output: The area of the circle.
--
-- Begun by: Prof. Adams, CS 214 at Calvin College.
-- Completed by:
-- Date:
----------------------------------------------------

with Ada.Text_IO, Ada.Float_Text_IO;
use  Ada.Text_IO, Ada.Float_Text_IO;

procedure circle_area is

   radius, area : float; 

   -- function circleArea computes a circle's area, given its radius
   -- Parameter: r, a float
   -- Precondition: r >= 0.0
   -- Return: the area of the circle whose radius is r
   ----------------------------------------------------
   function circleArea(r: in float) return float is 
      PI : constant := 3.1415927;
   begin
      return PI * r ** 2;
   end circleArea;

begin                           
   New_Line;
   Put_Line("To compute the area of a circle,");
   Put("enter its radius: ");
   Get(radius);

   area := circleArea(radius);

   New_Line;
   Put("The circle's area is ");
   Put(area);
   New_Line; New_Line; 

   Put("The circle's area is ");
   Put(area, 1, 15, 0);
   New_Line; New_Line; 
end circle_area;
Be sure to customize the program's opening documentation with your name, the date, etc.

Compiling Ada Programs

To translate our Ada programs into machine language, we will be using the (freeware) GNU Ada Translator (Gnat). Gnat provides a program named gnatmake that simplifies the translation process; just enter the following on the command-line:

  gnatmake circle_area
This will perform the 3 translation steps on your program:
   gcc -c circle_area.adb       compilation
   gnatbind -x circle_area.ali  binding calls to defs
   gnatlink circle_area.ali     linking modules together
The first command
   gcc -c circle_area.adb 
creates an intermediate file circle_area.ali, which is then bound and linked with the commands:
  gnatbind -x circle_area.ali
  gnatlink circle_area.ali
The gnatlink command creates the file circle_area, which contains your binary executable program.

In general, the command

gnatmake FileName

will build a binary executable named FileName by compiling the program stored in FileName.adb (as well as any other library units on which it depends), and then binding and linking the resulting object file to any library units it uses. Ada thus requires that the name of a source file match the name of the program stored within it.

Running Ada Programs

Since the Ada compiler creates a binary executable program, it can be run directly from the command-line:

   ./circle_area
Test your program's correctness by using the data values below:
   1

   2

   2.5

   4.99999
Compare your results to those you got previously, and make certain they are equivalent before continuing.

In Ada, comments begin with -- and end at the end of the line. Go through the code line by line and compare what happens when you run the program with the code that produces that behavior. On each line, add a comment explaining what that line is doing.

What is the difference between Ada's Put_Line and Put statements?

How can you control the precision of a real value being displayed by a Put statement?

Wrap Up

To create a printable recording of your session, use script again:

  1. Run script script.ada to record what appears in your terminal window.
  2. Use cat to list your program.
  3. Run gnatmake to show that it compiles correctly (a 'circle_area is up to date' message is sufficient).
  4. Run your program using each of the test values.
  5. Use Ctrl-d or exit to terminate script.

Return to the lab 1 page to complete the other parts of this exercise.


Calvin > CS > 214 > Labs > 01 > Ada


This page maintained by Joel Adams.