Strict Standards: Declaration of action_plugin_importoldchangelog::register() should be compatible with DokuWiki_Action_Plugin::register($controller) in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/lib/plugins/importoldchangelog/action.php on line 8

Strict Standards: Declaration of action_plugin_blog::register() should be compatible with DokuWiki_Action_Plugin::register($controller) in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/lib/plugins/blog/action.php on line 154

Strict Standards: Declaration of action_plugin_include::register() should be compatible with DokuWiki_Action_Plugin::register($controller) in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/lib/plugins/include/action.php on line 140

Strict Standards: Declaration of action_plugin_importoldindex::register() should be compatible with DokuWiki_Action_Plugin::register($controller) in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/lib/plugins/importoldindex/action.php on line 57

Strict Standards: Declaration of action_plugin_discussion::register() should be compatible with DokuWiki_Action_Plugin::register($controller) in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/lib/plugins/discussion/action.php on line 955

Deprecated: Assigning the return value of new by reference is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parserutils.php on line 205

Deprecated: Assigning the return value of new by reference is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parserutils.php on line 208

Deprecated: Assigning the return value of new by reference is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parserutils.php on line 389

Deprecated: Assigning the return value of new by reference is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/parserutils.php on line 530

Strict Standards: Declaration of cache_instructions::retrieveCache() should be compatible with cache::retrieveCache($clean = true) in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/cache.php on line 291

Deprecated: Function split() is deprecated in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/inc/auth.php on line 154

Strict Standards: Only variables should be passed by reference in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/doku.php on line 71
nex code:ada:dsa-ex0
 

DSA Warm-Up Exercise 2003

This was the warm-up exercise for my Data Structure and Algorithms course in second year at the University of Glasgow. It is a short program written in the programming language Ada. It's not particularly complicated, interesting or impressive, but I got 10/10 for it. I don't see much Ada code on the net to be honest, so a budding young programmer from the American Military might find it useful :-P (that was a little Ada in-joke there).

Description

This program takes in an integer followed by a list of names, equal in number to the value of the integer. The names are then sorted in ascending alphabetical order and displayed to standard output.

The Source Code

You can download the source here or view it below.

----------------------------------------------------------------------------
----------------------------------------------------------------------------
--                                                                        --
--     DSA 2 Warm-up exercise (ex0)                                       --
--     Program Title : NameSorter Version 1.0.0                           --
--     Author : nex                                                       --
--     Group : L                                                          --
--     Matriculation Number : ***REMOVED FROM ONLINE VERSION***           --
--     Date Last Modified : 20th October 2003                             --
--                                                                        --
--     Description : This program takes in an integer followed by a list  --
--                   of names, equal in number to the value of the        --
--                   integer. The names are then sorted in ascending      --
--                   alphabetical order and displayed to standard output. --
--                                                                        --
--     Assumptions : Number of names < 100,                               --
--                   Name length < 21,                                    --
--                   All User input is valid and correct.                 --
--                                                                        --
----------------------------------------------------------------------------
----------------------------------------------------------------------------
 
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
 
procedure Ex0 is
 
   -- Constants
   String_Length : constant Integer   := 20;
   Array_Length  : constant Integer   := 99;
   -- Tester is used to find the length of names
   Tester        : constant Character := ' ';
 
   -- Types
   subtype Nametype is String (1..String_Length);
 
   type Person_Type is record
      First_Name : Nametype;
      Last_Name  : Nametype;
      -- The length variable is required when assigning the value of Last_Name.
      Length     : Integer;
   end record;   
 
   type Full_Names is array (1..(Array_Length + 1)) of Person_Type;
 
   -- Variables   
   Name_List_Length : Integer;
   Names            : Full_Names;
 
 
----------------------------------------------------------------------------  
   -- Clear_Data 
   -- is used to make sure that there is no data stored in the
   -- Names arrays before the program puts its own data in them.
 
   procedure Clear_Data (Names         : in out Full_Names;
                         Array_Length  : in     Integer;
                         String_Length : in     Integer    ) is
 
   begin
 
      -- Clear variables of inherited data
      for H in 1..(Array_Length + 1) loop
         for I in 1..String_Length loop
            Names(H).First_Name(I) := ' ';
            Names(H).Last_Name(I)  := ' ';
         end loop;
         Names(H).Length := 0;
      end loop;
   end Clear_Data;
----------------------------------------------------------------------------
 
 
 
