Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Chapter 8
Sets and Records

8-1 Sets

If you would like to test a character to see if it is an uppercase letter, you may use the following condition:

    READ(Character);
    IF (Character >= 'A') AND (Character <= 'Z') THEN...

There is a simpler way to express the same condition. Take a look at this statement:

    IF Character IN ['A'..'Z'] THEN...

This expression speaks for itself; it is almost plain English. It says: “if the character is IN the set of uppercase letters [ 'A'.. 'Z'] then...”

In a similar way, you can express any set of items such as:

['a'..'z'] the set of lowercase letters
['A'..'Z', 'a'..'z'] the set of all letters
[0..9] the set of digits

The set is a structured data type that may include unordered elements (or members). You can express a set constant by listing its elements between brackets separated by commas. Unlike arrays, the order of elements in a set is not important. For example, the set [1,3,5,7], which represents the set of odd numbers between one and seven, is the same as the set [1,7,5,3]. This indicates another difference between sets and arrays. In arrays you can access any element by its position in the array, but with sets you cannot access individual elements. You can only test a data item to see if it is a member of the set using the IN operator. If the elements of a set form a continuous subrange, you may use the two periods (..); for example, the set [1,2,3,4,6,8] can be written as [1..4,6,8]. The elements of a set can be of any ordinal type, but all of the elements must be of the same type, which is called the base type.

8-2 Set Declaration and Assignment

You can declare a set variable using the keywords SET OF, as in this example where a set of the base type CHAR is declared:

    VAR
     LowerCase:SET OF CHAR;

After this declaration you can assign the variable “LowerCase” a set constant of the base type CHAR, for example:

    LowerCase:= ['a'..'z'];

You may then test a variable of the type CHAR for membership in this set using an expression like:

    IF Character IN LowerCase THEN...

Note that if you use the expression “IN [ 'a'..'z'] ” there is no need for declarations.

As with other structured types, it is preferable to declare sets in the TYPE section; you can then use this type in the VAR section to declare variables. The declaration takes the form:

    type-identifier = SET OF base-type;

Here is an example:

    TYPE
      Days = (Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday);
      Languages = (C,CPP,Pascal,Fortran,Basic,Cobol,Assembly);
      Digits = SET OF 0..9;
      Lowercase = SET OF 'a'..'z';
      Uppercase = SET OF 'A'..'Z';
      DaySet = SET OF Days;
      LanguageSet = SET OF Languages;
      CharacterSet = SET OF CHAR;
    VAR
      WholeWeek, WorkingDays, WeekEnd :DaySet;
      OddNum, EvenNum, Numbers       :Digits;
      Small             :Lowercase;
      Capital           :Uppercase;
      ProgCodes, HLL, LLL, MLL  :LanguageSet;
      Alphabet           :CharacterSet;

In these declarations, variables such as “Weekend,” “WorkingDays,” and “WholeWeek” are all sets of the base type “Days.” Any of these set variables may be assigned one or more elements of the enumeration “Days,” such as:

    WeekEnd:= [Saturday,Sunday];
    WorkingDays:= [Monday..Friday];
    WholeWeek:= [Monday..Sunday];

The last statement is equivalent to the statement:

    WholeWeek:= [Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday];

The value of a set variable is undefined until it is assigned a value. When you assign a set constant to a set variable, their base types must be compatible, i.e., they must be of the same type, subranges of the same type, or one of them must be a subrange of the other. Here are more assignments:

    OddNum:= [1,3,5,7,9];
    EvenNum:= [2,4,6,8];
    ProgCodes:= [C..Assembly];
    LLL:= [Assembly];

The empty set is a set with no members and is denoted by the constant []. You may assign this constant to any set variable of any base type, for example:

    OddNum:= [];

Rules and Restrictions

The following are the main rules and restrictions that control the use of sets:

1.  There is usually a limit on the maximum number of elements of a set. This limit varies with different Pascal implementations; in Turbo Pascal, for example, it is 255. The declaration “SET OF INTEGER ” is not allowed because the range of integers exceeds this maximum number, but you can get around that by using subranges such as “SET OF 0..99.”
In some implementations, the declaration “SET OF CHAR ” is not allowed either, in which case a subrange of the type CHAR might be used.
2.  You may assign a set to another set:
    NewSet:= OldSet;

In this case, “NewSet” is an exact copy of “OldSet.”
3.  You may declare an array of sets, as in:
    DaysArray = ARRAY[1..10] OF DaySet;

where “DaySet” is a previously declared set type.
4.  You cannot read or write a set using the input/output statements, but there are some programming techniques using set operations (explained in the next section) that may be used.
5.  Sets can be passed as parameters of subprograms, in which case they must be declared as types.

8-3 Set Operators and Operations

In addition to the membership operator “IN,” you can use the following operations with sets of compatible types:

1.  Union (+)
2.  Intersection (*)
3.  Difference (-)

Union

The union of two sets “S1” and “S2” is a set whose elements are in either “S1” or “S2,” or in both. The operator “+” is used for this operation, for example (using the previous declarations):

    Alphabet:= Small + Capital;

The set “Alphabet” will thus contain both the lowercase and the uppercase letters.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.