The most important item on all homework is YOUR NAME! No name, no credit. Staple or clip pages together. To get an A on a homework assignment that lists Extra Credit, you must do the extra credit.
Turn in or EMail only plain text! No word processor formats. You may use a word processor or other software tools and print the results and turn in paper. Put "Ada" and "HW number" in subject line. Attachments should be source files or result files with the extension .txt or .out
If you DO NOT have Ada95 compiler and adagide on your PC, ftp cs.nyu.edu/pub/gnat look for latest version, 3.12p or later, winnt for Microsoft systems. AdaGide may be a separate file. The README and/or INSTALL file gives installation instructions. You may want to create a directory "adaclass" and use this directory for your homework. (Copy or download the starter homework files from the links in the assignments or follow the 'download' link to get them.) Now, test to be sure Ada95 is installed correctly. Get the file sample1.adb into your directory. Click on the Ada95 icon, click on file, open and move to your directory adaclass, open sample1.adb (It just says sample1). Click run then build. It should show a message indicating completed. Click run then execute. A new window should pop up. Click in the new window, then type 25 followed by an enter. You should see output something like 5 = mod( 25, 10) Type control Z, ^Z, and see the exception message. Kill the window. Ignore the exception message, end-of-file was not handled. Ada95 is installed and working.
HOMEWORK PROBLEM 1 INTRODUCTION TO Ada'95 PURPOSE: To gain experience with IF-THEN-ELSE Ada structures Introduction to Text_IO Get, Put, Put_Line Single procedure main program structure Handling Text_IO exceptions Learning to compile and run Ada programs END RESULT: A complete Ada program that is a single procedure that compiles and executes with no errors PROBLEM: Use the skeleton structure from the attached change.adb or change2.adb and add your code to compute change for an Amount between 1 and 99 cents. Print number of quarters, dimes, nickels, and pennies with reasonable structure. Zero counts for coins are not to be printed out. Use an exception handler to catch bad input. TURN IN: Printout of compilation ( with no errors!) Printout of execution for test data (in order) 99 94 25 10 5 1 -5 100 ABC 1or use change.dat . OBSERVE: The input to Ada programs uses the operating system terminal handler. Thus nothing might happen until a is typed ( this allows for input line editing ). METHOD: In learning a new programming language it is most efficient to do it interactively. Before trying the first homework it is recommended that you type in the sample program that follows. Try the emacs editor or other editor of your choice. Compile interactively if your system provides the capability. Use the debugger only if desperate, usually not needed with Ada. Then set up .BAT or make for the "production" runs. This will get the mechanics of the procedures established for the rest of the course. EXTRA CREDIT: Do not change 1..99 to 0..99 or Integer or natural for any variable. In other words never try to store a zero. You may eliminate the Variables Quarters, Dimes, Nickels and Pennies if you do not need them. You can add a Constraint_Error exception handler if you wish. READING: BARNES 5.2, 7.1, 14.1 ISO 8652:1995 2.2, 5.3, 11.2 COMMANDS: DOS, Windows 95, 98, NT or Unix gcc -c change.adb gnatbl change.ali change < change.dat change < change.dat > change.txt gnatchop change.ada writes file change.adb FILES: change.adb change2.adb no "use" change.dat (If you had an Ada'83 .ada file, first do gnatchop xxx.ada, then use gnat on the resulting .adb and .ads files. Don't just rename file)
HOMEWORK PROBLEM 2 INTRODUCTION TO Ada'95 PURPOSE: To gain experience with array structures More on Text_IO Get, Put, on various types Internal procedures in a main program structure Handling characters and enumeration types END RESULT: A complete Ada program that is a single procedure with two internal procedures that compiles and executes with no errors PROBLEM: Write an Ada program to simulate a simple bank. You may use the skeleton program bank.adb that is attached or do your own from scratch. Each input line contains a simple bank activity: JOHN DOE OPEN 10.00 opens an account JIM JONES OPEN 37.50 opens an account JOHN DOE DEPOSIT 5.00 deposits $5 JIM JONES WITHDRAW 7.50 withdraws $7.50 JOHN DOE CLOSE 0.00 closes his account At the end of the run, print out the status of the accounts. TURN IN: Printout of source code (No compilation errors) Printout of execution for test data (in order) of the data listed above. OBSERVE: Character strings require exact match. This means quoted character constants need trailing blanks. Input data using Get on character strings also require trailing blanks (exact count!). Operating systems provide line editing, thus a carriage return is needed to commit the input. Input can be for one or more GET procedure calls. The physical location of an internal procedure or function is critical. They must be after all declarations and before the 'begin'. EXTRA CREDIT Give error message if opening an open account if closing a non open account if overdrawing, negative amounts, etc. Physically delete closed accounts to be ready for future homework assignments READING: Barnes 8.1 8.2 8.3 8.4 ISO 8652:1995 3.6 The following commands compile, link and run Homework 2 on Unix, W95, NT gcc -c bank.adb gnatbl bank.ali bank < bank.dat > bank.txt print bank.adb print bank.txt (If you had an Ada'83 .ada file, first do gnatchop xxx.ada, then use gnat on the resulting .adb and .ads files. Don't just rename file) The input data is (bank.dat): JOHN DOE OPEN 10.00 opens an account JIM JONES OPEN 37.50 opens an account JOHN DOE DEPOSIT 5.00 deposites $5 JIM JONES WITHDRAW 7.50 withdraws $7.50 JOHN DOE CLOSE 0.00 closes his account, takes out all his money BAD TRANS FOUL 1.00 bad transaction NOT OPEN CLOSE 1.00 bad, not open JIM JONES OPEN 15.00 no Jim, its open JIM JONES DEPOSIT -5.00 no Jim, try again, must be positive JIM JONES WITHDRAW -5.00 no Jim, positive ! JOHN DOE DEPOSITE 5.00 not open, forget it JIM JONES WITHDRAW 99.99 too much, reject Results of program as given to you are (bank.out): ENTER Transaction. Name,Transaction,Amount Name= JOHN DOE Transaction OPEN Amount = 1.00000E+01 Open ENTER Transaction. Name,Transaction,Amount Name= JIM JONES Transaction OPEN Amount = 3.75000E+01 Open ENTER Transaction. Name,Transaction,Amount Name= JOHN DOE Transaction DEPOSIT Amount = 5.00000E+00 Deposit ENTER Transaction. Name,Transaction,Amount Name= JIM JONES Transaction WITHDRAW Amount = 7.50000E+00 Withdraw ENTER Transaction. Name,Transaction,Amount Name= JOHN DOE Transaction CLOSE Amount = 0.00000E+00 Close ENTER Transaction. Name,Transaction,Amount Name= BAD TRANS BAD INPUT DATA ENTER Transaction. Name,Transaction,Amount Name= NOT OPEN Transaction CLOSE Amount = 1.00000E+00 Close ENTER Transaction. Name,Transaction,Amount Name= JIM JONES Transaction OPEN Amount = 1.50000E+01 Open Account Already Open ENTER Transaction. Name,Transaction,Amount Name= JIM JONES Transaction DEPOSIT Amount =-5.00000E+00 Deposit ENTER Transaction. Name,Transaction,Amount Name= JIM JONES Transaction WITHDRAW Amount =-5.00000E+00 Withdraw ENTER Transaction. Name,Transaction,Amount Name= JOHN DOE BAD INPUT DATA ENTER Transaction. Name,Transaction,Amount Name= JIM JONES Transaction WITHDRAW Amount = 9.99900E+01 Withdraw ENTER Transaction. Name,Transaction,Amount end of input data Bank Is Closed. FILES: bank.adb banknu.adb no "use" bank.dat bank.out
HOMEWORK PROBLEM 3 INTRODUCTION TO Ada'95 PURPOSE: To learn to build your own program library. To write an Ada package. Get an n log n sort for your future applications. Learn more on simple user created types. END RESULT: A complete Ada program that is a main procedure that WITH's a package you write. PROBLEM: Modify the bank account program from Homework 2 to With a package that contains a procedure to sort the account names / balances data. At the end of the run, print out the status of the accounts. First unsorted, then sorted by calling the sort procedure in the package. Add or rearrange data so that unsorted data is not in sorted order by account name. Sample enhanced data is in sort.dat TURN IN: Printout of compilation of main procedure and package ( with no errors!) Printout of execution for test data. OBSERVE: A package consists of two parts. The specification part and the body part. The rules of the Ada language allow the body to be changed without impacting any thing else. When the specification is changed ( i.e. recompiled ) then the body must be recompiled and all compilation units that WITH the specification must be recompiled. In general, a package specification can exists without a package body. ( a body must have a spec ) A package specification may contain any or all of the following: Type specifications, object specifications, named numbers, procedure, function and task specifications. NOTE: In order for a type to be known to both a package and a procedure that "with's" the package, the type must be defined in the package specification. This means some of the type declarations in HW2 must be moved from the HW2 procedure into the sort package and deleted from the HW2 procedure. For two objects to be the same type, you must be able to trace both back to the same physical type definition. ( Not a type definition that looks the same !) The package Standard that is automatically "withed" contains the physical type definitions for Integer, Float, String etc. READING: Barnes 9.1, 9.3, 12.1, 12.5, 12.7 ISO 8652:1995 6.1, 6.3, 7.1, 7.2, 8.4 Sample data for homework problem 3, SORT.DAT JOHN DOE OPEN 10.00 opens an account JIM JONES OPEN 37.50 opens an account JOHN DOE DEPOSIT 5.00 deposits $5 JIM JONES WITHDRAW 7.50 withdraws $7.50 JOHN DOE CLOSE 0.00 closes his account MARY SMITH OPEN 20.00 open another account AARON ADD OPEN 99.00 should sort first ZEB ZZEPT OPEN 99.00 should sort last BAD TRANS FOUL 1.00 bad transaction NOT OPEN CLOSE 1.00 bad, not open MARY SMITH OPEN 15.00 no Mary, its open MARY SMITH DEPOSIT -5.00 no Mary, try again MARY SMITH WITHDRAW -5.00 no Mary, positive ! JOHN DOE DEPOSIT 5.00 not open JIM JONES WITHDRAW 99.99 too much, reject JOHN DOE OPEN 99.00 back again, OK EXTRA CREDIT: The passing of SIZE on line 13 of SORT_TEST is not typical Ada style. This line would more commonly read: NNsort ( Data ) ; or possibly NNsort ( Data(2..7) ) ; Similarly the passing of OPEN_ACCOUNTS on line 86 of TEST_SHELLI_SORT can be eliminated by the procedure call: Shelli ( Account_Names(1..Open_Accounts),Balances(1..Open_Accounts)); In this example the call increased in length although the parameters were reduced from three to two. The corresponding procedure definitions would change on line 6 of SORT to: procedure NNsort ( Arr1 : in out Int_Array ) is and on line 47 of Test_Shelli_Sort to: procedure Shelli ( Arr1 : in out A_Names ; Arr2 : in out A_Balances ) is The Ada language feature that makes this style preferred is the attributes that are defined for arrays. Arr1'First is the first, lowest, subscript of ARR1 Arr1'Last is the last, largest, subscript of ARR1 Arr1'Range is a short hand for the range ARR1'FIRST..ARR1'LAST Arr1'Length is the number of values in the ARR1 For extra credit, modify SHELLI to have two parameters and use attributes as appropriate in the procedure body. Array attributes will be covered again in a later lecture. FILES: your homework 2 sort.dat sort.ada sort_test.ada shellitst.out demo of sort shellitst.ada Optional shellitst.for shellitst.f90 shellitst.c shellitst.cc shellitst.bas
HOMEWORK PROBLEM 4 INTRODUCTION TO Ada 95 PURPOSE: To learn about matrices, overloaded operators, defining and using exceptions. To specify types and exceptions in a utility library package END RESULT: A complete Ada program that is a main procedure that WITH's a package you modify. PROBLEM: Modify the attached package Real_Matrix_Arithmetic , a skeleton for a complete real matrix arithmetic package. Use the matrix multiply "*" as a guide to write matrix add "+". Keep the matrix multiply and use both in the main procedure. The only requirement for a matrix add to be legal is that A'Length(1) = B'Length(1) and A'Length(2) = B'Length(2). TURN IN: Printout of main procedure and package. Printout of execution for test data. ( modify to suit your needs. Matrices as small as 3 by 2 are acceptable , not square matrices) OBSERVE: The type Real_Matrix is defined in the package and is visible in the main procedure because of the "with" and "use" on Real_Matrix_Arithmetic. The objects MAT_1 etc. are declared and assigned storage in the main procedure. The object C is dynamically assigned storage with a size that depends on the formal parameter A when "*" is called. The user defined exception Matrix_Error is defined and may be raised in the package. This exception may then be handled as the user desires in the main procedure. This avoids the need for math routines to print out errors and allows the calling procedure to take corrective action for some types of errors. Be sure to handle first and second subscript ranges having different first subscripts. Only the length of the side of the matrix is important. EXTRA Add tests to make sure all cases work. READING: BARNES 9.1-9.6 A1.2 ISO 8652:1995 6.1 6.2 6.3 6.4 Annex K LECTURE: will also cover attributes in Annex K (follow AdaGide help to Annex K) The following are some convenient packages to have compiled. Int_IO.ads with Ada.Text_IO ; package Int_IO is new Ada.Text_IO.Integer_IO ( Integer ) ; NOTE: compiling the above package may allow faster compilation of later units. Replace "package Int_IO is new Integer_IO ( Integer ) ; use Int_IO ;" with "with Int_IO ; use Int_IO ;" before the package or procedure Define_Real.ads -- This package provides the definition of the type Real and Long_Real. -- For computers with only one floating point hardware both subtypes -- become the same. package Define_Real is subtype Real is Float ; -- may be Short_Float subtype Long_Real is Long_Float ; -- may be Float end Define_Real ; Real_IO.ads with Define_Real ; with Ada.Text_IO ; package Real_IO is new Ada.Text_IO.Float_IO ( Define_Real.Real ) ; Note: As projects get larger, there is more importance on not changing package specifications. This has two ramifications. First put the package specification in one file and the package body in another. (.ads and .adb for gnat) Second, only put "with" statements on the specification that are needed by the specification. The body typically needs more "with" statements. This speeds up compilations and reduces dependencies. More stray information : The VAX VMS naming convention for files containing package specifications is to add an underscore to the package name in order to get the file name. The package body file name is the same as the package name. The gnat naming convention is to use the package name with extensions .ads for specifications and .adb for bodies. Verdix now part of Rational uses xxx.a and xxx_b.a Remember to use gnatchop on starter files named *.ada FILES: matdemo.ada has it all int_io.ads define_real.ads real_io.ads real_matrix_arithmetic.ads real_matrix_arithmetic.adb matrix_demo.adb Optional seq_io_demo.adb direct_io_demo.ada direct_io_demo_nouse.ada
HOMEWORK PROBLEM 5 INTRODUCTION TO Ada 95 PURPOSE: To gain experience in Ada strong typing. This problem will use private types, derived types, subtypes, type conversion, real types and qualified expressions. END RESULT: A set of packages that allow the attached procedure to compile and execute correctly. PROBLEM: You are to provide a set of packages that physics students are to use in programming their homework. The problems are limited to distance, time, velocity and acceleration. The student must not be allowed to write code that violates dimensional analysis of physics. Still the student should be able to use square root and trig functions as well as non dimensional quantities. The only units that need to be considered are meter, second, meter per second and meter per second squared. Information supplied in the handout includes: dimensional analysis, definition of MKS and English systems of units, physical constants and equations of physics. It adds too much complexity to be able to handle all equations in any system of units. Conversion to base units and back is the most practical way to solve the general problem. In order to save typing, the full set of sample packages used in the lecture can be downloaded. Get the file physics.ada and use gnatchop on it. These do not have to be used. You can solve the problem by any method you choose. You will be making small changes in a number of files. TURN IN: Printout of source program and execution output. OBSERVE: The distinction between type conversion and qualified expression. The uses of derived types and subtypes. At least some of the packages would be generic in their final versions. Note that many Ada papers and text books show a lack of understanding for dimensions and units as related to Ada's strong typing capability. METHOD: Use the starter set of files or develop your own code. READING: BARNES 6.3, 6.4, 11.2, 11.3 11.4, IS0 8652:1995 3.2, 3.4, 3.5.6, 3.5.7, 4.6, 4.7 Units definitions FILES: physics.ada has it all hw5.adb
HOMEWORK PROBLEM 6 INTRODUCTION TO Ada 95 PURPOSE: To learn about generic packages Writing generic packages allows future users of the package to choose their own types. This is a great benefit to users applying strong typing. END RESULT: A complete Ada program that is a main procedure that WITH's and instantiates a generic package you write. The generic package must sort many types of arrays ( but may not be very robust and uses the slowest known method of sorting. PROBLEM: Using the attached example GENSUMPKG that contains a dumb generic package to add up the elements of an array, and using the attached example of a non generic sort , NON_GENERIC_SORT_PACKAGE , modify NON_GENERIC_SORT_PACKAGE.ADA to be a generic package and modify the main procedure NON_GENERIC_SORT_DEMO to make use of the generic version of NON_GENERIC_SORT_PACKAGE. TURN IN: Printout of source code for main procedure and package Printout of execution for built in test data. OBSERVE: There are relatively few changes. But, be careful the changes are very subtle. READING : ISO 8652:1995 all of chapter 12. Don't worry if you do not understand everything. There may be a dozen people in the world that understand the complete chapter. Reread 12.1, 12.1.2 and 12.2 after you complete the homework. BARNES : read all of chapter 17 on generics. Generic Summary EXTRA : For extra credit generalize the generic package to work when the range of INT_ARRAY can be anything allowed, including an enumeration type. The color types make a good test case for extra credit. FILES: non_generic_sort_package.ada copy to generic_sort_package.ads and .adb and modify non_generic_sort_demo.ada copy to generic_sort_demo.adb and modify extra_sort.ada extra_sort.adb ADDITIONAL SAMPLES and INSTRUCTIONS -- This example has three compilation units : -- The generic package specification for GENSUMPKG -- The package body for GENSUMPKG -- The test procedure that instantiates GENSUMPKG with -- generic actual parameters INTEGER and MY_ARRAY generic -- gensumpkg.ads type ELEMENT is private ; type KEY_1 is array ( INTEGER range <> ) of ELEMENT ; with function "+" ( U , V : ELEMENT ) return ELEMENT is <> ; package GENSUMPKG is procedure SUM ( A : in out KEY_1 ) ; end GENSUMPKG ; -- end of first compilation unit package body GENSUMPKG is -- gensumpkg.adb procedure SUM ( A : in out KEY_1 ) is TOTAL : ELEMENT := A ( A'FIRST ) ; -- no numbers allowed begin for I in A'FIRST + 1 .. A'LAST loop TOTAL := TOTAL + A ( I ) ; end loop ; A ( A'FIRST ) := TOTAL ; end SUM ; end GENSUMPKG ; -- end of second compilation unit with TEXT_IO ; use TEXT_IO ; -- testgens.adb with GENSUMPKG ; procedure TEST_GENSUMPKG is type MY_ARRAY is array ( INTEGER range <> ) of INTEGER ; STUFF : MY_ARRAY ( 1 .. 3 ) := ( 1 , 2 , 3 ) ; package INT_IO is new INTEGER_IO ( INTEGER ) ; use INT_IO ; -- This is the generic instantiation of the package above package MY_SUM is new GENSUMPKG ( ELEMENT => INTEGER , KEY_1 => MY_ARRAY ); --or package MY_SUM is new GENSUMPKG ( INTEGER , MY_ARRAY ) ; -- positional use MY_SUM ; -- optional, could also write MY_SUM.SUM below begin PUT_LINE ( " GENERIC SUM PACKAGE " ) ; SUM ( STUFF ) ; -- call procedure in instantiated package PUT ( STUFF( 1 )) ; PUT_LINE ( " = SUM " ) ; end TEST_GENSUMPKG ; -- GENERIC SUM PACKAGE \___results of running. -- 6 = SUM / For HW6, use the following as a starting point: This is the sample non generic sort package and corresponding test procedure that is to be converted to a generic package and corresponding test procedure that instantiates the generic package and run the built in test data. -- NON_GENERIC_SORT_PACKAGE.ADA package NON_GENERIC_SORT_PACKAGE is type INT_ARRAY is array ( INTEGER range <> ) of INTEGER ; -- the type statement above will move into the generic formal part procedure NNSORT ( ARR1 : in out INT_ARRAY ) ; end NON_GENERIC_SORT_PACKAGE ; package body NON_GENERIC_SORT_PACKAGE is procedure NNSORT ( ARR1 : in out INT_ARRAY ) is TEMP : INTEGER ; begin for I in ARR1'FIRST .. ARR1'LAST - 1 loop for J in I + 1 .. ARR1'LAST loop -- This is the classic compare and -- interchange at the heart of all sorts if ARR1 ( I ) > ARR1 ( J ) then TEMP := ARR1 ( I ) ; ARR1 ( I ) := ARR1 ( J ) ; ARR1 ( J ) := TEMP ; end if ; -- end of compare and interchange end loop ; end loop ; end NNSORT ; end NON_GENERIC_SORT_PACKAGE ; -- NON_GENERIC_SORT_DEMO.ADA with NON_GENERIC_SORT_PACKAGE ; use NON_GENERIC_SORT_PACKAGE ; --with GENERIC_SORT_PACKAGE ; -- NO use statement on generic packages ! with TEXT_IO ; use TEXT_IO ; with INT_IO ; use INT_IO ; procedure NON_GENERIC_SORT_DEMO is DATA : INT_ARRAY ( - 3 .. 4 ) := ( 6 , 7 , 3 , 4 , 8 , 1 , 2 , 5 ) ; -- *** INSTANTIATE YOUR GENERIC_SORT_PACKAGE HERE begin NNSORT ( DATA ) ; PUT_LINE ( " SORTED DATA " ) ; for I in DATA'RANGE loop PUT ( DATA( I )) ; NEW_LINE ; end loop ; end NON_GENERIC_SORT_DEMO ; Use gnatchop if compiling with gnat. Compile and execute then run NON_GENERIC_SORT_DEMO to get output SORTED DATA 1 2 3 4 5 6 7 8 The extra credit, a main test procedure should be something like this : This is very difficult. -- EXTRA_SORT.ADA with GENERIC_SORT_PACKAGE ; -- the package you write with TEXT_IO ; package EXTRA_SORT is type ALL_COLOR is ( BLACK, BROWN, RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE, GREY, WHITE ) ; type MY_COLOR is ( RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET ) ; type COLOR_ARRAY is array ( ALL_COLOR range <> ) of MY_COLOR ; MY_ARRAY : COLOR_ARRAY ( ORANGE..BLUE ) := ( VIOLET, RED, YELLOW, BLUE ) ; package MY_SORT is new GENERIC_SORT_PACKAGE ( MY_COLOR , ALL_COLOR, COLOR_ARRAY ) ; package ENUM_IO is new TEXT_IO.ENUMERATION_IO ( MY_COLOR ) ; begin MY_SORT.NNSORT ( MY_ARRAY ) ; -- result of instantiation for I in MY_ARRAY'RANGE loop ENUM_IO.PUT ( MY_ARRAY ( I ) ) ; end loop ; TEXT_IO.NEW_LINE ; end EXTRA_SORT ;
HOMEWORK PROBLEM 7 INTRODUCTION TO Ada 95 PURPOSE: To learn about Ada tasking and how to use it. This is a two part homework. You will end up with a plane chasing a target and firing a missile. END RESULT: A complete Ada program that is a main procedure that WITH's a package you write containing the tasks. This is the first of a sequence of two assignments that will result in one program. (Nothing to turn in for this part.) PROBLEM: Plane, target and missile problem: HW7 is to get the target flying on your screen. HW8 is to get a plane to fire a missile at the target. Tasking will be used for the target, plane and missile and other tasks. A rendezvous task will be used to access Shared data. ( Report and Read_Out ) A special Plot task is needed that uses only one PUT to output the cursor position and character. You mat use X Windows or Microsoft Windows or VT100. The target is to be started as a task from the main procedure. The main procedure may have to be kept alive until the task terminates. Experiment is HW7. OBSERVE: All the tasks may be placed in a single package. The main program may be used to clean house at the end of execution or the Target task may clean house (abort). READING: BARNES 18. ISO 8652:1995 9. Tasking summary NOTES: Different compilers will have different algorithms for context switching while tasking. The context switch may or may not occur when any form of Put is executed. I/O takes a relatively long time, thus if one task has output then all parallel tasks that must be kept running must also have output ( or must be locked from execution some other way ). The main procedure is the sponsor of the Shared task and thus must be kept running until the Target, Plane, and Missile tasks are complete. The global time T is used to synchronize all tasks and main program termination. Any code can set T to a large number to stop the process. This works reliably because the only other action to change T is to increment it. It would seem reasonable if GET would suspend waiting for input and let parallel task run. It usually doesn't! Most compiler use run till blocked for tasks rather than time slicing. On a uniprocessor, only one task runs at a time. The entries Shared.Report and Shared.Read_Out are to ensure that the four values X,Y,VX,VY are a consistent set independent of the context switching algorithm. This is the standard Ada construct for Shared data in Ada 83, protected types can be used in Ada 95. EXTRA: Use Ada 95 protected in place of select in Shared task FILES: hw78.ada has it all
HOMEWORK PROBLEM 8 INTRODUCTION TO Ada 95 PURPOSE: To learn more about Ada 95 tasking and how to use it. This is the second part. You will end up with a plane chasing a target and firing a missile. END RESULT: A complete Ada program that is a main procedure that WITH's a package you write containing the tasks. This is the first of a sequence of two assignments that will result in one program. (Nothing to turn in for this part.) PROBLEM: Plane, target and missile problem: HW7 is to get the target flying on your screen. HW8 is to get a plane to fire a missile at the target. Tasking will be used for the target, plane and missile and other tasks. A rendezvous task will be used to access Shared data. ( Report and Read_Out ) A special Plot task is needed that uses only one PUT to output the cursor position and character. You mat use X Windows or Microsoft Windows or VT100. The target and plane are to be started from the main procedure. The missile is to be started by the plane. Implement tracking in plane and missile to shoot down the target. Possible tracking algorithms are in track.adb, these get put into HW78.ADA gnatchopped files. OBSERVE: All the tasks may be placed in a single package. The main program may be used to clean house at the end of execution or the Target task may clean house (abort). READING: BARNES 18. Finish reading, or: ISO 8652:1995 9. Finish reading, then Annex D. NOTES: Different compilers will have different algorithms for context switching while tasking. The context switch may or may not occur when any form of Put is executed. I/O takes a relatively long time, thus if one task has output then all parallel tasks that must be kept running must also have output ( or must be locked from execution some other way ). The main procedure is the sponsor of the Shared task and thus must be kept running until the Target, Plane, and Missile tasks are complete. The global time T is used to synchronize all tasks and main program termination. Any code can set T to a large number to stop the process. This works reliably because the only other action to change T is to increment it. It would seem reasonable if GET would suspend waiting for input and let parallel task run. It usually doesn't! Most compiler use run till blocked for tasks rather than time slicing. On a uniprocessor, only one task runs at a time. The entries Shared.Report and Shared.Read_Out are to ensure that the four values X,Y,VX,VY are a consistent set independent of the context switching algorithm. This is the standard Ada construct for Shared data in Ada 83, protected types can be used in Ada 95. EXTRA: Use Ada 95 protected in place of select in Shared task FILES: track.ada Optional for timing tests t000008.ada t000009.ada t0000089.bat a000001.ada a000011.ada a000021.ada a000022.ada a000031.ada a000032.ada a000041.ada a000042.ada This is a comparison of Ada 83 "standard tasks" = T000008 vs Ada 95 "protected" = T000009 on various machines. Note vast differences in ratio with the only difference in the code being "task" vs "protected" definition of the buffer task, shown below. Bullet - Alpha 4000 OpenVMS 7.1 0.020000002 = clock resolution used for iteration stability Test Name: T000008 Class Name: Tasking CPU Time: 46.78 MICROSECONDS plus or minus 2.339 Wall/CPU: 1.00 ratio. Iteration Count: 102400 Test Description: Measure the average time to pass an integer from a producer task through a buffer task to a consumer task Test Name: T000009 Class Name: Tasking CPU Time: 22.51 MICROSECONDS plus or minus 1.125 Wall/CPU: 1.00 ratio. Iteration Count: 204800 Test Description: Measure the average time to pass an integer from a producer task through a buffer task to a consumer task, using protected buffer Rocket - Alpha 2100 OpenVMS 6.2 0.0201 = clock resolution used for iteration stability Test Name: T000008 Class Name: Tasking CPU Time: 52.93 MICROSECONDS plus or minus 2.646 Wall/CPU: 1.00 ratio. Iteration Count: 102400 Test Description: Measure the average time to pass an integer from a producer task through a buffer task to a consumer task no T000009 Sigpro - sparc 75MHz SunOS 4.1.3 0.006466002 = clock resolution used for iteration stability Test Name: T000008 Class Name: Tasking CPU Time: 332.15 MICROSECONDS plus or minus 16.607 Wall/CPU: 1.00 ratio. Iteration Count: 6400 Test Description: Measure the average time to pass an integer from a producer task through a buffer task to a consumer task Test Name: T000009 Class Name: Tasking CPU Time: 140.85 MICROSECONDS plus or minus 7.042 Wall/CPU: 1.00 ratio. Iteration Count: 12800 Test Description: Measure the average time to pass an integer from a producer task through a buffer task to a consumer task, using protected buffer LTA001 - sparc 200Mhz Solaris 2.5.1 0.001629762 = clock resolution used for iteration stability Test Name: T000008 Class Name: Tasking CPU Time: 309.94 MICROSECONDS plus or minus 15.497 Wall/CPU: 1.00 ratio. Iteration Count: 6400 Test Description: Measure the average time to pass an integer from a producer task through a buffer task to a consumer task Test Name: T000009 Class Name: Tasking CPU Time: 42.29 MICROSECONDS plus or minus 2.114 Wall/CPU: 1.00 ratio. Iteration Count: 25600 Test Description: Measure the average time to pass an integer from a producer task through a buffer task to a consumer task, using protected buffer Pentium 166MHz Windows 95 0.100000002 = clock resolution used for iteration stability Test Name: T000008 Class Name: Tasking CPU Time: 336.91 MICROSECONDS plus or minus 16.846 Wall/CPU: 1.00 ratio. Iteration Count: 102400 Test Description: Measure the average time to pass an integer from a producer task through a buffer task to a consumer task Test Name: T000009 Class Name: Tasking CPU Time: 82.62 MICROSECONDS plus or minus 4.131 Wall/CPU: 1.00 ratio. Iteration Count: 409600 Test Description: Measure the average time to pass an integer from a producer task through a buffer task to a consumer task, using protected buffer package TASK_PACK_8 is -- T000008 task type BUFFER_TYPE is entry TAKE_ITEM ( ITEM : in INTEGER ) ; entry GIVE_ITEM ( ITEM : out INTEGER ) ; end BUFFER_TYPE ; BUFFER_TASK : BUFFER_TYPE ; ... end TASK_PACK_8 ; package body TASK_PACK_8 is task body BUFFER_TYPE is type BUFFER_RANGE is range 0..20 ; subtype BUFFER_INDEX is BUFFER_RANGE range 1..BUFFER_RANGE'LAST ; BUFFER : array ( BUFFER_INDEX ) of INTEGER ; HEAD, TAIL : BUFFER_INDEX := BUFFER_INDEX'FIRST ; BUFFER_COUNT : BUFFER_RANGE := 0 ; begin loop select when BUFFER_COUNT < BUFFER_RANGE'LAST => accept TAKE_ITEM ( ITEM : in INTEGER ) do BUFFER ( HEAD ) := ITEM ; HEAD := ( HEAD mod BUFFER_RANGE'LAST ) + 1 ; BUFFER_COUNT := BUFFER_COUNT + 1 ; end TAKE_ITEM ; or when BUFFER_COUNT > 0 => accept GIVE_ITEM ( ITEM : out INTEGER ) do ITEM := BUFFER ( TAIL ) ; TAIL := ( TAIL mod BUFFER_RANGE'LAST ) + 1 ; BUFFER_COUNT := BUFFER_COUNT - 1 ; end GIVE_ITEM ; end select ; end loop ; end BUFFER_TYPE ; ... end TASK_PACK_8 ; package TASK_PACK_9 is -- T000009 type BUFFER_RANGE is range 0..20 ; subtype BUFFER_INDEX is BUFFER_RANGE range 1..BUFFER_RANGE'LAST ; type BUFFER_ARRAY is array ( BUFFER_INDEX ) of INTEGER ; protected TASK_BUFF_9 is entry TAKE_ITEM ( ITEM : in INTEGER ) ; entry GIVE_ITEM ( ITEM : out INTEGER ) ; private BUFFER : BUFFER_ARRAY ; HEAD, TAIL : BUFFER_INDEX := BUFFER_INDEX'FIRST ; BUFFER_COUNT : BUFFER_RANGE := 0 ; end TASK_BUFF_9 ; ... end TASK_PACK_9 ; package body TASK_PACK_9 is protected body TASK_BUFF_9 is entry TAKE_ITEM ( ITEM : in INTEGER ) when BUFFER_COUNT < BUFFER_RANGE'LAST is begin BUFFER ( HEAD ) := ITEM ; HEAD := ( HEAD mod BUFFER_RANGE'LAST ) + 1 ; BUFFER_COUNT := BUFFER_COUNT + 1 ; end TAKE_ITEM ; entry GIVE_ITEM ( ITEM : out INTEGER ) when BUFFER_COUNT > 0 is begin ITEM := BUFFER ( TAIL ) ; TAIL := ( TAIL mod BUFFER_RANGE'LAST ) + 1 ; BUFFER_COUNT := BUFFER_COUNT - 1 ; end GIVE_ITEM ; end TASK_BUFF_9 ; ... end TASK_PACK_9 ;
HOMEWORK PROBLEM 9 INTRODUCTION TO Ada 95 PURPOSE: To learn more about Ada 95 object oriented programming and how to use it, some uses of child packages, class wide types, and polymorphism. END RESULT: A complete Ada program that is a main procedure that WITH's a package you write containing more types of figures. PROBLEM: Write a package that has a shape derived from the Figure type. Do minimum editing to a copy of the Rectangle_Pkg to make a Diamond_Pkg and define a Diamond type. Add corresponding tests in the main procedure FigureC to test your Diamond type. NOTE: A diamond just has a height and width like a rectangle. This is meant to be easy. The extra credit is much harder. EXTRA CREDIT: Make a child package of the child package Figure_Pkg.Circle_Pkg for an ellipse. The package would be called Figure_Pkg.Circle_Pkg.Ellipse_Pkg . Make the type Ellipse from the type Circle by adding one item to the record called Y_Radius. Add the corresponding tests to FigureC for your new type Ellipse OBSERVE: Watch the naming conventions. They get long. READING: BARNES 12.3 13.1 .. 13.5 ISO 8652:1995 3.9, 3.10, 10. FILES: figurec.ada figurec.bat figure_tagged.ada figure_tagged.bat
HOMEWORK PROBLEM 10 INTRODUCTION TO Ada 95 PURPOSE: To learn about access types Binary trees are used because they rather naturally dictate the use of access types. END RESULT: A complete Ada program that is a main procedure that WITH's a package containing the binary tree manipulation routines INSERT, FIND, DELETE and OUTPUT. Use OUTPUT as a debugging aid as each feature is added. PROBLEM: Use the attached samples as a guide to write a package that has binary tree manipulation routines. You must add a procedure DELETE that takes a name and deletes the names node from the tree. You can build the same tree. To show that the cases work delete ( in order ) "E " , "B " , and "D " . Make provision for doing nothing to the tree if the name is not in the tree. Output the tree after every delete. TURN IN: Printout main procedure and package. Printout of execution for built in test data. OBSERVE: These tree manipulation routines are messy if recursive procedures and functions are not used. These happen to be in the class of "simple" recursive routines that can be thoroughly tested to be sure they terminate. READING : ISO 8652:1995 3.10 plus use the index for all references to "access" Barnes 10.1, 10.2, 10.4 EXTRA : For extra credit make the INSERT and DELETE work to maintain a balanced binary tree. This uses the balance factor BF. The INSERT is not too difficult but the DELETE is a bear! It turns out that just plain binary trees are very inefficient. But, balanced binary trees are very efficient. If the tree is maintained in balanced form at most O(log2 N) searches are needed for an INSERT, FIND or DELETE. On a Baltimore phone book N is a large number! An unbalanced tree could be the worse possible structure if the names were entered alphabetically. Zzepth could be all day looking for his phone number. FILES: hw10.ada hw10.bat Optional taccess1.adb taccess2.adb taccess3.adb taccess4.adb taccess5.adb taccess6.adb
HOMEWORK PROBLEM 11 INTRODUCTION TO Ada 95 PURPOSE: To learn about: Passing subprograms as arguments to other subprograms. Record discriminants. Interfacing to other languages. END RESULT: A complete Ada program that is a main procedure that expands on one of the three subjects in the PURPOSE: EXTRA CREDIT: Expand on any two of the three subjects in the PURPOSE. PROBLEM: Use the attached samples as a guide and expand on the sample. If you want to experiment with interfacing to other languages, you need a compiler on your machine for the other language. Note: gnat always comes with a C compiler, gcc. The file discrim.ada needs to be gnatchoped, then see comments. Basically add Rectangle: to the enumeration type, to the record, to the bodies of the functions. Then uncomment the use of Rectangle in the main procedure. TURN IN: Printout of your code and output from execution. OBSERVE: All of these subjects have special cases. keep it simple and avoid the corners of the language. READING : ISO 8652:1995 Just finish reading any unread sections. Barnes 6.6, 16., 21. FILES: The starter files are in this handout. discrim.ada disc.adb interf.adb c_funct.c interfex.adb c_funct2.c a_funct2.ads a_funct2.adb trig.adb integration.adb Records with discriminates The main concept of a discriminated record is that a record of this type may contain one of a number of sets of values. This is something like a C union, but is safe because you can not access a component that is not present. The main restriction is that a component name can only be used once in any record type definition. Remember, when initializing or storing into a discriminated record, you have to set the discriminant value(s) in the aggregate along with the values for that discriminants components. Examples follow in discrim.ada and disc.adb Interfacing Ada programs to other languages and visa versa Ada 95 Library Packages related to interfacing: Interfaces Interfaces.C Interfaces.C.Pointers Interfaces.C.Strings Interfaces.COBOL Interfaces.Fortran Other packages you may need to get down and dirty: System System.Address_To_Access_Conversion System.Machine_Code System.RPC System.Storage_Elements System.Storage_Pools Some nitty gritty is covered in the makefile makela The simplest example is interf.adb that uses c_funct.c, then interfex.adb, c_funct2.c, a_funct2.ads, a_funct2.adb, then cxbcm.adb that uses cmfinc.f . Passing functions and procedures as parameters. The most common uses are to do numerical integration or minimization processes. The simple trick is to declare an access type for the subprogram specification, then pass the subprogram'access as the parameter. The sticky language issue is that the access type has to be defined at the same scope level where it is used. Generics get around this problem. For a homework problem, write a function of your own and pass it to another function you write. This can all be in one .adb file like integrat.adb (DOS file name)
Last updated 8/30/00