Showing posts with label assignment. Show all posts
Showing posts with label assignment. Show all posts

Wednesday, September 12, 2012

AdaTutor - Outside Assignment 6

Exercise in Tasking

On page 33 of your printed course notes is a listing of TASKING.DUM. This program calls a task entry to display Tick! on the screen every five seconds until it has been displayed nine times.

Every time delay is executed, the message (5-second delay) is displayed so that the delay will be visible on the screen.  The program is entirely in lower case, because your assignment is to modify it.  If you make your modifications in upper case, it will be easy to see what you've changed.

We want you to change the declaration of T from a single task to an array of three tasks.  The tasks are numbered 1, 2, and 3.  Task 1 is to be activated every two five-second intervals.  Task 2 is to be activated every three five- -second intervals, and task 3, every four.  Also, instead of displaying Tick!, each task will identify itself by number, for example, Task number 3 is starting.  Output should look as shown on page 35 of your printed notes.

We recommend that you create an array of three counters.  Each counter counts down from its period (2, 3, or 4) to zero by one count every interval.  When a counter reaches zero, the corresponding task entry is called, and the counter is reset to its period.  All three counters should be initialized to zero, so that all three tasks display their messages immediately upon activation of the program.  You should use the rendezvous mechanism to inform each task of its number (1, 2, or 3).  Your program should use a loop to do an orderly shutdown of all three tasks at the end.

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

  1. Copy the file TASKING.DUM to TASKING.ADA.  Compile, link, and execute the program to make sure it displays Tick! nine times, with a 5-second delay after each "Tick."
  2. Edit TASKING.ADA to become your solution.  Make your changes in upper case.
  3. Compile TASKING.ADA, link, and execute.
  4. Compare your output with page 35 of your printed course notes.  If there are any errors, go back to step 2.
  5. When your output agrees with your printed course notes, you've finished the assignment and will have a chance to compare your solution with ours.
Please stop reading AdaTutor temporarily, and try Outside Assignment 6.  Work at your own pace; there's no deadline. Good luck!

Congratulations on Completing Outside Assignment 6!

If you like, you can compare your solution with ours, which is in TASKING.ANS.  A listing is on page 36 of your printed course notes.  Your solution might be different from ours, but if your output agrees with page 35 of your printed course notes, your solution is correct.

You've learned a great deal of Ada!  Let's go on to discuss some more records and types.

Click to view our solution:

< prev   next >

Monday, September 10, 2012

AdaTutor - Outside Assignment 5

Writing a Simple Line Editor

We're finally ready for the next Outside Assignment!  This assignment will give you a chance to write a program of greater complexity than the previous assignments.  By the time you've completed Outside Assignment 5, you should feel comfortable with Ada.  The full set of requirements for the line editor we want you to write are in your printed course notes, starting on page 20.  We'll discuss them briefly here.  No test driver is supplied, but after you've written the program, we'll give you some tests to perform manually on your line editor.  You've completed the assignment when your editor passes all the tests.

A line editor edits only one line of a text file at a time, and is much simpler than a screen editor that lets you move a cursor wherever you want.  The line editor you'll write, called Ledit, will take almost no effort to learn how to use.  The only commands are LIST and EXIT!

The user begins each line of text with a line number, similar to early versions of Basic.  Line numbers must be integers between 1 and 29999.  Regardless of the order in which lines are entered, Ledit maintains a linked list of lines in order by number, so that it can List the text in order, or write it to a file.

Line numbers need not be consecutive, and they may be preceded by any number of spaces.  For example, if we type

40 -- This is a comment.
   20 begin
10 with Ada.Text_IO; use Ada.Text_IO;
      30 end Add;
and then type LIST, the editor displays
   10 with Ada.Text_IO; use Ada.Text_IO;
   20 begin
   30 end Add;
   40 -- This is a comment.

To insert lines, we merely type lines with intermediate line numbers . In our example, if we now type

15 procedure Hello is
LIST
we'll see
   10 with Ada.Text_IO; use Ada.Text_IO;
   15 procedure Hello is
   20 begin
   30 end Add;
   40 -- This is a comment.

To replace a line, we simply retype the line with the same line number as the line to be replaced, and to delete a line, we type only the line number. If we now type

15 procedure Add is
40
LIST
we'll see
   10 with Ada.Text_IO; use Ada.Text_IO;
   15 procedure Add is
   20 begin
   30 end Add;

