Upload
regina-beasley
View
214
Download
0
Embed Size (px)
Citation preview
Useful Ada Information
Help for programming assignment
Clarifications
• You must read and write the moves directly – you may NOT convert them to strings for printing.
• You should print out the number of ties as well as the number of computer wins and user wins
• You need to handle the situation where the user types in rack or scisssors or piper (for example)
• If an exception occurs, you need to handle it and continue until either the user or the computer has won 3 games.
• Don’t use use clauses
Declaring a user-defined type(enumeration type)
• type typename is ( elementList );
• elementList → element
• elementList → element , {elementList}
• element can not be numeric
• element can not contain a reserved word
• element can not have spaces in it
Examples
• type car is (ford, chevy, lincoln, bmw);
• type city is (rome, naples, bologna);
• type die is (1,2,3,4,5,6);
• type die is (one, two, three, four, five,six);
• type city is (new york, paris);
• type city is (old town, paris);
Instantiating an enumeration type
• package name_io is new ada.text_io.enumeration_io (enum => type);
• instantiation allows reading and writing of enumerated type variables
• enum is the formal parameter name• => is called arrow• this example uses named parameter passing• formal parameter name is NOT required
Here’s the Ada specification for the procedure to print an integer
Procedure put (item : in Num;
width : in Field := Default_Width;
base : in Number_Base := Default_base;
• The default_base is 10• The default_width is num’width
• Given an integer variable named number• The following are equivalent calls to this procedure
Ada.integer_text_io.put (number);
Ada.integer_text_io.put (item => number);
Procedure put (continued)
• Given an integer variable named number, suppose you want the column width of your output to be 10 and you want to use the default base
• The following statements produce the same results• Ada.integer_text_io.put (number, 10);• Ada.integer_text_io.put (item => number, width => 10);• Ada.integer_text_io.put (width => 10, item => number);• Ada.integer_text_io.put (item => number, width => 10,
base => 10;• Ada.integer_text_io.put (number, 10, 10);• Ada.integer_text_io.put (base =>10, item => number,
width => 10);
Examples
• type car is (ford, chevy, lincoln, bmw);
• package car_io is new ada.text_io.enumeration_io (enum => car);
• type city is (rome, naples, bologna);
• package city_io is new ada.text_io.enumeration_io (city);
Examples
• type car is (ford, chevy, lincoln, bmw);
• package car_io is new ada.text_io.enumeration_io (enum => car);
• myCar : car;
• car_io.get( myCar);
• car_io.put (myCar);
Enumeration types have attributes
• typename’pos
• typename’val
• typename’first
• typename’last
• The enumeration type’s element list is ordered
• The first element is in position 0
Ada Random Number Generator
• The information on random number generation can be found in the Language Reference Manual at
• http://www.adahome.com/rm95/rm9x-A-05-02.html• Here is an example of code from the above site using Ada’s random number generator
• with Ada.Numerics.Discrete_Random; • procedure Dice_Game is • subtype Die is Integer range 1 .. 6; • subtype Dice is Integer range 2*Die'First .. 2*Die'Last; • package Random_Die is new Ada.Numerics.Discrete_Random (Die); • use Random_Die; • G : Generator; • D : Dice; • begin • Reset (G); -- Start the generator in a unique state in each run • loop -- Roll a pair of dice; sum and process the results • D := Random(G) + Random(G); ... • end loop; • end Dice_Game;
Ada subprograms
• can be separately compiled• can be separately executed
– execution only shows if there is output
• ada subprogram specifications – have extension .ads– header ends in a semicolon
• Ada subprogram bodies – have extension .adb– header ends in reserved word is
Ada functions
• Function body (i.e. Dot_Product.adb) below comes from the language reference manual
http://www.adahome.com/rm95/rm9x-06-03.html
function Dot_Product(Left, Right : Vector) return Real isSum : Real := 0.0; begin Check(Left'First = Right'First and Left'Last = Right'Last); for J in Left'Range loop Sum := Sum + Left(J)*Right(J); end loop; return Sum; end Dot_Product;
Ada procedures• subprogram_body ::= subprogram_specification is declarative_part
begin handled_sequence_of_statements end [designator];
procedure Push(E : in Element_Type; S : in out Stack) is begin if S.Index = S.Size then
raise Stack_Overflow; else S.Index := S.Index + 1; S.Space(S.Index) := E; end if; end Push;
above Procedure example can be found at http://www.adahome.com/rm95/rm9x-06-03.html