Set Procedures

The procedures include and exclude add and remove, respectively, a value from a set. In the next program the include function is used to add a randomly chosen number between 1 and 49 to the set of chosen numbers. The first for loop ensures that six different values are added to the set and the second for loop outputs the lottery numbers in ascending order.

program Lottery;
  {$APPTYPE CONSOLE}
uses
  SysUtils;
type
  TAllowed = set of 1..49;
var
  ChosenSet : TAllowed;
  Chosen, Count : integer;
begin
  randomize;
  ChosenSet := [];
  for Count := 1 to 6 do
    begin
      repeat
        chosen := random(49) + 1;
      until not (Chosen in ChosenSet);
      include(ChosenSet, Chosen);
    end;
  for Count := 1 to 49 do
    begin
      if Count in ChosenSet then
        write(Count, ' ');
    end;
  readln; 
end.          

Programming - a skill for life!

Declaration, assignment, operators and functions of sets