My_Array_Package and My_Aircraft: Ada Code for Array Processing and Aircraft Information, Exercises of Engineering

Two ada code snippets. The first one, my_array_package, is a package that includes procedures for displaying an array in both row-major and column-major orders, sorting the array using bubble sort algorithm, and displaying the sorted array. The second one, my_aircraft, is a package that includes a record type for aircraft_information, a function to create an array of aircraft_information, a procedure to sort the array based on latitude, and a procedure to compute and display the distances between the first aircraft and all other aircraft in the array. Both packages are written by 'joe b' and were last modified on 10/07/03.

Typology: Exercises

2011/2012

Uploaded on 07/22/2012

senapati_007
senapati_007 🇮🇳

3.8

(4)

109 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C-14 Solutions
1. Package Design
Data Structures
An array of nine integers
Subprograms
Function to accept 9 integers
Procedure to display the array in row major order
Procedure to display the array in column major order
Procedure to sort the array using the bubble sort algorithm
Algorithms
Accept_Numbers:
For I in 1 .. 9
Accept an integer
Store it in an array
Row_Major_Display:
For I in 1 .. 9
Display Element in Array(I);
If I mod 3 = 0 then
New_Line
Given the elements are in row-major order, the position locations are sequential.
Column_Major_Display:
For I in 1 .. 3
For J in 1 .. 3
Location_In_Array := I + (J-1)*3
Display Element in Array(Location_In_Array)
New_Line
If the elements are in column-major order, the locations in the one-dimensional
array have to be computed.
Bubble_Sort:
For I in 1 .. Array’Value(1)-1
For J in I+1 .. Array’Value(1)
If Array(I) > Array(J)
Swap the values
Note that the algorithm will sort the array in ascending order.
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download My_Array_Package and My_Aircraft: Ada Code for Array Processing and Aircraft Information and more Exercises Engineering in PDF only on Docsity!

C-14 Solutions

1. Package Design

Data Structures

� An array of nine integers

Subprograms

� Function to accept 9 integers

� Procedure to display the array in row major order

� Procedure to display the array in column major order

� Procedure to sort the array using the bubble sort algorithm

Algorithms

Accept_Numbers:

For I in 1 .. 9

Accept an integer

Store it in an array

Row_Major_Display:

For I in 1 .. 9

Display Element in Array(I);

If I mod 3 = 0 then

New_Line

Given the elements are in row-major order, the position locations are sequential.

Column_Major_Display:

For I in 1 .. 3

For J in 1 .. 3

Location_In_Array := I + (J-1)*

Display Element in Array(Location_In_Array)

New_Line

If the elements are in column-major order, the locations in the one-dimensional

array have to be computed.

Bubble_Sort:

For I in 1 .. Array’Value(1)-

For J in I+1 .. Array’Value(1)

If Array(I) > Array(J)

Swap the values

Note that the algorithm will sort the array in ascending order.

2. Code Listing

Package Listing

Package Specification

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

Checking: c:/docume~2/joeb/desktop/16070/codeso~1/my_array_package.ads (source file time stamp: 2003-10-08 13:48:58)


  1. -- Package specification of a package to
  2. -- 1. Create an array of 9 integers
  3. -- 2. Display the array as a 3x3 matrix
  4. -- i. assuming row-major order
  5. -- ii. assuming column-major order
  6. -- 3. Bubble Sort the 1-D array
  7. -- Specifier : Joe B
  8. -- Date Last Modified : 10/07/

  9. package My_Array_Package is
  10. type My_Array is array (1 .. 9) of Integer;
  11. function Create_Array return My_Array;
  12. procedure Display_Row_Major(Input_Array : in My_Array);
  13. procedure Display_Column_Major(Input_Array : in My_Array);
  14. procedure Bubble_Sort (Input_Array : in out My_Array);
  15. end My_Array_Package;

29 lines: No errors

Package Body

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

Compiling: c:/docume~2/joeb/desktop/16070/codeso~1/my_array_package.adb (source file time stamp: 2003-10-08 13:54:40)


  1. -- Package implementation of My_Array_Package
  2. -- Programmer : Joe B
  1. Temp := Input_Array(I);
  2. Input_Array(I) := Input_Array(J);
  3. Input_Array(J) := Temp;
  4. end if;
  5. end loop;
  6. end loop;
  7. end Bubble_Sort;
  8. end My_Array_Package;

72 lines: No errors

Test Program Listing

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