Thus the user can insert, replace, and delete lines by line numbers, without learning any commands!  If the user forgets the space after the line number, no harm is done; Ledit takes 20begin the same as 20 begin.  However, the user can indent code by adding extra spaces after the line number.  The following example has two extra spaces after each line number (three spaces total):

24   Put(2 + 2);
26   New_Line;
18   package My_Int_IO is new Integer_IO(Integer); use My_Int_IO;
LIST

   10 with Ada.Text_IO; use Ada.Text_IO;
   15 procedure Add is
   18   package My_Int_IO is new Integer_IO(Integer); use My_Int_IO;
   20 begin
   24   Put(2 + 2);
   26   New_Line;
   30 end Add;

When Listing, Ledit always allows exactly five spaces for the line number, so that the text lines up correctly:

LIST

    2 This is a sample listing,
   20 showing how the text
  200 lines up, even when
 2000 some line numbers are
20000 longer than others.

When we type EXIT, Ledit writes the output file without the line numbers. The text above would all start in column 1 of the output file.

For Ledit to be useful with files larger than a page, it must be possible to list a range of lines:

LIST 20 - 30

This is only a summary of the requirements for Ledit.  Please refer to pages 20-24 of your printed course notes for the actual requirements.  As a point of reference, our solution requires about 180 lines of Ada on four pages.

Here are the steps to follow for Outside Assignment 5.  They can also be found in your printed course notes on page 25:

  1. Carefully read the requirements starting on page 20 of your printed course notes.  Take your time.
  2. Write the Ada code, compile, and link.  Call the main program Ledit.  If you have any questions about what Ledit should do, you can compile and run our solution, which is in L_EDIT.ANS.
  3. Refer to pages 26-28 of your printed course notes for instructions on testing your line editor.  If any tests are failed, go back to step 2.
  4. When all the tests are passed, you've completed the assignment and will have a chance to compare your solution with ours.
Please stop reading AdaTutor temporarily, and try Outside Assignment 5.  It will probably take several days.  Take your time; there's no deadline.  Good luck!

Congratulations on Completing Outside Assignment 5!

If you like, you can compare your solution with ours, which is in L_EDIT.ANS.  A listing starts on page 29 of your printed course notes.  Note that a single procedure handles adding, deleting, and replacing lines.  Replacing is done by first deleting, then adding a line.

Your solution might be very different from ours, but if it passed all the tests, consider it correct.

Early in this course we used the generic package Integer_IO.  Let's now learn how to write our own generic packages, procedures, and functions.

Click to view our solution:

< prev   next >

Monday, August 27, 2012

AdaTutor - Outside Assignment 4

Exercise in Recursion

Your fourth Outside Assignment is to write, using recursion, a function specified by

    function Fib(N : in Positive) return Positive;

Fibonacci was a mathematician in the Middle Ages.  The so-called Fibonacci Series is a series of integers.&mnsp; Each number in the series is the sum of the two previous numbers.  The first two Fibonacci numbers are 1.

  N:    1  2  3  4  5  6   7   8   9  10  11   12   13   14   15   16 ...
Fib(N): 1  1  2  3  5  8  13  21  34  55  89  144  233  377  610  987 ...

Note that if N = 1 or N = 2, then Fib(N) = 1; otherwise, Fib(N) = Fib(N - 1) + Fib(N - 2).  Writing this function in Ada will be an easy and short assignment.  A test driver is provided in FIBTEST.ADA; a listing is on page 15 of your printed course notes.  As before, if all tests are passed, it displays "Congratulations, you completed the assignment!"  If there's an error, it displays the test case, the answer from your function, and the right answer.

A dummy solution is in FIB.DUM. As before, you shouldn't change the lines highlighted here:

    -- Dummy solution to Outside Assignment 4
separate (Fibtest) function Fib(N : in Positive) return Positive is begin
return 4;
end Fib;

The steps to follow for Outside Assignment 4 are similar to those of the last two Outside Assignments.  They're in your printed course notes on page 16:

  1. Copy FIB.DUM to FIB.ADA to make a copy of the dummy solution.  Also, compile the test driver FIBTEST.ADA. You need do this step only once.
  2. Edit FIB.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 FIB.ADA.  If the compiler finds errors, go back to step 2.
  4. Link with the name of the main program Fibtest.  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.

