- Describe what is meant by an ordinal data type. Give an example of a data type which is ordinal, and an example of one which is not ordinal. Identify two situations in which ordinal data must be used.
Ordinal data types can be listed in order, and counted in order.
Integer, boolean, and char are the predefined ordinal types. Real and
string are not ordinal. Ordinals must be used as array subscripts and
in CASE statements
- Define a type, and create a variable of that type, suitable for holding values consisting of the months of the year (January, February, ..., December).
TYPE
MonthType = (January, February, March, April, May, June,
July, Augst, September, October, November, December);
VAR
Month : MonthType;
- Define a type and create a variable of that type suitable for holding values on the range -100 to 100, inclusive.
TYPE
SubrangeType = -100..100;
VAR
Index : SubrangeType;
- The data type
TextLine = STRING[80] has already been defined for a program. Write a short Pascal procedure (call it KillBlanks that has as input a variable of type TextLine. The procedure is to remove all blank spaces from the string, and return the altered string. For example, the string "I love to ski in winter" would be returned as "Ilovetoskiinwinter".
PROCEDURE KillBlanks (VAR dataline : textline);
VAR
spot : integer;
BEGIN
spot := pos(' ',dataline);
WHILE spot > 0 DO
BEGIN
delete (dataline,spot,1);
spot := pos(' ',dataline)
END
END;
- Write a complete Pascal program that reads in the lines of the text file
TestData.txt and prints them to the screen with all blank spaces removed. You may use the procedure KillBlanks from the previous question without re-writing it here.
PROGRAM test;
TYPE
textline = string[80]; {from previous question}
VAR
infile : text;
line : textline;
PROCEDURE KillBlanks {as above}
BEGIN
ASSIGN (infile,"TestData.txt");
RESET (infile);
WHILE NOT EOF(infile) DO
BEGIN
READLN(infile,line);
killblanks(line);
WRITELN(line)
END {while}
END.
- Write a procedure (similar to
KillBlanks) called BlankCount that counts the number of blank spaces in a line, but does not alter them. (Hint: the clever student will see how to answer this question by making just a few minor modifications to KillBlanks.)
PROCEDURE BlankCount (dataline : textline; {<- no VAR !!}
VAR count : integer);
VAR
spot : integer;
BEGIN
count := 0;
spot := pos(' ',dataline);
WHILE spot > 0 DO
BEGIN
delete (dataline,spot,1); {dataline is not VAR - no harm done!}
count := count + 1;
spot := pos(' ',dataline,)
END
END;
- For
NumList (a variable of type ARRAY [1..6] OF 5..12) and Count (a variable of type INTEGER, write a FOR-DO instruction to assign a value to NumList. Be sure you a) write just one instruction (no semicolons allowed); b) assign each element of NumList a value; and c) assign only values on the correct component range.
FOR Count := 1 TO 6 DO
NumList[Count] := Count + 5
- Write a Pascal function
ZeroCount. The function has value parameter List of type Numbers where Numbers = ARRAY [1..80] OF INTEGER. The function is to return the number of values in List that are equal to zero.
FUNCTION ZeroCount (List : Numbers) : INTEGER;
VAR
Index, Count : INTEGER;
BEGIN
Count := 0;
FOR Index := 1 to 80 DO
IF List[Index] = 0
THEN Count := Count + 1;
ZeroCount := Count
END;
- Rewrite your answer to problem 8 as a procedure, with the count stored in the variable parameter
Count. Call the procedure CountZeros.
PROCEDURE CountZeros (List : Numbers;
VAR Count : INTEGER );
VAR
Index : INTEGER;
BEGIN
Count := 0;
FOR Index := 1 to 80 DO
IF List[Index] = 0
THEN Count := Count + 1
END;
- Write a Pascal procedure called
Reverse which will reverse the integers in a list. The procedure has three parameters: InList and OutList are of type List = ARRAY [1..50] OF INTEGER and Count is of type INTEGER. Only OutList is a variable parameter. InList contains the original list and Count contains the number of valid entries in InList. Create the reversed list in OutList. Thus, if the entries in InList were 5, 7, 4, 100, and 20 (in that order), then the entries returned in Outlist by Reverse would be 20, 100, 4, 7 and 5 (in that order).
PROCEDURE Reverse (Inlist:List; VAR OutList:List; Count:INTEGER);
VAR
Index : INTEGER;
BEGIN
FOR Index := 1 TO Count DO
OutList[Count - Index + 1] := InList[Index]
END;