0% found this document useful (0 votes)
58 views60 pages

Coi 4march2016

This document discusses set data types in the Turbo Pascal programming language. It defines a set as a collection of distinct elements of a given type, such as characters, integers, or other sets. Sets can be defined using enumerated values or ranges, and operators exist for testing set membership, union, intersection, and other operations. Sample code is provided to demonstrate declaring and initializing different sets of characters.

Uploaded by

Rupankár Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views60 pages

Coi 4march2016

This document discusses set data types in the Turbo Pascal programming language. It defines a set as a collection of distinct elements of a given type, such as characters, integers, or other sets. Sets can be defined using enumerated values or ranges, and operators exist for testing set membership, union, intersection, and other operations. Sample code is provided to demonstrate declaring and initializing different sets of characters.

Uploaded by

Rupankár Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

nrriiinui : Text Editor

12.5 hijawaia~?uuodnif
L

hhunruadid~
~ j $ ~~od~ I u u n od~8 ~ u u ~ i ( ~ d a ~ ~ u
HI i~ul ~ n
nvu~~: ~
z%?u~df~~nw
dddf:u?a WRQ y a ~ n n u(text data) k/Ut/ 6~dm~?Rw
adi (word processor) 63
unoll^rmIu'on?iu(text editors) ~ ~ ~ r d i u d 3 ~ q n ~ ~ ~ u ? a w a U ' ~(business
yRn14~fio
data-processing applications)
Turbo Pascal ~ ~ H I L L ~ u % ~ R ~ ~ R ~(dynamic
~ ~ ' ~ string
I I ~ :data
w R ? ~
t:
d ~ ' l ~ ~ . r ~ l + ' ~ d iY~~cdfmuknmz
type) nun~I;IJrlauunrnf:uat;~iu~niu~a adi~i~n'i~
(string variables) lu<?$€l9.7 ~ 6 ~ l u r ~ 7 ~ u ~ % ~ a ~ ~ R mTurbo
u ~ n Pascal
uf=uoJ
lduil? ucl:~~uu~Ciuuriun'u~~n~c~i~uuo~kao'nu~:
~ u u n 6 ~ m a :dos~i u ~unGw
~u
3 o y a ~so'nu.r:uad
i Turbo Pascal uarniliiritrmirk~Cu
&udfmuo'nu.r:adid
n.ruii?u
.s R t u ~ ~Y~ rdu o n ~ c i a s n a i u ~ ~ ~ = : q n a " a ~ ~
2'
87nr"ydffram rrdanrru conten Kd\ignCuiu (undefined) n7~didiurkran
d
rn (set assignment) rdw nrsI61J
Syntax Display
ninJrzniclr.aQmaa
r

Forrm : type
Set type = set of base type;

type
Letterset = set of 'A' .. 'Z';
iin3~u~uluiYd
: lauuii[v(~oofset type tnCsiuiiriih~
7 &~~ualu
base type ~i l r d d ~ ~ r ~ n d ~ n ? ~ set
r i l type
u % ~u78fid
44
~ a r~mad~~l%nvodu'u~n
ri%nolnfildndq'lubase type rra: base type ~ ~ ~ L U U ~ G R L ~ ~ (ordinal
O I U R Ltype)
U~!
d
q 1%base
u
hk~nn
nisdilfltfinwamul~~du'a47nan47u3u11adciiei
type vadr'lm Turbo Pascal d l ~ ~ 1 r a v ~ ~ w u 6 u o d d 7 ~ i ~ d y n Char (256)
q lurruuahu'o
L I D U ~ ~ ~ set
L of ~ ~ rh%finraa
~ char Z ~ ~ R
u 2'
rndadlnau rnlddmuimldluu~fiau'a~a
Integer L ~ base
N type brd
da
~ ~ U 1 1 d # f i ~ ~ integer
I
~ ~ ~n u~m~k di u%
~ nif256
!h~ dl
n i ~ r i ~ ~ w c l d ~ r a n u a (Set
r ~ ~Assignment
w ~ d ~ a and
~ Set ~lteials)
qb ~ h r a a
~onnuR-"Jfi~~uahraalriciid7d
$0~1318J&d

Odds := [ I , 3, 7, 91;
Evens := [O, 2, 4, 6, 81
rl~~unfiidic
q lfl7?~$?~~dTi~mIadfi
ddd~:ni&.iad~d 12.1
i ~ ~ l i ~i ~ u
~ n ~ i u k r l ~ ~ u n f i i r r d a = : 4(assigns) wn~ J r a a l f l C u C a ~ ~ d ~ r a ~ ~

IT 257 57 1
: WJOj
(1uaw6u!ssv )as) lsai~yunuLysLu
(suo!ssa~dxaalq!$edwm-d4) ~ $ ~ n $ n u i i p t nanlenxew
a ~ ~ i -MI anleAu!w on1F
.)
(anlewewanle~u!wnpir ~ p o ~oopo~$~ruh;~yunu~ya~on,u~y~yug~r01ron
( I ~ J ~$as)
H I usipahth+
Aelds!~xequAg
(slayae~qu! pasopua sanleA 40 p ! ~
-2..
rcase letters) inaiuvluiumu mattd5
rflu set expression
r 1
w vl.ro~?ud~i.a@8n~avlit~
"oni~~Won
A
Mud set expression O183:ynI
di~fiwnif
~'llm (set operators)
base type UOJ set var LrR: set expression ira~~ilunSml$bnun"ul6
LLRE

l u expression irada$u base type UOJset var


~ u l % n ? n h set
~ a ~ i i ~ u a n o n n n (Empty
L L b Set and Universal Set)

set is a set having zero elements denoted by [ I)


nu
nir&iaamii~~.~6a~l$ni3fiivlwmii
Middle := [ ]
unnvii~ain~.nm~ai~nfioiu
~am?id (undefined set)

(Undefined set is a set variable that is declared but it not assigned any values.)
t:
~ o n n n L G n hvluierfh ~amidd3:nouci?oriin~vlue1lw
base type h ~ i u
d
% ~ ~ L ~ ~ L ~ W I : M M(Universal
~ L ~ Q I set b a set containing all possible values of a set.)
t:
&oriiJ~.duLWmfim DigiSet aid base type L~u6%&Jdou
u
0..9 nJuuLannw
Guw'na'a~~~nw6aer
[0..9] n i ~ f i i ~ u m i i
Mixed := [0..9]
fiuiuK2uds~amMixed Li)w~onnw;ufia'
doan&d~31~$ufiw1.~sn'a~~d~~gm~ilw~~~\ii~
visoiannwhGnf riau\%~an&
mrmKud~aGmu~u
(Sets with Enumerated Type Values)
,,
1I
LSZ LI ' tLS
12.2 &a41 bikmirbaol (Set Operators)
"a
n i ~ f i i runi~srlaiooh
f ai'~nf:t+!iuurmh$aqau rsla5uioKadi'1r i i u n i ~
kJM&% !o in u a e u ~ ~ ~ ~ ~ i i u ~ i r : ~ ~ ~ a d i ~ ~ u n{u~qnodi~lf
ifn"(~u~~oiu~
~ ~ ~ ~ ~ Q U ~ I M ~ U ~ ~ ? ~(Testing I ~Set
: ~ ~ Ufor ~ YMembership)
E ~ J I P I B I

~ m ~ ~ i ? u d i ~ w i z 6(g;J
~ s r l9.18)
i ~ A o n n a ~ i i c i i r a w(a~particular
r value)
o ~ ~ u u n a ~ i 6 u n 5 oaa:riio$
ld ~ I L L M ~ ~ I O J ~ ~

Pascal $nsrlik A i r ~ n i ~ m a : ~ u i ~Jn vLoln'nri


~ in r~onsaogiid~rawi:

~urilu~uitinvo~rs~~ioli
#Ad u
iiwoununadl tfunir in a:n~uiuduriiuuuya m m u i s n ir u n in
~ura~rrnuda:~iludouI111~frnou
(compound condition)
Riadidrdu ~ m r ~ u u t i o u ~ ~
(Ch = '.') or (Ch = '7') or ( ~ =hI:') or (Ch = I!') ,

M % I ~ ' ~ W ~ ~ srl%ild
~ ~ U W ~ M ~

~ m um1 ~ n ~ ~ ~ q n m U q m 6 j ~ r u1unift+!i6~if
~~~~lun'ni~~3
type
CharSet = set of Char; {set type of Char elements}'

. var
Vowels, Uppercase, lowerwse : CharSet; (3 sets}
NextChar : Char; {a data character}

begin
Vowels := ['A', 'a', 'E', 'e', 'I' 'is,'O', 'o', 'U', 'us,];
I

Uppercase := ['A'..'Z']; I-

Lowercase := ['a'..'zl]
Bd+i~di~fqihoun~znLJ
NextChar dunj6'nw~u'onmu& if uan~Co
d ,?Y
n?iuriln?rhNextChar ~ J Y ~ Q ?content
~ U BOJ$~

repeat
Write ('Enter a letter> I);

ReadLn (NextChar);

{Display message about NextChar.)


if NextChar in Vowdls then
WriteLn (NextChar, ' is a vowel')

a else if NextChar in Uppercase then


WriteLn (NextChar, ' is an uppercase consonant')
i

else if NextChar in Lowercase then


WriteLn (NextChar, ' is an lowercase consonant')
else
WriteLn (NextChar, ' is not a letter')
until NextChar in ['A'..'Z', 'al..'z']
~dm.rzi~~~nci~u~iiu~~uQwodda::n~~6~~
~~i?h~G.unia
~ni?c~ui%nnm *

k u ~ i i u ~ ~ u u o ~ dinti1
o u l?lo
u
not (NextChar in ['A'..'Z', 'a1..'z']) {not a letter}
y'niinwnini?u?lonia~~suhird~1~6uf~0tb
NextChar not in 'A'..'Z', 'a1..'z'] {invalid expression)
Syntax Display
~ ? s l " l ~ ~ ~ n l ~ l ? t R in
~ l(Set
~ nMembership
Y o J ~ ~ ~ Operator
~ in)

Form : element in [list-of-elements]


~ a h r G u n ~ s n i a : ~ u ~ Qinn o~ ok d~ ~l l~u~~ o u l u d ~ ~ a t dTrue
ww~~~w
tdo element o$u list-of-elements nsflfu7 d o ~ b d 3 z L ~ U + w s LFalse
~w LLUU%&

4 ' o y a . u ~element
~ 6o~la'~~nuh~&n'u~u13n111~
n i h ~ i r u n q sin finisrlviou
(precedence) ~ ~ v i l ~ I J ~ d 7 ~ f i ~ n(relational
7~~~M' operators)
ud
dqwsqu dqwiqu u~rund~aaoacam
(Set Union, intersection. and
Difference)
~ m ~ 1 ~ ~ n w ~ o J t . a a ~ u ~ n ~ c 1ornswi7n~d7rirunqsn"d
fim~~~mi~~~Ld~
mua~nd
~ ~ n s z r i i i a u n i r u w t :r n
uawrau, hwjau ~la:w~d7d
a'u
nq.i47r(iun~s~arj,u p i i ~ ~~m
a ~ f i ~ ~ ~ u n ~ s r t o ~ ~ ~ n ~ A o I Pascal u~~~~~AmuI
h ~ u ~ n ~ s o l ~ r ~ w n i ~ a d i ~ a u ~ a+ (set
' ~ ~union).
d i i *~(set
~ w intersection),
ni~ UR:

- (set difference)
d-mma (union) uodRodram (~afi7rfun~sta.+) ~ u 7 e fLl ~~ R Y D J ~ ~ Q ~ ~ J

11, 31 + [2, 41 60 [I, 2, 3,41


['A', 'C', 'F'] + ['B', 'C''D', IF'] # Q ['A', 'B' 'C', 'Dl, IF']
'F'] + ['A', 'C', 'Dl 'F'J fin ['A, 'C', 'D', 'F']
['A', 'C', -
gaUjau (intersection) ? I O J R Q J L ~(6afi7
~ L C ~ U ~ ~ ~+)
L IMUXI~~JLII~Y
I~ QJ

~ u 7 8 n h f iod~ I w $ d w o ~ ~ g m
6'3ad74 L ~ M

['A', 'C',
'F'] + ['A', 'C', 'Dl, 'F'] %I ['A', 'C','Fs]
(-6u!ueew auo ueql aJow Jo~eJadoue 6u!~!6s! 6u!peope~o~oyedo)nLnllroLLurnll
, P
L ~ L
a ~ H ~
LPUUL~PPL~P~)#LLUP~~I m
r y ~n L~ U~L Ll Ul P ~ $ ~ L ~ L ~ P Q R P ~ ~ ~ ~ L L U
t~un5it.ycyrerLngi2rLu
qunLinnLno;i nLnunLturpmL&uuLnoLrrHnL~unLurpHrLun~iLyLuogi
v n
'-X -
rfiingincrinr~uvy~y ubc.op ubinruLunr)iLprLu
.
l r u r ~~a 6uen~ uO~1~~leau
~ n u ~ ~ ~ ~ u n u tu::un H~ yu $ ~ n g i n ~ r o ~ ~ u n $ i ~ ~ u B L I )
.
l
2
QplWL%L~rLUn$lLyrLUU91HL~ 2U11 - .'+ rLU9l51Lp+ 'ImSBd pbM57l\
(*spueredosl! lo JapJo aqj p ssa(pJe60~jlneseJ ewes eql
I R H ~ H G ~ U M ~ I U ~ JL ~ ~ ~ J J O R N I Q ~ M & A ~ O ~ ~(Unit
M ~set
J Lis~a' U ~ ~ I ~ ~
set containing a single element enclosed in brackets.)
M G ~U LJ ~~ Q R ~ ~ R I ~ ~ ~ ~ ~ I D J ~ I ~ ~ ~ ~ ~ ~J H
J JL~ U
~ U ~ I ~ ~ J K
Q Y ~ ~ ~ I J Lf ~i wW d
[I. 3.4. 5) + 2 { K ~ ~ ~ ~ I L ~ u ~ ~ ~ ~ ~ I O J \ S ~ ~ J L ~ B ~ }
Avis + Cadillac { f i t J~J'I~L~~}
ndi~fivn~.iKad~~~l
C
rwnri1ii6a~nh~~unimd~~~~~~~a~1
ntaacqn\ri?n6ac (invalid) u~rni
~ ~ ~ d q~nni o~l h6 ~12.2
Q n d i ~ ~ ~ n i 3 ~ n n i ~ &(constant) Cwxd
a L~WL$U?<M
u d d
[Avis] + [Cadillac] { . j ~ n d l i i i U n l 3 s l 7 n ~ ~ ~ \ r i ~ n ~ a ~ 1 ~
Avis \ r j d i ~ l l u C ~ rGw n r i i Avis ~ 3 %
\dgn&~~wnriia~~8un'iu~"JnA'au

&cii~iiMni.raiu~~6"iJw~%m
(Set Relational Operators)
Gadir~uni.rrirrw'ud=, -, c= UR; >= I ~ L ~ ~ L I U L R ~ U LLamnoJp
~~ROJ?~
~ ~ ~ i u i ~ ~ u u ~ ~ i u ~ (base 6 a ~L M
n ' u type) d Bn O~UmW~ R~~A ~~Y ~ V D J ~ I ~ ~ L ( ~ U Y
~ C i u u W a C i i ~ ~(Boolean
u u ~ ~ value)
nid7~icuni.r= LLR: <> n ~ ~ D ~ ~ l ~ ~ ~ l ~ ~ ~ ~ i j R ~ l % n ~ ~ ~ ~
69o L~IJ L ~ M

[ I .3] = I1.31 60 Ture


[ I ,3] = [2,4] 6 9 False
[ I ,3] - 11.31 ffa False
[ I ,3] <> [2,4] # a True
[1,3] = [3,1] fin True [1,3] <> [3,1] ffa False
[ ] = [I] fin False [ ] <> [I] True
o r ~ ~ u i i u s . m ~ ~ r ~ ~ ~ u ~ ~ ~ ~ ~ 6 1 nrrui%nlu~an\aJ
'1~1~iunis~~u~~
lhniy ([I .3] UR: [3,1] ~~nuran~iioqn'u)
olii~\3fimiuIJnG ~ U ~ I ~ ~ ~ I ~ ~ V O J L ~ ~ Q ~ L ~~ uL w
I Jt ~
~~I Kn U
" oL ~GnJ~ ~ t l \ d
Miuin (increasing ordinal sequence)
@?adiLfi~ni~t?uw'Md<= LLW:: >= ~ i ~ ~ ~ n ~ i a r ~ ~ a i ~ ~ u(subset)
w'~d~amtiou
uaqrdof~m(superset) &d
syngi Is'cl =< [ I
rtbnQ1 [ I =< Is'11
rr,ngr 1s'el =* IP'E'z' 11
rkbngi , [E' el =< 1s'11
I agrn~i[tj1s'z111 =< [E'LI
, : v PO&
w ~ r=<
uauwngi a r o ne t ~ u ~ u g (a i 1
n ~v)~0 u ~ i r s ~ u . i j o p % nv~ usr.
1 P$b%Ql [c'c] => [ 1
bkingi I --* [€'el
b#lftQl [E' 11 => [P'E'z'
11
r51~g1 [E'LI => [E'J1
< ? O ~ I J 12.4 ~1767Utidgl(Reading Sets)
nszuauolu ReadSet I%@12.1 h u ~ ~ o l h z l u n d ~ a u &. a(period)
n ua:'Iri
sll u u u hW uw%@
q Y ooKnw~uci~miZw~w
~ o ~ ~ n u 6 ~ u w~ lnsol i~etters
fi air^ lam
CahrSet)

Letters := ( 1; {initialize letters.}


~ % J B ~ ~Letters
' I M 'LL~~U~L ' I I. ~J L?A~Z ~ ~ O C ~if? ~ U & ~ I

if NextChar in ['A'..T]
then
Letters := Letters + [NextChar]; {Insert a letter.)
Ilk~5&fi%J$i!iVI@BI'a'IVIdL L E~~K~IULI~VI
~ Letters
I

procedure ReadSet (var Letters {output} : Charset);

{
Reads a sentence terminated by a period and stores the uppercase form
of each letter in Letters.
Pre : None.
Post : RBturns through Letters all the letters read before the period.

Sentinel = I.', {sentinel character}


var
NextChar : Char; {next input chamcter}

begin {ReadSet}
Letter := [ 1; {initialize Letters.}
Writetn ('Enter a sentence ending with symbol ', Sentinel); I

Read (NextChar);
while NextChar o Sentinel do
{invariant :
No prior value of NextChar is the sentinel and'letters contains each
dppercase letter read so far.

1
begin
NextChar := Upcase(NextChar); {Convert to uppercase.)
if NextChar in ['A'..'Z1] then
Letters := Letters + [NextChar]; {Insert a letter.)
Read (NextChar)
end {while)

{assert : Last character read was the sentinel.)


I
end; {ReadSet)

d
; 12.1 nJzY7UJlU ReadSet

cnu? Q ti7e 12.5 ni~~~rrurrlrm


(Writing Set)
n i 3 f i u h m msio~nmrouii~ndi~wn~m~iw~il~~~ui~n~~11m)r("s0'b~
wwir
d i d ~ilwaui~n~riiu"~~~~1::fiuw'n3ru~~~isa J fiuw'~nw<26~6
~ r i n t s e t ' I w ~12.2
~ ~ ~ j ' l w ~ a n 6 ~ u n w h~md3'
o w iLetters
f i f V C J ~ Ufii~3i$onPrintset 6; rA'. 'C', 'Z]
L ~ U W I ~ I ~ L O V ~PrintSet
U ot~~rmawn~ilw
{A, C, Z)

procedure PrintSet (Letters {input) : Charset);

{ . .

Prints the uppercase letters in set Letters.

I Pre : Letters is defined.


var
NextLetter : Char

begin {PrintSet}
Write ('{I);

for NextChar := 'A' to 'Z'do


if NextLetter in Letters then
Write (NextLe rint a set member.)
WriteLn ('1') 1 ,'

end; {PrintSet)

tbUU%% 12.2 Self-check

F~J~T~LOUW~QW~
a) A + (B C) -
b) A + (B C)
c)A+B+C
-
d) (C A) <= B
e)[ ] < = A * B * C
f) A + B e [ l . . l ]
Name := ' ';
WriteLn ('Legnth is ', Length (Name) : 1)
fii~unmugnasrdlfi
Name unrliun&n'<u length ~~LLRIP$QMIU
Length is 0 I

d ,au u
a i u g n a s r i i ~~ u i u i i dmu~nucn~uua?onu&q(NUNstring is a string
with zero characters.)
nisddumiu5n'nad&tflllaa (Converting a String to a Number)
Turbo Pascal ~narlJludiM'lu~3~~
Val ~ J ~ ~ ~ H ? u ~ L J ~ ~ u E ~ ~ u o ~ ~ ~ J S ~
X u
mu m~l~nnuscef6o~nifZw3n~s~d~riUi~dau
mo~~ilumuo'nusrK3~~1
muo'nufrh~aunuit& di)&nu~:~dui~dkiun83~nu~uw'udh~?u~3
Leu Pascal dgn6od &Q~IJL$W . *

'123' , '0.12E5' L i l ~ 6 ~
aiuo'nasrfitan ~ u i u 8 4mtlgnustdd content uoou'u ~ ~ u n i(Nume-
l~u
ric string is a string whose contents are a number.)
r u u R i i IntName UEIZ Error ~IluRjttdfaQ~~
Integer u'on?iu&~%~n
nsru-~u~iu
Val ('1234, IntNum, Error);
u L kL~ ~
~ h ~ ~ ' e i i 6 1 ~ 71234 Z lUn t ~ u mna: 0 Lfiulu Error naru3u4iu Val
mmIaJ8h (K3~~da
Error) Val n h f l ~
cii o ~dobiiu'oiinwainln
$amiuih$unn~:
Val ('12#34%', IntNum, Error);
d rru
niifiufii 3 136 Error ~ ~ f f ~ 1 ~ ~ 1 6 3 0 ~ n u s : ~3~~d Ii ~~i ~~d~d K
eLXIQUI/Q~ L A Z I
iinwain~iind;rnis~dduuorIaI~nnr~i
k<uciiuo.s IntNum%h.QnCuiu(undefined)

ReadNum L~u~3LLd3'llfh
Real brat IntNum LLA: Error 1~~<3LLd3~6n
Integer aQn
rr d u a
~ usn~i di ~l Integer
~ n ~ w i s i i i ~ f~i i a n ~ ~v?orii
~ Real ornnunu i r i u A a ~ n ~ I ~
Call Values Returned

Val (I-3507', IntNum, Error) IntNum $0 -3507 Error a! 0


Val ('-3507, RealNum, Error RealNum $Q -3507.0 Error Q! 0
Val ('1.23E3', RealNum, Error) RealNum !€I 1230.0 Error ?Q 0
Val ('1.23E 3,RealNum, Error) RealNum $Q undefined Error 60 6
Val (' 1.2E3', RealNum, Error) RealNum undefined Error o! 1

repeat
Write ('Enter an integer value? I);
ReadLn (NumStr);
Val (NumStr, InNum, Error)
unit Error = 0;
IntNum filmtJ~n%3rAiLRudl~~?"I~
L~udldlu?%dulu NumStr ( & J L L ~ ~
mtJO*nu3r) nTru?udiu Val nKu!u dl&bau,uuodhh IntNum (%fa Integer) 61mu
~nurrci?uupndlu,n r u ? w i u Val n~uiiurii~d2dturilu
Error (%%aInteger) ~ht
p . ~ 9 n r i i &nir
~
~ ~ ~ ~ d u $1 r u ~ ~ n r ~ w ~ n " ~~~~~n~~r~nf i~l $~LL~~U'I~LRII~IM~U
"~f hn hu ' I ~ " F d f
~du\rj?nn'od Cddflodn'~u'okmwmInvalid numeric format 6i~lifiY~dbdrttnruo::
~Wnnaid6u(abort)
Syntax Display
nJ3J3Mdltb Val (Val Procedure)
year I,
fiilrlud~" 9-12) iil contents
null n.n. (liq~~lrl~hd
uod Date fro 'Jan 25, 2004' content m~mukuannuw3ud31fi Month, Day LLR::
Year BZL~)U 'Jan' , '25' LLR: '2004' 817rrJd7<U
&O~IU 12.8
n.r-uaud7w printwords 1azd 12.3 L L € ? R J W R UWUIUWT~Q
~ R ~ ~ ~ ~ ~LID$
* I 1- 1 A d
Sentence 210464 L I U ~ J ~ T & U U ~ ~ B I ~~~ ~~%~ 0 1 2 7 ~ ~ 1 7 2 1 ~ ~ ~ ~ ~ ~ 1 ~ ~ l f t ~ j 7 d d

procedure Printwords (Sentence {input} : string);

{
Displays each word of a sentence on a separate line.
Pre : Variable-length string Sentence is defined.
Post : Each word in Sentence is displayed an a separate line.

1
wnst

var
Word : string; {each word}
SentLen, {length of Sentence}
First, {first character in each word}
Next : intqger; {position of next character}

begin {Printwords}
{Display each word of sentence on a separate line.}
'
First := 1;, {First word starts at position 1.}
SentLon := Length (Sentence);
for ~ e x :=
t 1 to SentLen do

IT 257
This is

First Next
pq
wAj91nbiuhcis:di~~n'?First gntdlwdl~i~w
Next + 1 ~di~wlfjillwdduad
o'nu.r:Ri u ~ n w fioinaon~ingd
u
en?iuk
Word := Copy (Sentence, First, SentLn - First + 1);
in"udlqn!nniuuodSentence 1~Word [hwj~d3:1~ni
First iYilf?b 17 ill:

ciiun~wisiiinam?nmu6a(23 - 17 + 1) d 7 v r i i d u ~ n d ~example
6.- d
n
Syntax Display
&%.4 copy (copy function)

Form : Copy (source, index, site)

63a d i d
Copy ('Mr. John Doe', 5 , 4 )
u 2 d
~n?iuwu?umw
: ~dn'$wnt?uh~
muo'nu.r:Liauuas source n4iuwJd index
un::d~:nau6an: size K~wirnfj~mnSd
source ~fIwil'aitd3wdf~iiuadm~Knll~5
index LLR:: size 60dig~~lftn
Integer

-
0 s
.
aaadbnm 61 index duwinlw~niim?iuui?uad
source mu~nusriid~rgn
d A - d r
nt?uAw61 size nrl~wwnua?anusru~nni~m?anu~::nbnna~~
- U
m~uwfiAiiiwd~
index
.e .e d
a:ii~awim~anus:n~wlaa~ua~mu~nu~:gnnKu~~
n-isdnfi~wo~a-irr
Enwsr (Concatenating Strings)
&fi$w concat W ini3da h wja~?a~nYU
ua~m~Knu3:
idaZC~Dwm
u6'nu~:.
h riwdqn
n-isdoK~rro~a-irra'nasr
wuiuid nin~~m~Knnll3::iu'i~~~n"w
i ~ a l M h
me~nuarqnlwai(Concatenating string is joining strings together to form a new
string.)
~)
nu, ( a s ~ a ~ ansunop
m
yr 383~03 l
nNPbpe P*ZL phi a a a ~ a t fiwb~
OC'ZC CLROL$
function Reverse (InString : string) : string;

{ Reverses the string stored in'lnstring )

var
I : Integer; ariable)
TempString : string {temporary reversed string)

begin {Reverse)
Tempstring := ' '; {Initialize Tempstring}

, for I := Length (InString) downfo I do


TempString := Concat (TempString, Instring [I]);

Reverse := TempString (Define result.)


end; (Reverse}
1 = (luawalels 'la6~el)sod
CC'ZC bLIj1@C$
L og PUtlSOd
~ l u % b ~ y e3ualuas
m
p~p nlnvi :ABP 118 AU aaq PUB ,
S P J ! ~:cnu!nu~y
PUtlSOd @I
m
.
a3ualuag :cltuonunl, pue, ronurii~ruugir~uron
nyngqlaii ~y~ y u n r n ~ y
2 I
(mualuas 'p6~e1)sod =: puysod
o nnpi rueluoa n muqmmnLLumuy@nDi~ 3 6 ~ ~y
r ~ n ~ m~ ,uPUB, Y
8 1nypbo
7 '-Pm
DL; m
p i ~ $ b t ~ n y ~ p . f l,lPUB, m
b ~ ?XRUDOUJ LF,nLLWLUEl3yUlfl%lDLPD Lb ZLMQ1fioP
m
:csuonuronryl~t~y~b~oyn~i
~g~rircunep
1 ~ nm u e n u s k d ~ ~ n ~ ~ n 2 1 p e ~ t
2
( q a ~ e e sBUIJJS) ZZEUORLY
m
LHH~LU
eu Y
aLJrd ci?odi~~du
h n a i u i i for $2du~uu?inn~w'u%61du
for counter := initial to final do statement
POS ~di~i.dii
tnaiu&aoahkd ~~snu'~~rs~d"k+i~fia'tb rnliduodmtl gnust
'for ' (contents Vod Targetl) LLnr ':=' [antents uo4 Target2)
u'onalukd if $7 k ~ u l ~ ~ ~ n n o s : 6 d o ~ ~ : ~ ~ l d i ~ ' ~ n ' n 1 ? ) d L ~ d l ~ d \ ~ ~ d

gnus:: Counter
PosFor := Pos (Targqtl , Statement);
PosAssign := Pos (Target& Statement);
if (PosFor > 0) and (PosAssign > Posfor) then
Counter := Copy (Statement, PosFor + 4,
PasAsslg6- PosFor 4) -
~~o~~inmao'nuse
'for do'no~z~~a
nii u ~kuciuuo~
h counter o3 d ~ d i ~ ~ ~ h
\
U u
6 PosAssign PosFor - 4
Posfor + 4 4iuauuodclaanus::h Cwnter +'Iiuam~atl~fi~ -
u t:
fimtlgnus:: 'for ID := 1 to N do X := X + 1' rfiulu Statement 613uM PosFor
O Z L 1,~ PosAssign ~141%8 LLa: content V Q J Counter Aom~Cnus::'ID' (fl71UU11
Liin'Y -
(8 - 1 4 L ~ I ~3) J
PosFor PosAssign Counter

El
Syntax Display
q4Tf.a'~POS

67 o d w
Pos ('you', 'Melyou')
" .a' d
iinaiucluiun.su : mngnwe source q n c m ~ a i n ~ i t l \ d u a i r w ~ ~ i d i i w c i ~
t:
uo~nis~R~~s~r~snua.~mtl~n~f=:dotl
pattern
((p6.m~)q@uel 'e6elsod 'zaualues) qalea
Uaql 0 < 6Jels0d fl
!(eoualues 'la6mU spd =: 6~e1sod
r3~ n ~ ~ u o c n ~ y u i ,
,aldwexe s!,
P
npn~uu
= U W J ~ S con
1 )
~
R K luelum nnrle eaualueg uLauoo ,eq1, : r n u ~ n ~ ~ ~ r o n ~ r i i r r u ~ ) g i ~ ~ u n ~
2" 2
!(p '(muques ' p 6 ~ e lsod
) 'mualue~)elelea
nLrnLn:rU
unrirBnLLuon ,eql, np
6 Z
~ $6~ L
~uelucx,n B ~ U L ~ ~ L L ~ ~ L L U : ~ ~ U ~ LY
Ie
J Q1~
-3 "P.8
,aldwexe aql s! ,
P
n p i n ~ wuarues
~u ronpq rualum >
n
r;
u :muonuu~&u~o
m
utnr~b~,~:rnu$
r5n1 .8
n
r prpKii~ p u ~ a rn p'aaua~ues
(9 ' i. 'muelues) alelea
nLrnLn:ruuourplnLtuoc uM1rrunLcnLn:suuoysLuorou
3 2
I
- ,eldwexa aql s! s!ql,
.8
:rrtuonuo~ynou:rp -ueluas L+~~O~OB
Zb'Zb PLpOL*
oqm0p=mujnurgr~I:IiiJs1o~i
nLrarcn:ruprl leased ocl.lnl L~n:mu~ourLun~iL~u~npyr~u~auofi
.
(vesul pue elelea e~npeao~d)
v e s u ~mn elelea W L ~ L ~ ~ Z L U
j

6'J~dld12.13
1uuij7 contents WOJ 0€4'70&7W3: 'is the stuff?'

LLn: contents 2104 New here'


L d
~Y~~IUR~L~U~~TZLI~U~
lnsert (NewString, Sentence, 1)
'Idmua'nur: 'Where' 6di~inddi1 vadmui"~~f=:
Sentence
rhI151JdnO~LLL(RJcontents 2106 Sentence L~U'

'Where is the stuff?'


ruuGii contents to
WOJ ~ a r g e t 'stuff UR: contents go6 NewString ' 6 o
I*#%!'

ilaclaiuk~
PosStuff := Pos (Target, Sentence);
if PosStuff > 0 then
lnsert (NewString, Sentence, PosStuff)
!
ZtimuOYnlrr: '+#%!' 'Ui~~G~rnuKnm:
'stuff IUsentence
L.&
RJMM contents

h a i v o ~Sentence nwiorilu
'Where is the "#%! stuff
620$ld 12.14
n3:uawiu Replace Iufl 12.15 L L ~ ~ ~ ~ ~ & v ~ : L ~ ~(Target)
I R ~ I ~ L ~ w I ~
Iwrnso'nus:6u~Cu (source) iiaumeo'na~z'l~ai(Pattern) ui\%q4n".a'u POS tdo~i
d d
dluHG6 Target, n3:LIatbJIU Delete LWonYoon UW:n3:u?wiu lnsert L W O Pattern
~

I u d n ~ ~ t i ~Target
u o ~i i n i ~ ~ ~ r n ~ 6 n a i ~ k n ~ ~Target
i~n'ilai~u
Syntax Display
nf,"LIaw6iuDelete

Form : Delete (source, index, size)

&2 o dld
Delete ('He**ll0', 3, 2)
i
iln7iunuiunjd : nio'nustokW~iu7usize Rign~udonainmubu.ir ,

source ~~u6u$iaugnu3zfi#o~di~~~dt
index w ~ n i k m asource
i 6o,q~gurnuKn~~t i

-
LLR:: size, index
.r
nagldrnm
~ Q J L ~ Mlnteger
%%~

Y
u
61 index i k i i u ~ n nLength
i~ (so"rce) atluumaon~3tkn q pnau?it 61size
w
4 - u u u
d ~ i ~ u ~ u m ~ o n u ~ t u i n n i i 0 1 a o n ~ ~ t4 nd"r ~~~~~oooo$$u o ~ r n u k u ~ r a t p n n u ~ t
~?u$iM$iat~di
L L H ~index
J

Syntax Display
nfzu7tbtiw Insert ,

Form : Insert (pattern, destination, index)

S2ad1.1
Insert ('bb', 'Bully', 3)
u 2'
jinaiuvruiuntw : muiiYnu3r pattern I ~ ~ ~ o w ~ ~ O ~ ~ Y . P , ~ index
~~IJ'MIM~I~L
wim~Lmafpattern LLRt destination 6 ~ t L f l ~ m ~ g nLLar : 6 o t ~ ~ w %lnteger
n ~index fh
CaGdrna
V 2'
3 1&6ih3t
f i i ~ ~ ~ n ~ 3 t ~ R ~ ~ ~ 255 ~ ~ 1 ~wargnnnmt
3 ~ 1 n n ~(truncated)
i

~ ~ j k ~ 3 z 255
vhd

procedure Replace (Target, Pattern {input} : string;


var Source {inputloutput} : string);

{
Replaces first string Target in Source with Pattern if found.
Pre : Target, Pattern, and Source are defined.
Post : Source is modified.

var
PostTarg : Integer; {position of Target}, , I
begin {Replace)
PostTarg := Pos (T e) {Locate Target.)
if PostTarg > 0 the
begin
Delete ( Source, PosTarg, Length (Target));
Insert (Pattern, Source, PosTarg)
end
else
WriteLn ('No replacemmt - ', Target,' not found.')
end; {Replace)

;dd 12.5 naumnu Replace

rruu3nGm 12.3
1. ~ ~ d i ~ a r ~ ~ i w n ~ d u ~ ~ n i ~ ~function
~ ~ n designator
n ~ : : ~ $1~4 a ~ ~ i ~ ~ b n
A'
i i ~ ~uuRiinirbd3rneKnu3::~ilu'dGm
u string [20] an: contents hu6~uiuuo~Templ
G O 'Abra9,un:: 'contents'UO4 Temp2 €I: cadabra'
a) Magic := Concat (Templ, Temp2)
b) Length (Magic)
c) HisMagic := Copy (Magic, 1, 8)
d) Delete (HisMagic, 4, 3) ,

e) Insert (Templ, HisMagic, 3)


f) Pos (Temp2, Magic)
g) Pos (Templ, Mqgic)
h) Val ('1.234', RealNum, Error)
i) Str (1.234 : 3 : 1, RealStr)
j) Val (Templ, RealNum, Error)
k) Str (0.00345 : 3 : 1, RealStr)
LSZ JI 009
rpuubu~nonzmu_on
w n ~ ~ ~ g u ~ b n ~ ~ z r n u , o n y\ufiuLpryuiiLp
u ~ ~ u i i z s i i y no?
.m
zrffiuonup\ nonzrnu_onucgnu~ L M ~ ~ ~ L c
~. H L ~ I ~ ~ ~ U ~ ~ L I C ~ ~ C
6
n .m C L (~
z b r ~ u r u g w r n ~~n~~uo$ronuurrrrptn~~no
u s u Q ! ~ E ? J6u!l!pa)
~ ~ o yri a ~ n s
rLunpr LprLuLpzruo91 (~o~!pa)
rugLmrrnynn~irLuzsiinniiuoorLu
(~~I~OJCI) LMSQ( C
)
( ~ 0 1 1 ~ 3x a l nLLuo6uugLturrnt..LFIUE~J
6
i irropw~p~bri riypnai
uppum ~ n ~ m~ n u c ~ r ~ ~ ~ m r r n nbiru~r ~ n
L U ~ L ~n ~ ~ n L ~ L ~ M M U ~ L~ ~i lUb ~~ S& ~u ~r ~
L ~I n~ ~L r~ ~ ~yM~r L~
~ U~
C ~u~ n~~pz
~~)
norpL ( J O S J parfour n y oy r n o i ~ n ~ c r ~ ~ p , a ~ y n ~~nm u
niim s eud ~
7 - -P
n r u n r p ~ i i o ~ r u zrs ~
i i p !Io (~oypalxal) roLLuo$rugLnrunL~,ap\Lri
(paJoJJ~nll1 XrDUOQLYPMYLBlrpCLURLrS$O P'Zb
6u(ssa3oJdBU~JJS) -
(~a6alu!a6ue~-u!
ue) .mw cl
n~~n$onpm~n~pnsinunuur~unn~rn~nzru --I
ysu\LyynLnLp.pLu\ L U \ U L ~ M
R
vtjo~pyzuirn y i n ~ n ~ ~ n s i n(svanum)
py\ nnapri2u~rz r n u o n u n p ~(uho_ -
n ~ r n ~ n z rnu ~ w
) n pm y n ~ ~ n ~ ~ ~ c p ~ n ~ i n ~ n ~ p n s r 'Zn ~ p ~ ~ n ~ ~ n ~ n
,ewxle,
I
zrnuonununsu=nLrirenn,ayrk
.m P .m
,3qe, zrnu_onuy\unHLp npirLnoe.
('S~J~W
PUBOSpJOMy3eq
~ aUeS ayl SpeaJ JeyJ~ U ! Je~S!
S aUOJp
n
-u!lsd V) ~~nnopvi:rnL$~LLeuLbnLpo~HLLnpLnLpuLanLpPezrnuonw
rgnLnH
, aUoJpU!lBdRLUy n 'L
L Ul,%LUOOU1ULQ auo~pu!ledrl$32br$~Jyfj$fin~lNl
nruiirppngi
l
l a 6 ~ e npbr
-n~nnyizairu e a a n\n~~u_n~ronoenyropr
Ado3 zsn sod naurl;,a\rn m9
aweu l a y 'aueu lse(
n n
pkprrL>n&izNir LU)rpHe u ~ m
npirL?oLu (aueu ISJ!~)nLrunMrono~oryn~zs11
n r y n ~ un~cu_n~rnon w
(eueu lsel) sbun~nognynyaa~nos~punns
oz nucui n
I
L L ~ R L L U ~U
~ !. J~I u
Sp ~c.~ u n u .m~ p i iu!lsaa ~ i laBel 'WJ~OS
~ u n zsn yq, -z
\
2) ib~nrd
(Analysis)
~ s r m u i 3 n I ~ ~ t ~ l a ' u ~ ~ ~ ~~Guni3ms
n ~ ~ ~ ~;nuxu
a ~ t iout ~Turbo
mdi
Pascal ~do n ~ f l i n i ~ d i r ~ u n i ~ ~udn d*uau'otn'u~~ms~iu
ilunn~n mort i sJd.run3u
t:
I i m u ~ n u r v & ~wnuud3ruasl
n wam~o"nu~r~mum3tdi~s~ikt~iu~\3ao~~fi
naiu&It nisoysl (Data Requirements)
6 ~ y ~ % (Problem
p1 Inputs)
Source : string {the source string)
Command : Char {each edit command)
~oi6ynJqcli(Problem output)
Source : string {modified source string)
3) oonrrrrrr (Design)
~sln
o?flu~$uh(Initial Algorithm)
I. diumuo'nunC(orpnnno~~fiflu
source
(Read the string to be edited into source)
2. repeat
3. di~f!itt'j~i~.n~u6
(Read an edit command)
I
(Perform and edit operation)
4. n~zflinisdi~Qun7.rm3~aufi
until done
n i s d u a r r ~ u ~ u c l r ~ ~ . ~ u p (Refinements
~ ~ ~ ~ ~ r ~ ~and
u nProgram
lu
Structure)
L: d
DoEdit ~ ~ L ~ U ~ . ~ ~ I L C U ~ I S C ( L H ~ I : : I ~
uun 4 gnn~zfli6aun~rlf~autw
rd odiurns~nu.rru'aynVn~~n"aiu
~ ~ ~ r n ~ r d i n i ~ d i ~ [iauuatih
' ~ ~ n i ~ d ~ a t n i ~
~ ~ ~nivdnawirC(urrknaiflu
~ n . r t a i i t r h c l ? u & u n ~ i ~ n ~ u ' ~ ~ a i 15.6 ~umt~u~d
U X
ohcliln?ruautiu DoEdit LSURJU .
f i ~ ~ d . r ~ a w(Local
l r d Variables)
OldStr : string {substring to be found, replaced, or deleted)
,-
NewStr ' : string {substring to be inserted)
Index : Integer {index to the string Source)
+I 4 I
a9~noS. *aVuI xavu 1
iasflaN
II """T
{ase~} pus
4) ni~fii'hkn'm a. (implmrntintation)
~ ~ u n ~ r m d n s u u 3 u ~ d u ~ 1j
%I ~ ~rtfau~um~~2i~ci?a
m ~I2.7 i~du~u t h (sample
~
run)

program TextEdit;
{Performs text editing operations on a source string)

Sentined = 'Q'; (sentinel command)

var
Source : string; {the string being edited)
Command : Char; {each edit command)

{Insert procedure Repoace.) {See Fig 12.5)


procedure DoEdit (Commhnd (input) : Char;
var Swm (tnputioutput) : string);

Performs the edit operation specified by Command.


Pre : Command and Source are defined.
Post : One or more data strings are read and Source is modified if
Command Is 'Dl, 'I1, 'F', or 'R'. If Command is 'Q', a message
is displayed; othenwise, nothing is done.

1
var
NewStr, OldStr : string; {work strings)
Index : Integer; {index to string Source)
LSZ &I
I(a3~nos'JISPIO) sod =: xapul
!(JISPIO) UlPeetl
I(,L ~ ! JleUM
~ S Pu!d,) W!JM
{PU!~}
u!6aq : Id.
! ( a ~ n o g'JISPIO) sod =: xapul
:(JISPIO) UlPeatl
WUM alwa,) ~ W M
f(, i 6~1~1s
40 pueuuoa asm
Write (OldStr);
WriteLn (' found of position', Index : 3)
end
else '

begin
Write (OldStr);
Writetn (' not found')
end {if)
end; {Find}
' R : begin {Replace)
Write ('Replace old string? ');
ReadLn (OldStr);
Write ('With new string? ');
ReadLn (NewStr);
Replace (OldStr, NewStr, Source)
end; {Replace}
'G' . WriteLn ('Quitting text editor.')
else
WriteLn ('Invalid edit catiaracter')
end {case}
end; {DoEdit}

begin VextEdit}
{Read in the string to be edited.)
WriteLn ('Enter the source string : ');
ReadLn (Source);

{Perform each edit operation until done.)


repeat
{Get the operation symbol.)
WriteLn;
Write ('Enter D(Delete), I(lnsert),');
Write ('F(Find), R(Replace), Q(Quit)> I);
eeadLn (Command);
Command := UpCase (Command) {Convert to uppercase)

{Perform operation.)
DoEdit (Command, Source);

{Display latest string.)


Write ('New source : ');
1

WriteLn (Source)
until Command = Sentinel
end. VextEdit)
Output Window.
Enter the source string :
Mary had a cute little lamb.

Enter D(Delete), I(lnsert), F(Find), R(Replace), Q(Quit)'> f


Find what string? cute
. cute found of position 12
New source : Mary had a cute little lamb.

Enter D(Delete), I(lnster), F(Find), R(Replace), Q(Quite) > i


Insert what string ? very
At what position ? 12
New Source i Mary h
Enter D(Delete), I(ln (Replace), Q(Quit) > R
Replace old string ? lam
With new string ? lamb c
New source : Mary had a very cute little lamb chop.

Enter D(Delete), I(lnsert), F(Find), R(Replace), Q(Quite) > D


Delete what string ? very cute little
New source : had a lamb chop.

Quitting text editor.


New source : Mary had a lamb chop.

U
sd 12.7 ~ d s u n r x r u s s l ~ 7 ~ n s u ' ~ ~ 1 3 ~ ~ ~(Text
~ ~ zEditor
n ~ ~ ~Program
d ~ 3 0 ~and
i i Sample Run)

12.5 $oijnwaim+auao~ni$b$uuldsun.ru(common programming


Errors)
W n
souanaialil~ni~l#'lak
(Errors in Using Sets)
ad U
Y'onas-41 hrdamm nkisnu.audaln q n'miu i i o d i o ~dusiuri
~n ou4~a::~i
hlkd-,rirunia ~ ~ ~ ~ M ~ J W R U U H ~ & ~ ~ ~ ~ J & ~ ~ : : ~ ~ ~ ~ " M ~
i u s
diufiil~aJuduramiij,[ I, mnamni3fii~umFiiaLii~Cm~~5~
~qAirftuniauo~ Pascal ~n7~63~.a"l6n'urrm
f l 7 i ~ ~ a J i~K7dii
~ft~nia
~ZI~

rdi~u)1drio~inn~iritwnino~fiu~~~br91
91 1 unudrrt~u~areu Wsm~i'Hiiadfirm
M~JIIJ~U (unitset) ( ~ a m d i i ~ o i % n ~ $~dornlh$nau$J
ni) ~uienvo~~rn
nladirQuniari?uaau~o J bra (set union operation) luihod

['A', 'El, 'O', 'U'] + 'I' {incorrect set union)


b.hn6o~
91 rrnzn~ard~u%~irflu
['A', 'E', 'O', 'U'] + ['I1] {corrcet set urrlon)
[L 'E '81uai n$ s#6!a
[8 '9 'P 'Z '01 uai oy s1!6!a
[c 'Z ' CI uai o$ s#6!a
[L '9 '8'z]Ma1 Qg s@U!Jd
repuar o~ su6!a
(pn~asuo3ieosed M ~ JO
N heuurns) leased E OD~~CL~BL_Y~IPO
(SJOJJ~
w -B
yaaw a6ueu) ns~nosnwur~uu~ukv~opn~b~ruo~;c {+US) (a~!iaas!p~al!dwoo)
9 Pa
r nnu~j nu~r )~ue~ u
rouiqnw~iiaLuaerb~ n ~ j ~ _ ~ r o n n -u -~ Pp ~z u ~ropi
?r -4
uniL.npiplq P O N ~ L .
roursuLMnginnenyzrnuonu
9 c.
vasul ncwLnzruzNir sod w . . y r ~ n i ~ q
- r,
(slolwado u!-y!nq) ~ u n l r ~ u n $e i ~ p ~ u
m
)
( ~ e p ~ onunorogircurynouno&rcponeNetruurp1
slop )OYI?LLLMPOR Pa Pa
IB3Sed PJepUqS
nwwegpvuii m
leased oqJnl nuguezrnuo Pa
n Lnsrcuncie pm~ uL ~ J . L L ~ L ~ ~
( S ~ ~ ~8u!sn ZLLU_ORLY#iLLUNiULNMUCOt
J J SU! PIOJJ~)
y ~ ~ n ~ z c s p u a i n ~ n n ~ u r n ~~JpL~~i ~~ Qp R
z~U~
~~L~
sNi ~LLU
2 A
nmr v y uaironn~Eu~nnlcpzup~~no~uuroyui uaiynyrw ~ o i r o n n n g i ? i r ~ u n ~ r ~ ~
~- au n l n unyny
l Pa
repuain~nnun~ r ~ ~ ~ u ~ u ~ w ~ ~ ~~ ~eL nU ~
L Rn ~ p r e
.r
~I!JM OLH Pa
~ L U ) ~ ~ ~ U ~ L ~ ~ L I J ~
peau ~ L ~ ~ L ~ P ~ E O B L ~ ~ ~ U U usiucaropi
Construct Effect

Set Relations
Primes *= Digits , True fi Primes L ~ ~ U L ~ V I ~ DDigits
~JVD.~~
Primes >= [ ] LOU True ~ a u o
Prime *> [ ] True ni Prime dmiBn
[A, 2, 31 = [3, 2, 11 True ~wm:j~r~~~dumunif~~uultji
o'uh (unordeved)
String Declaration
FirstName, LastName LLB: TempName
I

Capacity = 10;
type
StringType = string [Capacity];
var
FirstName, LastName, TempName
: Stringtype;
String Assignment
FirstName := 'Daffy'; LEU'~afFy''luFirstName
LastName := 'Duck'; ~fiu'Duck' b.4 IastName
TempName := LastName; UR: TempName
String Copy
TempName := Copy (FirstName, 1, 3); fiih~uq
'Daf TempName
String Concatenation
TempName := Concat (FirstName, Ln'u 'DaffyDuck' IU
TempName
LastName);
TempName := FirstName + LastName;
String Search
Pos ('Du', FirstName); na'Yfiu 0 ('Du' not found)
Pos ('Du', LastName);
Pos ('Du', TempName);
![9 's 'PI + tlas =: Zlas (e
npLopuaoon slualuoo uonra [c- tl o y rlas esbqylunwLy .s
R
ngiuainlrru~rLpuuLropp~pugLr0~Lp.
qp;r~~r~por~pu~"
2
c . 1
'P r L ~ U U l l R L t U P
RLtU$2U11 ~ ~ L f i l ~ l U U ~ l r @ n U ~c.L cR. B ~ U 0 1 0 n U R ~ Q ~ H ~ ~ P L ~ @
r
req:, o Isrr r ~a6alu1
npM ad4 1904 pp ~ ~ ~ ~ ? ) )nM
~ nI g
QoI~
ppkw
La
u~ ~ e 4 3OLY J86~lulu~arengi(ad4 aseq) ~ G U ~ ~ ~ P ~ U M.E~ U ~ L ~ ~ U L L I
4 Isr
(a~!~anwuoa)
p -ck. m r ~ u p l p ~
u ~ v r k n ~ ~ r i r ~ u naauaJay!p
~ r ~ p re~oup t uo!paaaw! ue 'uoyn las (e
R
: u k p u ~ n u ~ ~ r n ~ p u l u h r ~.zu o r ~ i ~ ~ ~ ~ u
VQQUw (m les~a~!un) ~ U ~ R ~ M U U Q I' t
y ~ e q p q ~ ! n~o~ u g n n i i
I

-74.1E-1 OEO 25.3E+01 -74.1 0.0


u ~ f i u dgniiodlu Pascal
a) d ~ ~ u o t l i i d d ~ ~ 3 u d i i r 2(integers)
b) ii~auorlsliid&~~u
invalid Integer lu Pascal
C) ~ ~ ~ ~ o t b ~ i ~ d d ~ i l(real
u 8numbers)
~uau~ " r ~ Pascal
dOnhdlu
invalid real number 1%Pascal
d) dlRuorl3fild~d~~u
e) U ' F ~ I ~ P ~ G I ~ ~ J ~ ~ L S U ~ I L H ~ D U ~ M ~ U ~ P ~ S ~ I
f) ddilnui~d~nniio~lu
u Pascal w 816 lfio'rmi u
udIimuirodfiu%~~irlrriiii~l
uar un~v~w~d3tnouRiu
A d
1.2 BosioWwa~aobfii~*~~d~~flu
identifier daniiod L L R L ~ & I ~ R ~ ~ I J ~identifier
JL~U

lrionrio~
U ~~a~iuiirll'lu~oU"u~~ifld~fi
ITEM-2 ufo TO-MORROW averylongnameindeed
IN-PUT array PC49 McDougall X99999 $100
Functior! case AND program boolean writeln
A-number -counter ID# Item2
a; 2
2.1 ruu3iii X, ri)uKaud~ffititd1uau~6~v~~~1nn1~n~::vSini~15i~uuo~
y LLR~ z
i n ~ n rvuadniud3
.~on?~ua"J~'1dsii~6~~tja h a z ~oorld$ttandrln$unou
x, y u
x := 5 0 ;
~ i r o~it~arIUci~:rl(l
i i ~
-
a) 6.75 12.3 1 3
I

b)6'11 -42div5
c) 175 mod 15 div 3 ' 6 5
d) 1 3 + 7 ' 5 - 4 ' 5 d i v 2
e) 11 m o d 4 d i v 2 < * 0
9 ('A' >= T)or ('9 >= '8')and ('A' < '1')
#a 3
Y
3.1 ada~iunir~idiuuodnrrua~iu~idiidu
procedure puzzle (N : nonnegative integer) ;
begin
repeat
write (N mod 10) ;
N := Ndiv 10
until N = 0
end;
~doh~un~flwiniiuofidu'tl n ddi~riir5~649 ~oidynuo~~dllsunru kao=:Ills
3.2 a) ~ ~ ~ ~ ~ ~ I ~ r ~ ~ n r u i u ~ u y a ~ i l u ~ ~ u ~ o~.~iuau~in~U'uoi
riiuriiclniu~~do
speed1 YRZ speed2 n i u d i h o ~ n $ u ~ r a q n o uuunilnGriiuinn411G
ii
rd w
mulanaaddo highspeed ~ o$ni kiuh~uzd
mudw.adnul~'dnnQtlu
uaddiulrlsi~
A A
b) odlirtlun%uaw7u%o findlarger % L R ~ ~ J ~ I no U UR:: second aiu
U ~first

di&d data type LOU real ~ n $ ~ i t n l l s ~ 0 ~ ~ ~ ~ 1 ~ ~ m ' ~ ~ ~ d ~ i i ~


d
n larger dd~fluvariable formal parameter
program exam-41 (ou
var a, b, k : intege
function f (x : integer
begin
f:=3'x+2
end ;
begin
a := 0;
for k := 1 to 5 do a := a + k :
b := f (a) ;
writeln (b)
end.
~ i n ~ d ~ ~ ~ n r u r~ ~iad~du i~ rd m i n l i ~ i u a ~ e ~ 0 1 ~ ~ ~ 1 1 1 0 ~ ~ d r ~ ~ n r u i F 0 0 : ~
program exam-42 (output) ;
const morgansubrange = 1 .. 10 ;
var k , m : integer ;
a : array [morgansubrange] of char;
begin
a := 'ABCDEFGHIJ' ;
for k := 1 to 10 do
begin
m := k mod 2;
if m = 1 then write {ark])
end ;
writen ('xyz')
end.
! pua
(43)al!m asla
-741E-1 1E4 . 25.3E+01 -1.27E-03 0.0
I ~ . s I ipnrio~lu
a) ~ L R ~ ~ ~ ~ T ~ ' ~ J ~ J L I(integers) M ~ M L ~ uPascal
b) i i r ~ u a r u i ~ d ~invalid
~ i l uinteger lu Pascal
C) Z~LRY~ZITCIJ~JLU~.SIU~M~"TJ
(real numbers) dgn6aJlu Pascal
d) Z ~ L R ~ ~ Z ~ J ~ ~ J invalid A number 1%Pascal
~ J L %real
Pascal
e)-iirillmu'lJ8J~flurilL~~aun"uiu
d'd d
1.2 BodaWu~a~lmlji
J ~ J L ~identifier
~ U i p n hJ L L R ~ O ~ DJ~~ ~J UL U identifier
~ IU
d L:r
bhn6a~lttio^au~ui~~~luaa~bu'il~l.a"L~~di
q

A Example1 , TO-MORROW ' averylongnameindeed


IN-PUT array PC49 McDougall X99999 $100
Output lx end program boolean writeln
A-number -counter 1 ID# Item2
1.3 Pascal d g n 6 a ~ a ~ n 5 m d i ~ h ~ d
a~~%uuirwad

((A > 6) OR (B > C)) AND (C > A)


NOT (A 7 B) AND (C > A)
LSZ JI 919
ui6aq
! p o ~ da~npa3o~d
!
!pus
(jaqwnu g6!p auo,)uta)y~
r . u!6aq
! 1 30Jd a ~ n p a o ~ d
! ~afjaau!: u JeA
\
! 1.13sasn
!()nd~no'~ndu!)asa=rpalas-aldwexaurw6ard
E ~ C ~ P L ~
~ C ~ ~ O U ~ ~ P L ~ O ~ U R P ~ ~ ~ Q ~2.2
Spua
(P)UI~I!JM
f3,q-e=:p
!q+e=:3
! ha~sAlu
u!6aq
! pua
I !A+x=:q r
:A-x=:e
!z1=:A
!01=:x
u!6aq
! ~efja~u!
: A 'x Jen
! ha~sAwanpam~d'
! ~a6a)u!: p '3 'q 'e Jen
! ()nd)no)uexa-leuy wu6old
ta
u i ; u ~ o i r u ~ n : m o ~ n p ~ ~ ~~ up~t yon r u i i r m w u gr-z
z Q$
procedure prod;
begin
writeln('three digit number')
end;
begin
clr$cr ;
write('enter a number
readln (n);
case n of
1..9 : procl;
10..99 : proc
100..999 : proc3 ;
end
I

end.
a; 3

type
days = (mon, tue, wed,
var
today : days ;
begin
writeln(ord(rnon)
writeln(ord(thr)) ;
today := friday ;
today := pred(today) ;
if today = thr then writdln('Hello') ;
nry r ~ p u i & l a u~l p r o ~ r ; y n ~ ~ p i n y ~ o (
X
nnuLaurro OOE rp oo 1 - ~$H(UBEL~ULL ( s n ~ ~ I~~g~) I ~ I L I L U ~ O ~~I~\
(~acl
3
-ue~qe~)
~ R \ R L IYI(LU~Ou ~ a g t u t fbl n n ~ pJQ
i r+nLLuo+&\nuln,lnruiicp1nnbir a
v o#
!pua
!y + I=: [y lab *
a) Pascal resewed word b) Standard identifier
c) Valid identifer vl%~ d) Invalid identifier

end ................................................................................................
Rate ..................................................................................................

Start .............. ..................................................................


ThislsAlongOne .............. ..............................................................
Bill .......... .............................................................

X=Z ..................................................................................................
Sue's ..................................................................................................
I-2 ocuon31rilrirywoG (literal values) d o ~ d ~ ~ n r i o a v l ~ a ~ J ~Pascal
n & o c61IJ
~u
r . t d
~"RlOcIrriu
on~ayunP ~ ~ ~ ~ I ~ ~ ; ~ ~ I ~ I ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
$oyR (data type) 6o€l:I3
-

'XYZ' ..................................................................................................

'x' ..................................................................................................
True ..................................................................................................
.............................................................................................
eueu JnoA =: s (3
.............................................................................................
. ,aueu Ayy, =: 3 (q
3.2 a) oin$~f17iu&dcase
~ ~ d a (are
u hequivale
case X = Y of
True : WriteLn ('Eq
False : WriteLn ('un
end {case)
b) adL!UU#~~7iukd case ~ ~ U U C(corresponds
U to) rYuban-~ia&dnested-if
Y
'Ujdd14tb
If (Grade >= 'A') and ( ~ r a d e<= C )
then
WriteLn ('Passing')
else If (Grade = 'D') or (Grade * IF') then
WriteLn ('No credit')
else
WriteLn ('Invalid grade')
Ga 4
4.1 a d ~ n s l 0 ~ ~ ~ ~ : ~ ~ ~ 2 1 # ~ ~ 6 1 w n i 6(syntax
1 7 l n ~errors)
~ u w ' ~u ~
d#fln7iu&fi~
i i ~ md& a i n ~ u ~ r i ~ i icuiuj ~ i u(flowchart) d Q n h
If X > 25.0 then

else

end (if)
4.2 N ~ i n w " u f i u ~ ~ ~ n ~ ~ o Nd ~Lrna
a~~$~lw~dr~~nrudiu"itladfiuy1n ~~~tl~iu~u
!lJu¶Ju&d
~~ndwlCJ 1 2 3 4 ...N L ~ ~ C ( A D 2J 3 4 5 ...N + 1
uo?dmu 3 4 5 6 ...N + 2 ~$Ud;:ofl~d &adla L$Y ~ M ? U T ~ I ~ U Y ~ V I L Y / I ~ U5
fiud
! 0'98 =: M
u!6aq
! leas : z 'M
JPA
! (laau : {)nofindu!} x JBA)a6uaq3 aJnpaa0Jd
! l e a : A 'X 'M
JBA
~o16ynuo~W
.run.ru
procedure WhatDo (I : In
begin
if I > 1 then
begin
Write (I : 2) ;
WhatDo (I - 1) ;
Write (I : 2)
end
end ; (WhatDo)
!Je43 : Aea
JBA
Percentate := 72 / 10 ;
A :=5; /J

Writeln (Day : 10 : 2) ;
Writeln (A + B : 8 , Percent :8)
end

lndent := 1 ;
for Ch := 'A' to 'Z' DO
begin
Writeln (Ch : Indent) ;
lndent := lndent + 1
end ;
$a 3
3.1 um~~o16~mi~n.'o~
o~oiuiun7sri1~1uuo~Ids~~nsuu'1~~j1~ib"
tt~r

program ExerciseNine (output) ;


var
A : integer ;
procedure Sub1 (A : integer) ;
begin
A := 20 ;
WriteLn (A)
end;
procedure Sub2 (var A ; integer) ;
begin
A := 3 0 ;

IT 257 625
! [1 + ! * zlx + t! * zlx =: t1 +!Ix
! [ 1 - ! * z l x =: [l-!lx
f 0'0 1 + [\]x =: [!Ix
!c=: !
L~TII;IILFO x n U ) Y ~ l m ~ o n ~ . ~ k ~ I L L m
~ ~ ~ L L U L I ( ~ L U UzB7' ~ ~ ~ P U P I ~ L L ~
~ ~ ~ U L O P ~ E I " ~ I I L ~ ~(IUBW~IBIS)
M ~ U M ~~'P ~
~ lL l ~t ~O ~L ~~ ~ ~~spii~pa~ngo'rb
9'P9- O'P1 0'21 9'Z 0'8 0'9 O'Z1 0'91
n r u u 8 u g ~ r n ~ ~ g ~ y f ) ux ynl u~n u~L ~
y ~ C'P
u~~n~l
r m m
v 02
{a~ms
ase:, 40 1 ! pua
~ r g P~nLLuofi~lRPlnul~Mlnn~qrb
noprr a ~ n u n ~ ~J!u-palsau L ~ ~PI'P
~ P L P P asea
nLtuofiuLn 01 '". 'E 'Z ' 1 '0 PrbiLpp ~ 3 (3t)aLp99L!%g9
6 ~ aJo3Syl14nMQ Z'E
pua
(v) Ulal!JM
f (v) zqns
Ioli: v
u!6aq
! pua
for i := 5 to 7 do
x[i] := x[i+ 11;
for i := 3 downto 1 do
x[i + 11 := x[i]
d sa
4.2 rli~wnlfl
A tUuunah6'u~u~un
d~uibnudmsi?firii~da
16.0 12.0 6.0 8.0 2.5 12.0 14.0 -54.5
o~dsu+amon?iu~~l~~ikdol~d
a) u ~ n w $ ~ u i $ 7.0
n 6
u d u A d
b) r h ~ u i ~ u i Q n ~ a n ~ i ~ ~ ~ ~ ~ i Q n ~ i ~ ~ ~ ~ n ~ w ~
u d d u dd u u d u
C) ~u~uiQn~~n~~~o~n~in~uiBnm~n~~~na~iiu~
d) ~ d u d i ~ u i $ n ~ a f ! ~8n j i ? ~
e) odniw amnuad~uiQn57cn"a~~m
f) ad; r u r r u i $ n u d a r n i u o d ~ a ~ i $ n ~ n 6 a ~2~ ~uci?rrnwwn;rururi~r<alu
n63~
R ~ I ~ ~ V ~ J U ~ ? AnswerAmy
~I<U

g) L L N ~ ~(display)
UR ~ u i % n ~ n ~ a ~ ~ d uu~ddu33$n
i ~ ~ ~ ~ d ~ i l u ~ ~ u ~
:€I5
A and
odduun3ruawiu~oFlllCountArray R i i ~ u ~ r 1 a h 6 " ~ w qLetterCount
aa 445
26 6?18diri)urfiv4ibluau~Ru~ u ~ f l ~ n f i ~ i c i i r rB
~ui$n$d~um i i~nd' uu u ~
n ~ ~ ~ o y \ r o y u & p y ~ u k : ~ ~ ~ r oR y u b u(sa6uwqns) p
\ u h n ~ ~ ooopn~g 1.2
z 0:
'?l~l4lJlv~t$NflRL$OPb ((8) O$'~%\LPIIPO?U~
R ~ ~ ~ P ~ R L U I I L ~ ~ % L ~ % \ ~ L ~ U ~ Upua L L u!6aq
, Uzm ~
I ~ O ~ ~(pl
~ P O Nby,~nq
fiLP r02(
r ~ ~ n o u ~ ~ p u b n s ~ r p ~ ~pu8
9
t ~zunr ouj6aq
~ ~pry
y neui
~ ~ ny~(3)
n~~c~
6
r~yububnzuarpln~~p1n$1
type Prime = (2, 3, 5, 7, 9, 11, 13);
I

2.3 ~~fi~ir~ni~~r:nifi~~m~~~~~uoi~I~w"
I

type (Class = (Frosh, Soph, Jr, Sf);


Y.
Ol~~tb0d~l~lY0d

(a) Ord(Succ(Pred(Soph)))
(b) Pred(pred(Jr))
i7a 3
' 2
3.1 o d r ~ w m d ~ e ~ w & o ~ ~ o m 3~i u' ik~d i i i ~ t b

for NextCh := 'A' to 'Z' do


Write(NextCh) ;
er
3.2 ado^rmiun3iun %.JIUYQJGWF)&LY I U
; (boolean expression) f i d d l d t b T]inI.ftb~~
l u " ~ l u h u r G u ~ 6(complement)
1~

(Ch >= 'a') and (Ch <= 'z')


r'u u c- er
3.3 (a) a ~ o ~ u ~ u ~ n u n < m v o ~ g d n " ~~~~t b
~~a in~~i iy~t u
b "~ i u ~ ~ c i i o : : I . f

function Hypot (XI Y : Real) : Real ;


begin {Hypot}
Hypot := Sqrt (Sqr(X) * Sqr(Y))
End; {Hypot}
(b) o ~ ~ f o u $ a i u ~~y puo it jn~ i S f i a ~ u ~ 6 A
~ i ker
l t b B L~~UWII

K W hEldfldn'.a'tbItb$7 LLdf c
6.p 4
d' d u d'd
(a) show u~rwunC;S?~
ein1drunru$iadi~d0~3i~~dwtbn~~l~e11~~1dr~~nru
waJn.rtu3udiu sumoiff L ~ D ~ ~ ~ ~ ~ ~ I ~ ~ ~ ~ & ~ ~ o I ~ R ~ L ~ P ~ ~ L

7 n n i I u u s s ~ ' n d2. ... u n ~ e~ d


(b) o ~ ~ w n ~ r o i 6 y n ~ o d ~ d 5 ~ ~ n f b l ~ ~ d ~z 1ui:
X,~Y, i f 7wd ~ o ~ c i i I
program Show:
var
W, XI Y, Z : Integer ;
procedure SumDiff (Numl, Num2 : Integer ;
var Num3, Num4 : Integer);
begin {SumDiff) \

Num3 := Numl + Num2 ;


Num4 := Numl - Num2 ;
end; { ~ u m ~ i f f )
begin {Show}
X := 9; Y := 7; z':= 3; W,:= 5;
Writeln (' X, Y Z YV ') ; WriteLn;
WriteLn('Linel :' , X : 4,Y : 4, Z : 4,W :4):
u d
uaansln 2 SumDiff(X, Y, Z, W);
WriteLn('Line2:' , X : 4, Y : 4, Z : 4, W : 4);
un~nC(
3 SumDiff(Y, XI Z. W);
WriteLn('Line3:', X : 4, Y : 4, Z : 4, W : 4);
u d
uaannn 4 SumDiff(Z, W, Y, X); I

WriteLn(Line4 :', X : 4, Y : 4, Z : 4, W : 4);


c. d
uatnnn 5 SumDiff(,Z, Z, XI Y);
WriteLn('LineS:', X : 4, Y : 4 , Z : 4, W : 4);
~nhC(
6 SumDiff(Y. Y. Y. W);
WriteLn('Line6:', X : 4, Y : 4, Y : 4, Z : 4, W : 4);
end. {Show}
y"a5
d d
a~duunxuau~iumu~n
~ m u ~ t h ~ C a i n p l (circle)
a ~ n ~.qndrocain$
u
d d a:
nnauu (square) ~u:qnAmuain;dm~nduu (triangle) ainuududds~m.ru
u d u A
~ ~ n d i w ~ ~ ~ ~ n a o n ~ c~ ~ ~ 5 uSo ~ n Taii.'?~onn~~uarwiu
I,
~ ~ o i ~ 1 ~ ~ ~ 6 ~
I~ain~dnldiinaiunuium3dn'u

You might also like