Docsity

Ada Programming: Translating Dates and Working with Enumerations and Packages, Exercises of Engineering

Toggle sidebar
bg1
C8-1
Algorithm
1. Use a subtype to represent the numbers for months
2. Use an enumeration to represent the named months
3. Use an enumeration to represent the roman months
4. Get the inputs from the user
5. Convert the month into roman and named formats using
a. New_Type_Package’Val(Month_Type’Pos(Month) –1);
6. Display the months in all three formats to the user.
Note: The enumerations range from 0 to (number_of_elements_in_Enumeration –1).
Code Listing
Compiling: c:/docume~2/jk/desktop/16070/codeso~1/translate_dates.adb (source file time stamp: 2003-09-
24 01:35:00)
1. --------------------------------------------------------------------
2.
3.
4.
5. --------------------------------------------------------------------
6.
7. with Ada.Text_Io;
8. with Ada.Integer_Text_Io;
9.
10. procedure Translate_Dates is
11. --use a subtype to limit the date to be between 1 and 31.
12. subtype Date_Type is Integer range 1..31;
13. -- use a subtype to limit the month to be between 1 and 12
14. subtype Month_Type is Integer range 1..12;
15.
16. -- define an enumeration type for roman months
17. type Roman_Month_Type is
18. (I,
19. Ii,
20. Iii,
21. Iv,
22. V,
23. Vi,
24. Vii,
25. Viii,
26. Ix,
27. X,
28. Xi,
29. Xii);
30.
31. -- define an enumeration type for names of months
32. type Named_Month_Type is
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Ada Programming: Translating Dates and Working with Enumerations and Packages and more Exercises Engineering in PDF only on Docsity!

C8-

Algorithm

1. Use a subtype to represent the numbers for months

2. Use an enumeration to represent the named months

3. Use an enumeration to represent the roman months

4. Get the inputs from the user

5. Convert the month into roman and named formats using

a. New_Type_Package’Val(Month_Type’Pos(Month) –1);

6. Display the months in all three formats to the user.

Note: The enumerations range from 0 to (number_of_elements_in_Enumeration –1).

Code Listing

Compiling: c:/docume~2/jk/desktop/16070/codeso~1/translate_dates.adb (source file time stamp: 2003-09- 24 01:35:00)



  1. with Ada.Text_Io;
  2. with Ada.Integer_Text_Io;
  3. procedure Translate_Dates is
  4. --use a subtype to limit the date to be between 1 and 31.
  5. subtype Date_Type is Integer range 1..31;
  6. -- use a subtype to limit the month to be between 1 and 12
  7. subtype Month_Type is Integer range 1..12;
  8. -- define an enumeration type for roman months
  9. type Roman_Month_Type is
  10. (I,
  11. Ii,
  12. Iii,
  13. Iv,
  14. V,
  15. Vi,
  16. Vii,
  17. Viii,
  18. Ix,
  19. X,
  20. Xi,
  21. Xii);
  22. -- define an enumeration type for names of months
  23. type Named_Month_Type is
  1. (January,
  2. February,
  3. March,
  4. April,
  5. May,
  6. June,
  7. July,
  8. August,
  9. September,
  10. October,
  11. November,
  12. December);
  13. -- create a package that can print the month in roman numerals
  14. package Roman_Month_Io is new Ada.Text_Io.Enumeration_Io(Enum => Roman_Month_Type);
  15. -- create a package that can print the month's name
  16. package Named_Month_Io is new Ada.Text_Io.Enumeration_Io(Enum => Named_Month_Type);
  17. Year : Integer;
  18. Date : Date_Type;
  19. Month : Month_Type;
  20. Roman_Month : Roman_Month_Type;
  21. Named_Month : Named_Month_Type;
  22. begin
  23. Ada.Text_Io.Put("Please Enter the Date 1..31 : ");
  24. Ada.Integer_Text_Io.Get(Date);
  25. Ada.Text_Io.New_Line;
  26. Ada.Text_Io.Put("PLease Enter the Month 1 ..12 : ");
  27. Ada.Integer_Text_Io.Get(Month);
  28. Ada.Text_Io.New_Line;
  29. Ada.Text_Io.Put("PLease Enter the Year: ");
  30. Ada.Integer_Text_Io.Get(Year);
  31. Ada.Text_Io.New_Line;
  32. -- convert the month into roman month
  33. Roman_Month := Roman_Month_Type'Val(Month_Type'Pos(Month)-1);
  34. -- convert the month into the name
  35. Named_Month := Named_Month_Type'Val(Month_Type'Pos(Month)-1);
  36. -- display the dates in regular format
  37. Ada.Text_Io.Put(" i. ");
  38. Ada.Integer_Text_Io.Put(Date);
  39. Ada.Text_Io.Put(" / ");
  40. Ada.Integer_Text_Io.Put(Month);
  41. Ada.Text_Io.Put(" / ");
  42. Ada.Integer_Text_Io.Put(Year);
  43. Ada.Text_Io.New_Line;
  44. -- display the date in named format
  45. Ada.Text_Io.Put(" ii. ");
  46. Ada.Integer_Text_Io.Put(Date);
  47. Ada.Text_Io.Put(" ");
  48. Named_Month_Io.Put(Named_Month);