Compiling: c:/docume~2/joeb/desktop/16070/codeso~1/test_my_array.adb (source file time stamp: 2003- 10-08 14:03:20)


  1. -- Program to test My_Array_Package
  2. -- Programmer : Joe B
  3. -- Date Last Modified : 10/07/

  4. with My_Array_Package;
  5. use My_Array_Package;
  6. with Ada.Text_Io;
  7. procedure Test_My_Array is
  8. New_Array : My_Array_Package.My_Array;
  9. begin
  10. New_Array := Create_Array;
  11. Ada.Text_Io.Put_Line("Displaying unsorted array in Row-Major order");
  12. Display_Row_Major(New_Array);
  13. Ada.Text_Io.Put_Line("Displaying unsorted array in Column-Major order");
  14. Display_Column_Major(New_Array);
  15. Bubble_Sort(New_Array);
  16. Ada.Text_Io.Put_Line("Displaying sorted array in Row-Major order");
  17. Display_Row_Major(New_Array);
  18. Ada.Text_Io.Put_Line("Displaying sorted array in Column-Major order");
  19. Display_Column_Major(New_Array);
  20. end Test_My_Array;

29 lines: No errors

C –15 Solutions

1. Invert a 3x3 Matrix

Data Structure

A new type my_3x3_matrix as a real array (1..3, 1..3)

Subprograms

1. Function to create the matrix

2. Procedure to display the matrix

3. Function to compute the determinant

4. Function to compute the inverse

5. Main program to invert the matrix

Algorithm

Create_Matrix:

For I in 1 .. 3

For J in 1 .. 3

Accept Matrix(I,J)

Return Matrix to the user

Display_Matrix:

For I in 1.. 3

For J in 1.. 3

Display Matrix (I,J);

New_Line

Compute_Determinant:

1. Convert the matrix into a real_matrix (as defined in

Generic_Real_arrays)

2. Compute the determinant using the det function defined in

Generic_Real_Arrays.Operations.

3. Return the computed Value

Compute_Inverse:

1. Convert the matrix into a real_matrix (as defined in

Generic_Real_arrays)

2. Compute the inverse using the Inverse function defined in

