HOMEWORK PROBLEM 2_4 INTRODUCTION TO Ada II 586.2 PURPOSE: To learn about generic procedures This is the second part of a two part homework. Even though packages are very important, there is still a use for stand alone procedures. This assignment requires the development and use of a generic procedure. END RESULT: A complete Ada program that is a main procedure that WITH's and instantiates a generic procedure you write. The generic procedure will be able to sort many types of arrays . Use of a generic sort will provide a useful tool for future applications. PROBLEM: Using the attached examples, develop a stand alone, independently compiled, generic procedure for the Shelli sort . Make up a main procedure that instantiates the generic procedure and uses the instantiated procedure to sort an array of something. A possible main procedure similar to the extra credit of HW2_3 is EXTRA_SORT_2.ADA. SUGGESTION: Convert PUSERS:[ADA.ADACLASS]SHELLI.ADA from the package SORTS to a generic package GENERIC_SORTS. Then extract the procedure SHELLI from the generic package to become a generic procedure GENERIC_SHELLI. TURN IN: Printout of compilation of main procedure and package ( with no errors!) Printout of execution for built in test data. OBSERVE: This is basically a simplification process. There are times when procedures can be more appropriate than packages. READING : MIL-STD-1815A Reread all of chapter 12. You will understand more ( But, not all ) Barnes book is chapter 13. The syntax of one form of a generic package is: generic -- generic formal part package NAME is -- package specification ( could contain a procedure specification ) end NAME ; package body NAME is -- package body ( could contain a procedure body ) end NAME ; The syntax of one form of a generic procedure is: generic -- generic formal part procedure NAME ( ... ) ; procedure NAME ( ... ) is -- procedure body end NAME ; A stand alone procedure can usually be easily extracted from a package and equally as easily inserted into a package. The choice of "packaging" is determined by the desired structure of the program. Some technique is needed in writing generic packages and procedures. Consider the following generic formal part : type ELEMENT_TYPE is private ; -- anything including records and arrays type INDEX_TYPE is ( <> ) ; -- any integer type of enumeration type type ARRAY_TYPE is array ( INDEX_TYPE range <> ) of ELEMENT_TYPE ; Note: Three generic formal parameters are defined ELEMENT_TYPE INDEX_TYPE ARRAY_TYPE and ARRAY_TYPE uses the previous two in its definition. The problem is to write generic bodies that involve objects of these types. A , B : ELEMENT_TYPE ; -- what can you do with A and B ? I : INTEGER ; J : INDEX_TYPE ; -- what can you do to combine I and J ? Q : ARRAY_TYPE ; -- what attributes are available ? Q'FIRST .. Q'LAST OK subscripts A'FIRST NO J'FIRST .. J'LAST OK low .. high values I + INDEX_TYPE'POS(J) OK I+J NO J + INDEX_TYPE'VAL(I) OK if it fits J := INDEX_TYPE'SUCC(J) OK J := J + 1 NO J := INDEX_TYPE'PRED(J) OK J := J - 1 NO A := Q(J) OK A := Q(I) NO A := B ; OK A := 0 ; NO J := INDEX_TYPE'VAL(INDEX_TYPE'POS(J'FIRST) + INDEX_TYPE'POS(J'LAST))/2 -- the middle of an enumeration type J_STRING := INDEX_TYPE'IMAGE(J) ; J := INDEX_TYPE'VALUE(J_STRING) ; J := INDEX_TYPE'SUCC(Q'FIRST) J := INDEX_TYPE'PRED(Q'LAST) Unfortunately, a concession was made that could help compiler writers to produce better code. This is one of the classic cases of Dijkstra's "More sins are committed in the name of efficiency that any other name" This uses an UNCONSTRAINED array : type MY_COLOR is ( RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET ) ; type ALL_COLOR is ( BLACK, BROWN, RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE, GREY, WHITE ) ; type COLOR_ARRAY is array ( ALL_COLOR range <> ) of MY_COLOR ; procedure MY_SHELLI is new GENERIC_SHELLI ( MY_COLOR , ALL_COLOR, COLOR_ARRAY ) ; This uses a CONSTRAINED array : type MY_COLOR is ( RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET ) ; type ALL_COLOR is ( BLACK, BROWN, RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE, GREY, WHITE ) ; type COLOR_ARRAY is array ( ALL_COLOR ) of MY_COLOR ; procedure MY_SHELLI is new GENERIC_SHELLI ( MY_COLOR , ALL_COLOR, COLOR_ARRAY ) ; Two different generic procedures are required ! The procedures differ only in the generic_formal part by removing " range <> "