How to print a set in Pascal

2 Answers

0 votes
program PrintElementInSet;

type
  TMySet = set of 1..10; // Define a set with elements from 1 to 10

var
  MySet: TMySet;
  i : Integer;

begin
  MySet := []; // Initialize the set as empty
  Include(MySet, 1); // Insert the element 1 into the set
  Include(MySet, 2); // Insert the element 2 into the set
  Include(MySet, 5); // Insert the element 5 into the set
  Include(MySet, 6); // Insert the element 6 into the set

  for i := Low(TMySet) to High(TMySet) do
    if i in MySet then
      WriteLn('Set contains: ', i);
end.




(*
run:
  
Set contains: 1
Set contains: 2
Set contains: 5
Set contains: 6

*)

 



answered Aug 5, 2025 by avibootz
0 votes
program PrintElementInSet;

type
  TColor = (Red, Green, Blue, Yellow);
  TColorSet = set of TColor;

procedure PrintColorSet(const ASet: TColorSet);
const
  ColorNames: array[TColor] of String = ('Red', 'Green', 'Blue', 'Yellow');
var
  color: TColor;
  FirstElement: Boolean;
begin
  Write('[');
  FirstElement := True;
  for color := Low(TColor) to High(TColor) do
  begin
    if color in ASet then
    begin
      if not FirstElement then
        Write(', ');
      Write(ColorNames[color]);
      FirstElement := False;
    end;
  end;
  Writeln(']');
end;

var
  MyColors: TColorSet;

begin
  MyColors := [Red, Green];
  PrintColorSet(MyColors); 

  MyColors := [Green, Blue, Red];
  PrintColorSet(MyColors); 

  MyColors := []; // Empty set
  PrintColorSet(MyColors); 
end.



(*
run:
  
[Red, Green]
[Red, Green, Blue]
[]

*)

 



answered Aug 5, 2025 by avibootz
...