Generic_Real_Arrays.Operations.

  1. for J in 1 .. 3 loop
  2. Ada.Text_Io.Put("Please Enter Number in (");
  3. Ada.Text_Io.Put(Integer'Image(I));
  4. Ada.Text_Io.Put(",");
  5. Ada.Text_Io.Put(Integer'Image(J));
  6. Ada.Text_Io.Put(") : ");
  7. Ada.Float_Text_Io.Get(Input_Matrix(I,J));
  8. Ada.Text_Io.Skip_Line;
  9. end loop;
  10. end loop;
  11. return Input_Matrix;
  12. end Create_My_Matrix;
  13. -- user procedure to display the matrix
  14. procedure Display_My_Matrix (
  15. Input_Matrix : in My_3x3_Matrix ) is
  16. begin
  17. for I in 1 .. 3 loop
  18. for J in 1 .. 3 loop
  19. Ada.Float_Text_Io.Put(Input_Matrix(I,J));
  20. end loop;
  21. Ada.Text_Io.New_Line;
  22. end loop;
  23. end Display_My_Matrix;
  24. -- function to compute the inverse
  25. function My_Inverse (
  26. Input_Matrix : My_3x3_Matrix )
  27. return My_3x3_Matrix is
  28. -- local variable to convert the user defined matrix to the package defined
  29. -- matrix
  30. My_Real_Matrix : My_Real_Array.Real_Matrix (1 .. 3, 1 .. 3);
  31. Output_Matrix : My_3x3_Matrix;
  32. begin
  33. -- do type conversion from user defined type to package defined type
  34. for I in 1.. 3 loop
  35. for J in 1 .. 3 loop
  36. My_Real_Matrix(I,J) := Input_Matrix(I,J);
  37. end loop;
  38. end loop;
  39. -- compute inverse using the generic package function
  40. My_Real_Matrix := My_Real_Array_Operations.Inverse(My_Real_Matrix);
  41. -- reconvert back to user defined type
  42. for I in 1.. 3 loop
  43. for J in 1 .. 3 loop
  44. Output_Matrix(I,J) := My_Real_Matrix(I,J);
  45. end loop;
  46. end loop;
  47. --return computed inverse
  48. return Output_Matrix;
  49. end My_Inverse;
  50. function My_Determinant (
  1. Input_Matrix : My_3x3_Matrix )
  2. return Float is
  3. My_Real_Matrix : My_Real_Array.Real_Matrix (1 .. 3, 1 .. 3);
  4. begin
  5. -- do type conversion from user defined type to package type
  6. for I in 1.. 3 loop
  7. for J in 1 .. 3 loop
  8. My_Real_Matrix(I,J) := Input_Matrix(I,J);
  9. end loop;
  10. end loop;
  11. -- compute the determinant and return to user
  12. return (My_Real_Array_Operations.Det(My_Real_Matrix));
  13. end My_Determinant;
  14. begin
  15. -- create and display the matrix
  16. Ada.Text_Io.Put_Line("Please Enter the Matrix : ");
  17. A := Create_My_Matrix;
  18. Ada.Text_Io.Put_Line("Created Matrix : ");
  19. Display_My_Matrix(A);
  20. Ada.Text_Io.New_Line;
  21. -- compute determinant
  22. Det := My_Determinant(A);
  23. -- check for singularity
  24. if Det = 0.0 then
  25. Ada.Text_Io.Put_Line("Cannot invert the matrix");
  26. else
  27. -- compute inverse and display
  28. B := My_Inverse(A);
  29. Ada.Text_Io.New_Line;
  30. Ada.Text_Io.Put_Line("Inverted Matrix : ");
  31. Display_My_Matrix(B);
  32. end if;
  33. end Pset_4_Inversion;

131 lines: No errors

Algorithms

Create_Aircraft:

For I in 1 .. 10

Prompt the user to input relevant information

Store the information in Array(I)

Return Array to the main program

Sort_Aircraft:

For I in 1 .. Num_of_Aircraft -

For J in I+1 .. Num_Of_Aircraft

If Array(I).Latitude > Array(J).Latitude

Swap the records in Array(I) and Array(J)

Return sorted array to the user

Compute_Distances:

For I in 2 .. Num_Of_Aircraft

Compute difference in latitudes (dlat)

Compute the differences in longitude (dlon)

Covert the differences into distances using the WGS-

approximations in the handout (dlat_dist, dlon_dist)

Distance between the aircraft = sqrt(dlat_dist^2 + dlon_dist^2)

Display computed distance to the user.

Main Program:

Create aircraft using the Create_Aircraft function

Sort the aircraft

Compute the distances and display it to the user

Code Listing

My_Aircraft Package Specification

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

Checking: c:/docume~2/joeb/desktop/16070/codeso~1/my_aircraft.ads (source file time stamp: 2003-10- 18:10:40)


  1. -- Package to specify aircraft parameters and the

  2. -- related subprograms

  3. -- Specifier : Joe B

  4. -- Date Last Modified : 10/07/


  5. package My_Aircraft is

  6. Num_of_Aircraft : constant Integer := 10;

  7. type Aircraft_Information is

  1. record
  2. Aircraft_Number : Integer;
  3. Latitude : Float;
  4. Longitude : Float;
  5. Heading : Float;
  6. Velocity : Float;
  7. end record;
  8. type Aircraft_Array is array (1 .. Num_Of_Aircraft) of Aircraft_Information;
  9. Latitude_Conversion : constant Float := 1852.24;
  10. Longitude_Conversion : constant Float := 1314.13 ;
  11. function Get_Aircraft_Info return Aircraft_Array;
  12. procedure Sort_Aircraft (
  13. Input_Array : in out Aircraft_Array );
  14. procedure Compute_Distances (
  15. Input_Array : in Aircraft_Array );
  16. end My_Aircraft;

32 lines: No errors

My_Aircraft Package Body

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

Compiling: c:/docume~2/joeb/desktop/16070/codeso~1/my_aircraft.adb (source file time stamp: 2003-10- 08 18:26:26)


  1. -- Package body for My_Aircraft

  2. -- Programmer: Joe B

  3. -- Date Last Modified : 10/07/


  4. with Ada.Text_Io;

  5. with Ada.Integer_Text_Io;

  6. with Ada.Float_Text_Io;

  7. with Ada.Numerics.Elementary_Functions;

  8. package body my_aircraft is

  9. function Get_Aircraft_Info return Aircraft_Array is

  10. Output_Array : Aircraft_Array;

  11. begin

  12. for I in 1 .. Num_of_Aircraft loop

  13. Ada.Text_Io.Put("Please Enter Information of Aircraft");

  14. Ada.Text_Io.Put(Integer'Image(I));

  15. Ada.Text_Io.New_Line;

  16. Ada.Text_Io.Put("Aircraft Id : ");

  17. Ada.Integer_Text_IO.Get(Output_Array(I).Aircraft_Number);

  18. Ada.Text_Io.Skip_Line;

  19. end Compute_Distances;

  20. end My_Aircraft;

87 lines: No errors

Main Program

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

Compiling: c:/docume~2/joeb/desktop/16070/codeso~1/test_my_aircraft.adb (source file time stamp: 2003- 10-08 18:12:00)


  1. -- Program to test My_Aircraft
  2. -- Programmer : Joe B
  3. -- Date Last Modified : 10/07/

  4. with My_Aircraft;
  5. procedure Test_My_Aircraft is
  6. Test_Aircraft : My_Aircraft.Aircraft_Array;
  7. begin
  8. Test_Aircraft := My_Aircraft.Get_Aircraft_Info;
  9. My_Aircraft.Sort_Aircraft (Test_Aircraft);
  10. My_Aircraft.Compute_Distances(Test_Aircraft);
  11. end Test_My_Aircraft;

20 lines: No errors