----------------------------------------------------------------------------
   -- Read_Names
   -- is used to read in the names that are to be sorted and then
   -- assign the First_Name and Last_Name values to the correct variables.
 
   procedure Read_Names (Name_List_Length :    out Integer;
                         Names            : in out Full_Names;
                         Tester           : in     Character  ) is
 
   -- Counter is a local variable required for loops
   Counter : Integer;        
 
   begin
 
      -- Get number of names
      Get (Item => Name_List_Length);
      Skip_Line;
 
      --Read in names
      for I in 1..Name_List_Length loop
         Get_Line (Item => Names(I).First_Name,
                   Last => Names(I).Length     );
 
         -- Find end of First_Name and start of Last_Name
         Counter := 1;
         while Names(I).First_Name(Counter) /= Tester loop
            Counter := Counter + 1;
         end loop;
 
         while Names(I).First_Name(Counter) = Tester loop
            Counter := Counter + 1;
         end loop;
 
 
         -- Assign values to last names
         for H in Counter..Names(I).Length loop
            Names(I).Last_Name(H - (Counter - 1)) := Names(I).First_Name(H);
            Names(I).First_Name(H) := ' ';
         end loop;
 
      end loop;
 
   end Read_Names;
----------------------------------------------------------------------------
 
 
 
----------------------------------------------------------------------------
   -- Sort_Names
   -- is used to re-arrange the records in the Full_Names array
   -- such that the order in which they appear in the array is ascending
   -- alphabetical order.
 
   procedure Sort_Names (Name_List_Length : in     Integer;
                         Names            : in out Full_Names;
                         Array_Length     : in     Integer    ) is
 
   begin
 
      for I in 1..Name_List_Length loop
         for H in 1..Name_List_Length loop
            if Names(H).Last_Name > Names(I).Last_Name then
 
               -- Names(Array_Length + 1) should always be unused by input
               -- as Number of names is defined as equal to Array_Length, 
               -- thus it can be used as a temporary store.
               Names(Array_Length + 1) := Names(I);
               Names(I) := Names(H);
               Names(H) := Names(Array_Length + 1);
            end if;
         end loop;
      end loop;
 
   end Sort_Names;
----------------------------------------------------------------------------
 
 
 
----------------------------------------------------------------------------
   -- Display_List
   -- displays the list of names in the Full_Names array.
   -- It also takes out unnecessary 'blanks' so that the output is neater.
 
   procedure Display_List (Name_List_Length : in     Integer;
                           Names            : in     Full_Names;
                           Tester           : in     Character  ) is
 
   -- Counter is a local variable required for loops
   Counter : Integer;
 
   begin
 
      -- Output to the screen
      for I in 1..Name_List_Length loop
 
         -- Find end of First_Name to make output neater
         Counter := 1;
         while Names(I).First_Name(Counter) /= Tester loop
            Counter := Counter + 1;
         end loop;
 
         for H in 1..Counter loop
            Put (Item => Names(I).First_Name(H));
         end loop;
 
         -- Last_Name already neat, so just output
         Put (Item => Names(I).Last_Name);
         New_Line;
      end loop;
 
 
   end Display_List;
----------------------------------------------------------------------------
 
 
-- Start of main program execution --
begin
 
 
   -- Clear arrays of any un-erased data
   Clear_Data   (Names         => Names,
                 Array_Length  => Array_Length,
                 String_Length => String_Length);
 
   -- Read the list of names from standard input and store in the 'Names' variable
   Read_Names   (Name_List_Length => Name_List_Length,
                 Names            => Names,
                 Tester           => Tester           );
 
   -- Sort the names in the 'Names' variable in ascending alphabetical order
   Sort_Names   (Name_List_Length => Name_List_Length,
                 Names            => Names,
                 Array_Length     => Array_Length     );
 
   -- Display to standard output the contents of the 'Names' variable
   Display_List (Name_List_Length => Name_List_Length,
                 Names            => Names,
                 Tester           => Tester           );
 
end Ex0;

Example of Use

Input (each end of line is an 'enter'):

3
Alex Stevens
Fergus Urquhart
Lexie Hart

Output:

Lexie Hart
Alex Stevens
Fergus Urquhart

Assumptions

Number of names < 100,
Name length < 21,
All User input is valid and correct.

And for those of you using windows who want to see it run and just don't know what to do, here is a windows executable: ex0.exe

Nexami Engeo 2007/07/05 17:12


Strict Standards: Only variables should be passed by reference in /freeola/users/0/0/sr0193000/htdocs/dokuwiki/doku.php on line 79