Please exit AdaTutor temporarily, and try Outside Assignment 4.  Work at your own pace; there's no deadline. Good luck!

Congratulations on Completing Outside Assignment 4!

Our solution to Outside Assignment 4 (in FIB.ANS):

< prev   next >

Tuesday, August 21, 2012

AdaTutor - Outside Assignment 3

Exercise in Records

Your third Outside Assignment is to write a function specified by
   type Month_Type is (Jan, Feb, Mar, Apr, May, Jun,
                     Jul, Aug, Sep, Oct, Nov, Dec);
   subtype Day_Subtype is Integer range 1 .. 31;
   type Date is record
      Day   : Day_Subtype;
      Month : Month_Type;
      Year  : Integer;
   end record;
   function Tomorrow(Today : in Date) return Date;

Given any date, Tomorrow should return the following date.  Your function will be tested only with legal dates.  As with Outside Assignment 2, a test driver is already written; it's in NEXTDATE.ADA.  A listing is on page 13 of your printed course notes, but you needn't understand the test driver.  If your function fails any test, you'll see the test case, the answer from your function, and the right answer.  Otherwise, you'll see "Congratulations, you completed the assignment!"

The definitions of Month_Type, Day_Subtype, and Date are in the test driver; you shouldn't define them inside your function.  A dummy solution is in TOMORROW.DUM; it looks like this:

   -- Dummy solution to Outside Assignment 3
separate (Nextdate) function Tomorrow(Today : in Date) return Date is begin
return Today;
end Tomorrow;

Again, you'll probably want to remove or change the comment line, and you shouldn't change the lines highlighted above.  You may, but don't have to, include Answer : Date; in the declarative region and make return Answer; the last statement before end Tomorrow;.

Normally, years divisible by 4 are leap years.  But if a year is divisible by 100, it must also be divisible by 400 to be a leap year.  Thus, 2000 is a leap year, but 1900 and 2100 are not.  Note that Today.Year is divisible by 4 if and only if Today.Year mod 4 = 0.  You may assume that Today.Year will always be between 1583 and 3999, because outside this range the calendar gets more complicated.

The steps to follow for Outside Assignment 3 are very similar to those of Outside Assignment 2.  They're in your printed course notes on page 14:

  1. Copy TOMORROW.DUM to TOMORROW.ADA to make a copy of the dummy solution.  Also, compile the test driver NEXTDATE.ADA.  You need do this step only once.
  2. Edit TOMORROW.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 TOMORROW.ADA.  If the compiler finds errors, go back to step 2.
  4. Link with the name of the main program Nextdate.  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.

There are many ways to solve this problem.  In our solution we declared an array of Day_Subtype with subscripts of type Month_Type.  Some students use a case construct on Today.Month; some use an if block with elsifs.

This assignment should be a simple exercise in records.  Our solution fits on one screen.  If your solution starts to get long and difficult, you should think the problem through again.  Don't try to save the computer a few microseconds; computers are supposed to save people time.  Instead, minimize the complexity of the program to save yourself programming effort!

Remember that an entire record can be assigned in one statement.  Also, try to calculate the number of days in the month and then test for end of month and end of year in only one place.  This is better than having several blocks of code testing for end of month and end of year in three or four different places in your program.  One last hint: remember that Month_Type'Succ(Today.Month) will raise a Constraint_Error if Today.Month is Dec.

Please exit AdaTutor temporarily, and try Outside Assignment 3.  Work at your own pace; there's no deadline. Good luck!

Congratulations on Completing Outside Assignment 3!

Our solution to Outside Assignment 3 (in TOMORROW.ANS):

< prev   next >

Thursday, August 16, 2012

AdaTutor - Outside Assignment 2

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
      Answer : Triangle;
   begin
      if ... end if;
      return Answer;
   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.

Please stop reading AdaTutor temporarily, and try Outside Assignment 2.  Work at your own pace; there's no deadline.&nsp; Good luck!

Congratulations on Completing Outside Assignment 2!

Our solution is in TRITYPE.ANS. It looks like this:

< prev   next >

Saturday, August 11, 2012

AdaTutor - Outside Assignment 1

Preparing to Run Ada on Your Computer

Since these screens disappear, you'll probably want to refer to your printed course notes when doing the Outside Assignments.  The first assignment appears on page 8.  In this assignment, we'll compile, link, and run our first two programs, Hello and Add.