C9-

Algorithm

Package Specification

1. Declare the type.

2. Create a new package within the package specification to provide predefined

functions for the enumeration type.

3. Declare the function prototypes for both the predecessor and successor functions.

Package Implementation

1. Successor function:

a. if the input_value = Type’Last then

return Type’First

b. else

return Type’Succ(input_value);

2. Predecessor function:

a. if the input_value = Type’First then

return Type’Last

b. else

return Type’Pred(input_value);

Code Listing

GNAT 3.13p (20000509) Copyright 1992-2000 Free Software Foundation, Inc.

Checking: c:/docume~2/jk/desktop/16070/codeso~1/my_type_package.ads (source file time stamp: 2003- 09-24 01:59:20)


  1. -- Package specified to declare the type and two functions
  2. -- Specifier : Jayakanth Srinivasan
  3. -- Date Last Modified : 09/23/

  4. with Ada.Text_Io;
  5. package My_Type_Package is
  6. type Day is
  7. (Monday,
  8. Tuesday,
  9. Wednesday,
  10. Thursday,
  11. Friday,
  12. Saturday,
  13. Sunday);
  1. package Day_Io is new Ada.Text_Io.Enumeration_Io(Enum => Day);
  2. function Successor (
  3. Day_In : Day )
  4. return Day;
  5. function Predecessor (
  6. Day_In : Day )
  7. return Day;
  8. end My_Type_Package;

28 lines: No errors

GNAT 3.13p (20000509) Copyright 1992-2000 Free Software Foundation, Inc.

Compiling: c:/docume~2/jk/desktop/16070/codeso~1/my_type_package.adb (source file time stamp: 2003- 09-24 02:00:04)


  1. -- Package implementation of My_type package
  2. -- Implementer : Jayakanth Srinivasan
  3. -- Date Last Modified : 09/23/

  4. package body My_Type_Package is
  5. function Successor (
  6. Day_In : Day )
  7. return Day is
  8. begin
  9. if Day_In = Day'Last then
  10. return Day'First;
  11. else
  12. return Day'Succ(Day_In);
  13. end if;
  14. end Successor;
  15. function Predecessor (
  16. Day_In : Day )
  17. return Day is
  18. begin
  19. if Day_In = Day'First then
  20. return Day'Last;
  21. else
  22. return Day'Pred(Day_In);
  23. end if;
  24. end Predecessor;
  25. end My_Type_Package;

32 lines: No errors

C-10 1.

For X = 3

Case 1.

IF x >= 0 THEN x:= x+1; ELSIF x >=1 THEN x := x + 2; END IF ;

In the case above, only the x:= x+1 statement is executed and the result is 4;

Case 2.

IF x >= 0 THEN x := x + 1; END IF ; IF x >= 1 THEN x := x + 2; END IF ;

In this case, both the x:=x+1 and x:= x+2 statements will be executed and the result is 6.

C-10 2.

Package Specification Listing

GNAT 3.13p (20000509) Copyright 1992-2000 Free Software Foundation, Inc.

Checking: c:/docume~2/jk/desktop/16070/codeso~1/my_math_package.ads (source file time stamp: 2003- 09-24 03:27:46)

. ----------------------------------------------------------- . -- Package specified to implement two arithmetic functions . -- Specifier : Jayakanth Srinivasan . -- Date Last Modified : 09/23/ . ----------------------------------------------------------- . . . package My_Math_Package is . subtype Menu_Choice is Integer range 1 .. 3; . . procedure Menu ( . My_Menu_Choice : out Menu_Choice ); . . function Add ( . X : Float; . Y : Float ) . return Float; . . function Multiply ( . X : Integer; . Y : Integer ) . return Integer; . end My_Math_Package;

23 lines: No errors

Package Code Listing

GNAT 3.13p (20000509) Copyright 1992-2000 Free Software Foundation, Inc.

Compiling: c:/docume~2/jk/desktop/16070/codeso~1/my_math_package.adb (source file time stamp: 2003- 09-24 03:27:46)


  1. -- Package implementation of My_Math package
  2. -- Implementer : Jayakanth Srinivasan
  3. -- Date Last Modified : 09/23/

  4. with Ada.Integer_Text_Io;
  5. with Ada.Text_Io;
  6. with Ada.Float_Text_Io; |

C-10 3.

