Is there a better way to program this,

Viewed 162
with Ada.Text_IO;                  use Ada.Text_IO;
with Ada.Integer_Text_IO;          use Ada.Integer_Text_IO;
with Ada.Float_Text_IO;            use Ada.Float_Text_IO;
with Ada.Numerics.Float_Random;
with Ada.Numerics.Discrete_Random;

procedure Probability is
   
   subtype My_Characters is
     Character range 'a' .. 'z';
      
   package My_Random_Character_Package is
     new Ada.Numerics.Discrete_Random(My_Characters);
   
   use My_Random_Character_Package;
   
   Gen_1 : My_Random_Character_Package.Generator;




procedure My_Character_Program(First_Character, Second_Character : in Character) is
      
   begin
      
      Put(First_Character);
      Put(" ");
      Put(Second_Character);
      New_Line;
      Put("All characters between ");
      Put(First_Character);
      Put(" and ");
      Put(Second_Character);
      Put(":");
      for C in Character range First_Character .. Second_Character loop
         Put(" ");
     Put(A);
      end loop;
      
   end My_Character_Program;
   
procedure Part_2 is 
      
      First_Character, Second_Character : Character;
      
   begin
      
      Put_Line("PART 2:");
      Put_Line("Two random characters from a to z will now be generated.");
      Put("Random characters: ");
      First_Character  := Random(Gen_1);
      Second_Character := Random(Gen_1);
      
      if First_Character > Second_Character then
     My_Character_Program(Second_Character, First_Character);
      else
     My_Character_Program(First_Character, Second_Character);
      end if;
      
   end Part_2;   


   
begin   
   
   --  Reset(Gen_1);
   Part_2; 
   New_Line(2);


end Probability;

The following program will pick two random letters between A and Z and then it will type out all the letters thats between them. So for instance if it randomly generates the letters

d and g

it will type out

d e f g

But I need your asstistance. Even if my program works I don't think its executed that well, especially the part 2. Is there not a way where I can put a part of my "Part 2 procedure" in my "My character program procedure"? As you can see I swaped the first and second character. Can I not do that in another way?

I appreciate any help that I could get.

3 Answers

Look at your 'toolbox' -- Ada provides exactly what you want, in the form of subtype and the Succ/Pred attributes, which you already used.

procedure My_Character_Program(First, Second : in Character) is
  Subtype Middle is Character range
     Character'Succ(First)..Character'Pred(Second);
begin
  Put_Line( First & ' ' & Second );

  Put("All characters between " & First & " and " & Second & ':');
  for C in Middle loop
     Put(' ' & C);
  end loop;
  Put_Line( "." );
end My_Character_Program;

As for Part_2, allow me to suggest that a much better method is to use inline declare-blocks and attributes/renaming.

--...
First_Character  := Random(Gen_1);
Second_Character := Random(Gen_1);
 
Declare
  First  : Character renames Character'Min(First_Character,Second_Character);
  Second : Character renames Character'Max(First_Character,Second_Character);
Begin
  My_Character_Program(First, Second);
End;

I’d be inclined to try a bit of recursion (and reduce the number of calls to Put - sorry, style issue, not your question I know):

   procedure My_Character_Program
     (First_Character, Second_Character : in My_Characters)
   is
   begin
      if First_Character > Second_Character then
         My_Character_Program (Second_Character, First_Character);
      else
         begin
            Put_Line (First_Character & " " & Second_Character);
            Put("All characters between "
                  & First_Character
                  & " and "
                  & Second_Character
                  & ":");
            for C in My_Characters range First_Character .. Second_Character
            loop
               Put(" " & C);
            end loop;
            New_Line;
         end;
      end if;
   end My_Character_Program;

An alternate approach is:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
procedure Main is
   subtype letters is Character range 'a' .. 'z';
   package rand_letter is new Ada.Numerics.Discrete_Random (letters);
   use rand_letter;

   seed : Generator;

   procedure print_range (First : letters; Last : letters) is
   begin
      for C in Character'Min (First, Last) .. Character'Max (First, Last) loop
         Put (C);
      end loop;
      New_Line;
   end print_range;

begin
   Reset (seed);
   for I in 1 .. 7 loop
      print_range (Random (seed), Random (seed));
   end loop;
end Main;
Related