1 -- name : e. adams
  2 -- course: cs240
  3 -- due date: sept 5, 2007
  4 -- source: binary_search_test2.adb
  5 -- executable: binary_search_test2.exe
  6 -- professor: adams
  7 -- compiler: gnat ada
  8 
  9 -- Purpose:  this program performs a binary search on an ordered list
 10 --           of integers entered by the user
 11 -- Input: number of elements in the array less than or equal to 10
 12 --        elements in the array
 13 --        value to look for
 14 -- Output: the location where the value is found or a message saying
 15 --         value not in list
 16 
 17 ÏÏÏwith Ada.Text_IO;         --  use Ada.Text_IO;
 18 ÏÏÏwith Ada.Integer_Text_IO;  -- use Ada.Integer_Text_IO;
 19 ÏÏÏwith Bin_Search;           -- use Bin_Search;
 20 ÏÏÏwith ada.io_exceptions;
 21 ÏÞßàprocedure Binary_Search_Test2 is
 22 ÏÏ§
 23 ÏÏ§-- Test driver for setting up array of values and calling a search routine.
 24 ÏÏ§
 25 ÏÏ§ÏïÏsubtype List_Type is bin_search.Integer_Array (1..10);
 26 ÏÏ§
 27 ÏÏ§----------------------------------------------------------------------------
 28 ÏÏ§ÏÞßàprocedure Get_List (List  : out List_Type;
 29 ÏÏ§ÏÏ§Ï                Count : out Natural) is
 30 ÏÏ§ÏÏ§ÏíÏdummy : character;
 31 ÏÏ§ÏÏ§-- Ask tester for list of values to put in search array.
 32 ÏÏ§ÏÏ§begin
 33 ÏÏ§ÏÏ¨¹¹Ïada.text_io.Put_Line ("Enter number of test elements (0-10).");
 34 ÏÏ§ÏÏ¨¹¹ÏAda.Integer_Text_IO.Get (Count); -- if count not in range, exception will occur later
 35 ÏÏ§ÏÏ§
 36 ÏÏ§ÏÏ¨¹¹ÏAda.Text_IO.Put_Line ("Enter the test elements in order, one per line.");
 37 ÏÏ§ÏÏ§   
 38 ÏÏ§ÏÏ¨¹¹±for Index in 1..Count loop  -- use user's input to enter values in list 
 39 ÏÏ§ÏÏ§ÏÏ5                         -- error will occur here when count is not in range
 40 ÏÏ§ÏÏ§ÏÏ7¹¹´begin          
 41 ÏÏ§ÏÏ§ÏÏ5ÏÏ¨¹¹ÏAda.Integer_Text_IO.Get (List(Index));
 42 ÏÏ§ÏÏ§ÏÏ5ÏÏ§Ïðîìexception
 43 ÏÏ§ÏÏ§ÏÏ5ÏÏ§ÏÏ¨¹³´when ada.io_exceptions.data_error =>  -- character or float entered
 44 ÏÏ§ÏÏ§ÏÏ5ÏÏ§ÏÏ§Ï6¨¹¹Ïada.text_io.get (dummy);
 45 ÏÏ§ÏÏ§ÏÏ5ÏÏ§ÏÏ§Ï6¨¹¹Ïada.text_io.put ( " value '" );
 46 ÏÏ§ÏÏ§ÏÏ5ÏÏ§ÏÏ§Ï6¨¹¹Ïada.text_io.put (dummy);
 47 ÏÏ§ÏÏ§ÏÏ5ÏÏ§ÏÏ§Ï6¨¹¹Ïada.text_io.put_line ("' that you entered was not valid ");
 48 ÏÏ§ÏÏ§ÏÏ5ÏÏ§ÏÏ§Ï6¾¹¹Ïada.text_io.put (" please re-enter ");
 49 ÏÏ§ÏÏ§ÏÏ5ÏÏ§ÏÏ§Ï÷´when constraint_error =>
 50 ÏÏ§ÏÏ§ÏÏ5ÏÏ§ÏÏ§Ï6¨¹¹Ïada.text_io.put_line (" oops, too many elements "); 
 51 ÏÏ§ÏÏ§Â¹ÇÏÏ§ÏÏ©Ï¶¾¹¹Ïexit;
 52 ÏÏ§ÏÏ§ÏÏ5ÏÏ§
 53 ÏÏ§ÏÏ§ÏÏ5ÏÏ©end;
 54 ÏÏ§ÏÏ§ÏÏ°end loop;
 55 ÏÏ§ÏÏ§
 56 ÏÏ§ÏÏ©end Get_List;
 57 ÏÏ§----------------------------------------------------------------------
 58 ÏÏ§ÏÞßàprocedure Put_List  (List  : in List_Type;
 59 ÏÏ§ÏÏ§Ï                Count : in Natural) is
 60 ÏÏ§ÏÏ§                 
 61 ÏÏ§ÏÏ§begin
 62 ÏÏ§ÏÏ¨¹¹±for i in 1..count loop
 63 ÏÏ§ÏÏ§ÏÏ7¹¹Ïada.integer_text_io.put (list(i));
 64 ÏÏ§ÏÏ§ÏÏ°end loop;
 65 ÏÏ§ÏÏ¨¹¹Ïada.text_io.put_line ("  ");   
 66 ÏÏ§ÏÏ§Ïðîìexception
 67 ÏÏ§ÏÏ§ÏÏ¨¹³´when constraint_error =>
 68 ÏÏ§ÏÏ§ÏÏ§Ï6¨¹¹Ïada.text_io.new_line;
 69 ÏÏ§ÏÏ§ÏÏ©Ï¶¾¹¹Ïada.text_io.put_line ("  Hah! caught the error in put_list ");
 70 ÏÏ§ÏÏ©end put_list;                         
 71 ÏÏ§-------------------------------------------------------------------------------
 72 ÏÏ§ÏíÏNum_Elements : Natural;       -- number of elements to search
 73 ÏÏ§ÏíÏList         : List_Type;     -- list of elements to search
 74 ÏÏ§ÏíÏSearch_Value : Integer;       -- value to search for in List
 75 ÏÏ§ÏíÏLocation     : Natural;       -- index of Value in List
 76 ÏÏ§
 77 ÏÏ§begin
 78 ÏÏ¨¹¹ÏGet_List (List => List, Count => Num_Elements);
 79 ÏÏ¨¹¹ÏPut_List (List => List, Count => Num_elements); 
 80 ÏÏ¨¹¹ÏAda.Text_IO.Put ("Input value for which to search: ");
 81 ÏÏ¨¹¹ÏAda.Integer_Text_IO.Get (Search_Value);
 82 ÏÏ¨¹¹ÏAda.text_io.put (" Search Value is ");
 83 ÏÏ¨¹¹Ïada.integer_text_io.put(Search_Value);
 84 ÏÏ¨¹¹Ïada.text_io.new_line;         
 85 ÏÏ¨¹¹Ïada.text_io.put ("Num_Elements is: ");
 86 ÏÏ¨¹¹Ïada.integer_text_io.put (num_elements);
 87 ÏÏ¨¹¹Ïada.text_io.new_line;         
 88 ÏÏ¨¹¹Ïbin_search.Binary_Search (List     => List(1..Num_Elements),
 89 ÏÏ§ÏÏÏÏÏÏ         Value    => Search_Value,
 90 ÏÏ§ÏÏÏÏÏÏ         Location => Location);
 91 ÏÏ¨¹³´if Location /= 0 then
 92 ÏÏ§Ï6¨¹¹ÏAda.Text_IO.Put ("Search value found at location ");
 93 ÏÏ§Ï6¾¹¹ÏAda.Integer_Text_IO.Put (Item => Location, Width => 2);
 94 ÏÏ§Ïö´else
 95 ÏÏ§Ï¸¾¹¹ÏAda.Text_IO.Put ("Search value not found in list");
 96 ÏÏ§ÏÈÏend if;
 97 ÏÏ¨¹¹ÏAda.Text_IO.New_Line(2);
 98 ÏÏ§Ïðîìexception
 99 ÏÏ§ÏÏ¨¹³´when constraint_error =>
100 ÏÏ§ÏÏ§Ï6¨¹¹Ïada.text_io.new_line;
101 ÏÏ§ÏÏ§Ï6¾¹¹Ïada.text_io.put_line 
102 ÏÏ§ÏÏ©Ï¶ÏÏÏÏÏÏÏ(" Not all of your data got into the program. Search may be flawed  ");
103 ÏÏ§   
104 ÏÏ©end Binary_Search_Test2;
105