Algorithm

1. Display the menu to the user

2. Get the menu choice from the user

3. If Choice is 1 then

a. Prompt the user for two floating point numbers

b. Clear the screen

c. Compute the sum using the math package

d. Display the answer in the required format.

4. If Choice is 2 then

a. Prompt the user for two integer numbers

b. Clear the screen

c. Compute the product using the math package

d. Display the answer in the required format.

5. If Choice is 3 then

a. Exit the program

Code Listing

GNAT 3.13p (20000509) Copyright 1992-2000 Free Software Foundation, Inc.

Compiling: c:/docume~2/jk/desktop/16070/codeso~1/test_math.adb (source file time stamp: 2003-09- 03:47:18)

  1. -- Program to implement a menu driven program using the
  2. -- the math package
  3. -- Programmer : Jayakanth Srinivasan
  4. -- Date Last Modified : 09/23/

  5. with My_Math_Package;
  6. with Ada.Text_Io;
  7. with Ada.Float_Text_Io;
  8. with Ada.Integer_Text_Io;
  9. with Screen;
  10. procedure Test_Math is
  11. Choice : My_Math_Package.Menu_Choice;
  12. X,
  13. Y : Integer;
  14. Number_X,
  15. Number_Y : Float;
  16. begin
  17. loop
  18. -- obtain the choice from the user
  19. My_Math_Package.Menu(Choice);
  20. -- exit if the user chooses 3
  1. exit when Choice = 3;
  2. case Choice is
  3. when 1 =>
  4. -- obtain two floating point numbers
  5. Ada.Text_Io.Put ("Please Enter the Value of X : ");
  6. Ada.Float_Text_Io.Get(Number_X);
  7. Ada.Text_Io.Skip_Line;
  8. Ada.Text_Io.Put("Please Enter the Value of Y : ");
  9. Ada.Float_Text_Io.Get(Number_Y);
  10. Ada.Text_Io.Skip_Line;
  11. -- clear the screen
  12. Screen.Clearscreen;
  13. -- display the results
  14. Ada.Text_Io.Put("Adding");
  15. Ada.Float_Text_Io.Put(Number_X);
  16. Ada.Text_Io.Put("and");
  17. Ada.Float_Text_Io.Put(Number_Y);
  18. Ada.Text_Io.Put(":");
  19. Ada.Text_Io.New_Line;
  20. Ada.Float_Text_Io.Put(Number_X);
  21. Ada.Text_Io.Put("+");
  22. Ada.Float_Text_Io.Put(Number_Y);
  23. Ada.Text_Io.Put("=");
  24. Ada.Float_Text_Io.Put(My_Math_Package.Add(Number_X, Number_Y));
  25. Ada.Text_Io.New_Line;
  26. when 2=>
  27. -- obtain two integers
  28. Ada.Text_Io.Put ("Please Enter the Value of X : ");
  29. Ada.Integer_Text_Io.Get(X);
  30. Ada.Text_Io.Skip_Line;
  31. Ada.Text_Io.Put("Please Enter the Value of Y : ");
  32. Ada.Integer_Text_Io.Get(Y);
  33. Ada.Text_Io.Skip_Line;
  34. -- clear the screen
  35. Screen.Clearscreen;
  36. -- display the product
  37. Ada.Text_Io.Put("Multiplying");
  38. Ada.Integer_Text_Io.Put(X);
  39. Ada.Text_Io.Put("and");
  40. Ada.Integer_Text_Io.Put(Y);
  41. Ada.Text_Io.Put(":");
  42. Ada.Text_Io.New_Line;
  43. Ada.Integer_Text_Io.Put(X);
  44. Ada.Text_Io.Put("*");
  45. Ada.Integer_Text_Io.Put(Y);
  46. Ada.Text_Io.Put("=");
  47. Ada.Integer_Text_Io.Put(My_Math_Package.Multiply(X, Y));

Problem S1 Solutions 1.a

x  y  2 z  1

x  4 y  2 z 5

x  y  z 0

x  y  2 z  1

x  4 y  2 z 5

2 x  5 y 4

2(3 x  6 y 5)

3(2 x  5 y 4)

3 y 2

y 2 / 3

3 x  6(2 / 3) 5

3 x 1

x 1/ 3

1/ 2  2 / 3  z 0

z 1

x  4 y  2 z 5

2( x  y  z ) 0

3 x  6 y 5

1.b

« ». Subtract row 1 from row 2

Ÿ « 3 4 6. Subtract row 1 from row 3

z 1

y 2 / 3

x 1/ 3

1.c

x

y

x 1/ 3

y 2 / 3

z

z 1

ª a^ b^ c^ º

det « d e f^ »^ a ( ei  fh )  b ( di  fg )  c ( dh  eg )

«¬ g h i »¼