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)
- -- Package specification of a package to
- -- 1. Create an array of 9 integers
- -- 2. Display the array as a 3x3 matrix
- -- i. assuming row-major order
- -- ii. assuming column-major order
- -- 3. Bubble Sort the 1-D array
- -- Specifier : Joe B
- -- Date Last Modified : 10/07/
- package My_Array_Package is
- type My_Array is array (1 .. 9) of Integer;
- function Create_Array return My_Array;
- procedure Display_Row_Major(Input_Array : in My_Array);
- procedure Display_Column_Major(Input_Array : in My_Array);
- procedure Bubble_Sort (Input_Array : in out My_Array);
- 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)
- -- Package implementation of My_Array_Package
- -- Programmer : Joe B
- Temp := Input_Array(I);
- Input_Array(I) := Input_Array(J);
- Input_Array(J) := Temp;
- end if;
- end loop;
- end loop;
- end Bubble_Sort;
- 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)
- -- Program to test My_Array_Package
- -- Programmer : Joe B
- -- Date Last Modified : 10/07/
- with My_Array_Package;
- use My_Array_Package;
- with Ada.Text_Io;
- procedure Test_My_Array is
- New_Array : My_Array_Package.My_Array;
- begin
- New_Array := Create_Array;
- Ada.Text_Io.Put_Line("Displaying unsorted array in Row-Major order");
- Display_Row_Major(New_Array);
- Ada.Text_Io.Put_Line("Displaying unsorted array in Column-Major order");
- Display_Column_Major(New_Array);
- Bubble_Sort(New_Array);
- Ada.Text_Io.Put_Line("Displaying sorted array in Row-Major order");
- Display_Row_Major(New_Array);
- Ada.Text_Io.Put_Line("Displaying sorted array in Column-Major order");
- Display_Column_Major(New_Array);
- 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.
- for J in 1 .. 3 loop
- Ada.Text_Io.Put("Please Enter Number in (");
- Ada.Text_Io.Put(Integer'Image(I));
- Ada.Text_Io.Put(",");
- Ada.Text_Io.Put(Integer'Image(J));
- Ada.Text_Io.Put(") : ");
- Ada.Float_Text_Io.Get(Input_Matrix(I,J));
- Ada.Text_Io.Skip_Line;
- end loop;
- end loop;
- return Input_Matrix;
- end Create_My_Matrix;
- -- user procedure to display the matrix
- procedure Display_My_Matrix (
- Input_Matrix : in My_3x3_Matrix ) is
- begin
- for I in 1 .. 3 loop
- for J in 1 .. 3 loop
- Ada.Float_Text_Io.Put(Input_Matrix(I,J));
- end loop;
- Ada.Text_Io.New_Line;
- end loop;
- end Display_My_Matrix;
- -- function to compute the inverse
- function My_Inverse (
- Input_Matrix : My_3x3_Matrix )
- return My_3x3_Matrix is
- -- local variable to convert the user defined matrix to the package defined
- -- matrix
- My_Real_Matrix : My_Real_Array.Real_Matrix (1 .. 3, 1 .. 3);
- Output_Matrix : My_3x3_Matrix;
- begin
- -- do type conversion from user defined type to package defined type
- for I in 1.. 3 loop
- for J in 1 .. 3 loop
- My_Real_Matrix(I,J) := Input_Matrix(I,J);
- end loop;
- end loop;
- -- compute inverse using the generic package function
- My_Real_Matrix := My_Real_Array_Operations.Inverse(My_Real_Matrix);
- -- reconvert back to user defined type
- for I in 1.. 3 loop
- for J in 1 .. 3 loop
- Output_Matrix(I,J) := My_Real_Matrix(I,J);
- end loop;
- end loop;
- --return computed inverse
- return Output_Matrix;
- end My_Inverse;
- function My_Determinant (
- Input_Matrix : My_3x3_Matrix )
- return Float is
- My_Real_Matrix : My_Real_Array.Real_Matrix (1 .. 3, 1 .. 3);
- begin
- -- do type conversion from user defined type to package type
- for I in 1.. 3 loop
- for J in 1 .. 3 loop
- My_Real_Matrix(I,J) := Input_Matrix(I,J);
- end loop;
- end loop;
- -- compute the determinant and return to user
- return (My_Real_Array_Operations.Det(My_Real_Matrix));
- end My_Determinant;
- begin
- -- create and display the matrix
- Ada.Text_Io.Put_Line("Please Enter the Matrix : ");
- A := Create_My_Matrix;
- Ada.Text_Io.Put_Line("Created Matrix : ");
- Display_My_Matrix(A);
- Ada.Text_Io.New_Line;
- -- compute determinant
- Det := My_Determinant(A);
- -- check for singularity
- if Det = 0.0 then
- Ada.Text_Io.Put_Line("Cannot invert the matrix");
- else
- -- compute inverse and display
- B := My_Inverse(A);
- Ada.Text_Io.New_Line;
- Ada.Text_Io.Put_Line("Inverted Matrix : ");
- Display_My_Matrix(B);
- end if;
- 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)
-- Package to specify aircraft parameters and the
-- related subprograms
-- Specifier : Joe B
-- Date Last Modified : 10/07/
package My_Aircraft is
Num_of_Aircraft : constant Integer := 10;
type Aircraft_Information is
- record
- Aircraft_Number : Integer;
- Latitude : Float;
- Longitude : Float;
- Heading : Float;
- Velocity : Float;
- end record;
- type Aircraft_Array is array (1 .. Num_Of_Aircraft) of Aircraft_Information;
- Latitude_Conversion : constant Float := 1852.24;
- Longitude_Conversion : constant Float := 1314.13 ;
- function Get_Aircraft_Info return Aircraft_Array;
- procedure Sort_Aircraft (
- Input_Array : in out Aircraft_Array );
- procedure Compute_Distances (
- Input_Array : in Aircraft_Array );
- 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)
-- Package body for My_Aircraft
-- Programmer: Joe B
-- Date Last Modified : 10/07/
with Ada.Text_Io;
with Ada.Integer_Text_Io;
with Ada.Float_Text_Io;
with Ada.Numerics.Elementary_Functions;
package body my_aircraft is
function Get_Aircraft_Info return Aircraft_Array is
Output_Array : Aircraft_Array;
begin
for I in 1 .. Num_of_Aircraft loop
Ada.Text_Io.Put("Please Enter Information of Aircraft");
Ada.Text_Io.Put(Integer'Image(I));
Ada.Text_Io.New_Line;
Ada.Text_Io.Put("Aircraft Id : ");
Ada.Integer_Text_IO.Get(Output_Array(I).Aircraft_Number);
Ada.Text_Io.Skip_Line;
end Compute_Distances;
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)
- -- Program to test My_Aircraft
- -- Programmer : Joe B
- -- Date Last Modified : 10/07/
- with My_Aircraft;
- procedure Test_My_Aircraft is
- Test_Aircraft : My_Aircraft.Aircraft_Array;
- begin
- Test_Aircraft := My_Aircraft.Get_Aircraft_Info;
- My_Aircraft.Sort_Aircraft (Test_Aircraft);
- My_Aircraft.Compute_Distances(Test_Aircraft);
- end Test_My_Aircraft;
20 lines: No errors