Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pascal

What are sets in Pascal?

Sheza Naveed

A set is a collection of elements that have the same type. The data type of sets can be defined in Pascal.

Defining sets

The following example shows the set being defined followed by variables of that set type being declared:

type
set-name = set of base type;
var
Months = (January, February, March, April, May, June, July, August, September, October, November, December);

Letters = set of char;

Alphabets = set of 'A' .. 'Z';

Set operations

The following operations can be performed on sets:

Operation Description
Union Gives a new set containing all the members of the sets on which union has been performed
Intersection Gives a new set containing only the common members of the sets on which intersection has been performed
Difference Gives a new set containing members that are not common to either set on which difference has been performed
Symmetric Difference Gives a new set containing those members of the sets not in the intersection but in either of the sets on which symmetric difference has been performed
Inclusion If all the elements of Set 1 are in Set 2 but not vice versa
In Checks if a certain element is present in a set or not

Set operators

The following table shows different set operators and their descriptions given the following:

S1 := [‘1’, ‘2’, ‘3’, ‘4’];

S2 := [‘3’, ‘4’, ‘5’, ‘6’, ‘7’];

Operator Description Example
+ Union S1 + S2 = [‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’]
* Intersection S1 * S2 = [‘3’, ‘4’]
- Difference S1 - S2 = [‘1’, '2,]
>< Symmetric Difference S1 >< S2 = [‘1’, ‘2’, ‘5’, ‘6’, ‘7’]
= Equality S1 = S2 gives the boolean value False
<> Non-equality S1 = S2 gives the boolean value True
<= Contains (if one set is a subset of the other) S1 = S2 gives the boolean value False
Include Includes an element in the set Include (S1, [‘5’]) will give a set [‘1’, ‘2’, ‘3’, ‘4’, ‘5’]
Exclude Excludes an element from the set Exclude (S1, [‘4’]) will give a set [‘1’, ‘2’, ‘3’]
In If a certain element is a member of a set or not [‘1’] in S1 gives the boolean value True

Example

The following example shows the usage of different operators in Pascal:

program setFurniture;
type  
furniture = (bed, side_table, dressing_table, dining_table, chair, cupboard);  
furnitures = set of furniture;  
 
procedure displayFurnitures(f : furnitures);  
const  
list : array [furniture] of String[14]  
  = ('bed', 'side_table', 'dressing_table', 'dining_table', 'chair', 'cupboard');  
var  
   fr : furniture;  
   s : String;  

begin  
   s:= ' ';  
   for fr := bed to cupboard do  
      if fr in f then  
      begin  
         if (s<>' ') then s := s +' , ';  
         s := s + list[fr];  
      end;   
      
    writeln('[',s,']');  
end;  
 
var  
   drawing_room, bed_room, f : furnitures;  
   isTrue : boolean;
 
begin  
   f := [bed, side_table, dressing_table, dining_table, chair, cupboard];
   displayFurnitures(f);
   
   drawing_room := [side_table, chair, dining_table];
   bed_room := [bed, side_table, dressing_table, cupboard];
   f := drawing_room + bed_room; {union}
   displayFurnitures(f);


   drawing_room := [side_table, chair, dining_table];
   bed_room := [bed, side_table, dressing_table, cupboard];
   f := drawing_room * bed_room; {intersection}
   displayFurnitures(f);
   
   drawing_room := [side_table, chair, dining_table];
   bed_room := [bed, side_table, dressing_table, cupboard];
   f := drawing_room - bed_room; {difference}
   displayFurnitures(f);
   
   
   drawing_room := [side_table, chair, dining_table];
   bed_room := [bed, side_table, dressing_table, cupboard];
   f := drawing_room - bed_room; {symmetric difference}
   displayFurnitures(f);

   
   
   isTrue := [bed, side_table, dressing_table, dining_table] = [dining_table, chair, cupboard]; {equality}
   writeln(isTrue);
   
   isTrue := [bed, side_table, dressing_table, dining_table] <= [dining_table, chair, cupboard]; {nonequality}
   writeln(isTrue);
   
  
end.

RELATED TAGS

pascal

CONTRIBUTOR

Sheza Naveed
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring