Docsity

Ada Package for Creating & Manipulating 3x3 Integer Array: Row & Column Order, Bubble Sort, Exercises of Aeronautical Engineering

Toggle sidebar
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

Partial preview of the text

Download Ada Package for Creating & Manipulating 3x3 Integer Array: Row & Column Order, Bubble Sort and more Exercises Aeronautical 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

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

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

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