First compile HELLO.ADA, then link, and then execute the program.  The exact steps will depend on the Ada compiler you're using.  You'll probably have to refer to the documentation that came with your compiler.  Note that some implementations of Ada use the term "binding" instead of "linking."  When you run the program, it should display Hello! on your screen.

Then compile ADD.ADA, link, and run the program.

When we compile, we specify the name of the source file being compiled.  Some, but not all, implementations of Ada assume the extension to be .ADA if it's not specified.  When we link, we specify the name of the main program.  In these two programs, the program name agrees with the name of the source file, without the extension.  The linking step produces a file which we can execute.

With some Ada compilers, we must type a command to create an Ada library before compiling.  For example, with one compiler we tried, we had to type the command NEWLIB before we could compile programs.  However, after typing that command only once, we could compile any number of Ada programs.  Again, you'll have to consult your compiler documentation to see if such a command is necessary.

If you haven't done so already, please stop reading this AdaTutor temporarily, read page 8 of your printed course notes, and try the first Outside Assignment.

When you finish Outside Assignment 1, we'll go on to discuss the Ada compilation environment.

We know this first assignment wasn't very interesting, because the programs HELLO.ADA and ADD.ADA were supplied for you.  But we had to be sure we can compile, link, and execute Ada programs, so we can do the remaining assignments.

The rest of the Outside Assignments should be more challenging!

Let's go on to discuss the Ada compilation environment.

The Ada Compilation Environment

The programs Hello and Add were complete in themselves, and we were able to link them as main programs.  Of course, most Ada programs are more complex than that, involving calls to subprograms (procedures and functions).

When we compiled HELLO.ADA and ADD.ADA, the Ada compiler didn't "know" that we were going to link them as main programs.  After compiling them, we could have compiled another program that calls Hello, Add, or both, and link that other program as the main program.  The Ada system doesn't know which compilation unit is the main program until we link.

For example, we could create a file TEST.ADA containing the following:

with Hello, Add;
procedure Test is
begin
   Hello;
   Add;
end Test;

This new program does nothing but call procedures Hello and Add.

After compiling HELLO.ADA and ADD.ADA in either order, we could compile TEST.ADA, which withs Hello and Add.  Then we could link with the name of the main program, Test, to make an executable file.  Running Test would display "Hello!" on one line and "4" (with some leading spaces) on the next line.

Ada comes with several packages, such as Ada.Text_IO, that are already compiled.  That's why our programs can say with Ada.Text_IO; and call its procedures and functions.  We can also with previously compiled procedures and functions that are not in packages, as we did with procedures Hello and Add in the program Test above.  However, use applies only to packages.

When we compile a call to a subprogram, Ada always checks that the number and types of parameters in the call match those of the subprogram.  In the example above, Hello and Add had no parameters, and Ada checked that the calls to them in Test also had no parameters.

With some other languages, this checking isn't done.  For example, a Fortran main program might say CALL SUB(I, J, K), while the subroutine might say SUBROUTINE SUB(I, J).  Fortran will compile both of these correctly, without giving any error messages or warnings.  When the program is run, however, the results are unpredictable.  The program might simply give wrong answers with no warning.  An Ada compiler always catches that mistake!

In our example Test, we wrote and compiled the subprograms before the calling program.  Ada also lets us write and compile the calling program first, because we can compile a subprogram specification separately from its body.  (We'll learn more about that later.)  The specification contains the name of the subprogram, and the names, number, and types of any parameters.  The specification also tells which parameters are inputs, which are outputs, and which are both inputs and outputs.  That's all the information the compiler needs to compile calls to the subprogram; the body can be supplied later.  When the subprogram body is compiled, the compiler will make sure it's consistent with the specification.  For example, we could compile just these two lines:

    procedure Hello;

    procedure Add;
and then compile TEST.ADA.  We could later compile HELLO.ADA and ADD.ADA as the procedure bodies, and then link with the name of the main program, Test.

An Ada 95 compiler may defer generating code until the all the required bodies are present; GNAT is an example of such a compiler.  Most compilers, however, generate code when each unit is compiled.  Naturally, in any system all bodies must be present before we can link.

Question

True or False?  A procedure specification must be written before calls to the procedure can be compiled.

< prev   next >