1 CMPE 104 Pascal (Modified slides of Tom Rethard)
-
Upload
kathryn-bradley -
Category
Documents
-
view
219 -
download
0
Transcript of 1 CMPE 104 Pascal (Modified slides of Tom Rethard)
![Page 1: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/1.jpg)
1
CMPE 104
Pascal
(Modified slides of Tom Rethard)
![Page 2: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/2.jpg)
2
Pascal Designed by Niklaus Wirth Development started in 1968 First working compiler in 1970 Pascal-70 Report was on 29 pages
![Page 3: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/3.jpg)
3
Pascal Goals Goals
It is teaching language Reliability Simplicity Efficiency
Wirth: “the principle to include features that were well
understood, in particular by implementers, and to leave out those that were still untried and unimplemented, proved to be the most successful single guideline.”
1. The language is suitable for teaching programming in a systematic way.
2. The implementation of the language is reliable and efficient, at compile-time and run-time, on all available computers.
![Page 4: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/4.jpg)
4
Example
Program AbsMean (input, output);const Max = 900;type index = 1 .. Max;var
N: 0 .. Max;Data: array [index] of real;sum, avg, val: real;i: index;
…
![Page 5: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/5.jpg)
5
Example (con’t)beginsum := 0;readln (N);for i := 1 to N do
beginreadln (val);if val < 0 then Data[i] := valelse Data[i] := val
end;for i := 1 to N do
sum = sum + Data[i];avg := sum/N;writeln (avg);
end.
![Page 6: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/6.jpg)
6
EnumerationsTypeDayOfWeek = (Sun, Mon, Tue, Wed, Thu, Fri, Sat);Month = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);vartoday, tomorrow: DayOfWeek;begin…today := Tue;tomorrow := today + 1;…today = Jan; /* type error …
![Page 7: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/7.jpg)
7
Operations := succ pred = <> < > <= >=
![Page 8: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/8.jpg)
8
Subrange Types
var DayOfMonth 1 .. 31; Restricts the range of values for
DayOfMonth to the integer subrange of 1..31
Can also use in enumerations:Type WeekDay = Mon .. Fri;
![Page 9: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/9.jpg)
9
Sets Set of <ordinal type> (enumeration type(char,Boolean), subrange type) Var S, T: set of 1..10; S := [1, 2, 3, 5, 7]; T := [1 ..6]; If T = [1, 2, 3, 5] then …
![Page 10: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/10.jpg)
10
Set Operations = <> <= subset or equal >= But: no < or > !
![Page 11: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/11.jpg)
11
Arrays Any upper or lower bound Can also use enumeration types as
array indices Examples
var A: array [1 .. 100] of real;var HoursWorked: array [Mon .. Fri] of
0 .. 24;
![Page 12: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/12.jpg)
12
Arrays
Var day: Mon .. Fri; TotalHours: 0..120;begin
TotalHours := 0;for day := Mon to Fri do TotalHours := TotalHours +
HoursWorked[day];
![Page 13: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/13.jpg)
13
Arrays of Characters
Any finite discrete type for index
var Occur: array [char] of integer;…
Occur[ch] := Occur[ch] + 1;…
if Occur[‘e’] > Occur[‘t’] then …
![Page 14: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/14.jpg)
14
More Complex Arrays
var M: array [1..20] of
array [1 .. 100] of real;
or
var m: array [1 .. 20, 1 .. 100] of real;
![Page 15: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/15.jpg)
15
More on Arrays Need to be static, not dynamic
Must know types at compile time
Dimensions are part of the array type
Arrays are considered the same type if index types and base types both match
![Page 16: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/16.jpg)
16
Type problemstype vector = array [1 .. 100] of real;var U, V, vector;function sum (x: vector): real;
…begin … end {sum};
Can writevar W: array [1 ..75] of real;
But cannot write:Sum(W)
![Page 17: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/17.jpg)
17
Type Problems Types of W and of x are not the
same because the ranges of the indices are different!
![Page 18: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/18.jpg)
18
Record Types Heterogeneous data Multiple components Various types
![Page 19: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/19.jpg)
19
Recordstype
person = recordname: string;age: 16 .. 100;salary: 10000 .. 100000;sex: (male, female);birthdate: date;hiredate: date;
end;string = packed array [1 ..30] of char;date = record
mon: month;day: 1 ..31;year: 1900 .. 2100;
end;month = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
![Page 20: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/20.jpg)
20
Using a Record To use a record:
var newhire: person;
just like any other type
![Page 21: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/21.jpg)
21
Getting to the Components
varnewhire: person;today: date;…newhire.age := 25;newhire.sex := female;newhire.date := today;
![Page 22: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/22.jpg)
22
More Possibilities
if newhire.name[1] = ‘A’ then …
typeemployeeNum = 1000 .. 9999;
varemployees: array
[employeeNum] of person;EN: employeeNum;
![Page 23: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/23.jpg)
23
Making it Simpler
with newhirebegin
age := 25;sex := female;date := today
end;
![Page 24: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/24.jpg)
24
Storage Groupings Homogeneous
Arrays All elements are the same type Computed (dynamic) selector (subscript or
index) Heterogeneous
Records Elements (components) may be of different
types Static selector
![Page 25: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/25.jpg)
25
Variant Records Sometimes records vary from one
record type to another. Think of this as a primitive form of
subclassing
![Page 26: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/26.jpg)
26
Variant Recordstype
plane = recordflight: 0 .. 999;kind: (B727, B737, B747);status (inAir, onGround, atTerminal);altitude: 0 .. 100000;heading: 0 .. 359;arrival: time;destination: airport;location: airport;runway: runwayNumber;parked: airport;gate: 1.. 100;departure: time;
end; {plane}
![Page 27: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/27.jpg)
27
What’s Wrong? Not all data has meaning at the
same time. Can imply a plane is located at one
airport and is parked at another Violates security principle.
![Page 28: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/28.jpg)
28
Variant Recordstype
plane = recordflight: 0 .. 999;kind: (B727, B737, B747);case status: (inAir, onGround, atTerminal);inAir:(
altitude: 0 .. 100000;heading: 0 .. 359;arrival: time;destination: airport);
onGround: (location: airport;runway: runwayNumber);
atTerminal: (parked: airport;gate: 1.. 100;departure: time);
end; {plane}
![Page 29: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/29.jpg)
29
Implementation
flightkind
status
altitudeheadingarrival
destination
locationrunway
parkedgate
departure
![Page 30: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/30.jpg)
30
The Dreaded Pointer There is a problem with pointers
and strong typing! Pascal solves this problem by
typing pointers
![Page 31: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/31.jpg)
31
Typed Pointers
varp: ^real;x: real;c: char;
beginnew(p);p^ := 3.14159;c := p^; {illegal}
end
![Page 32: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/32.jpg)
32
Pointers with Records
varp: ^plane;
begin…p^.plane.parked[1] ……
end;
![Page 33: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/33.jpg)
33
Interpretations for equivalency
Structural equivalenceStructural descriptions of the types be the same
Name equivalenceNames must be same
![Page 34: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/34.jpg)
34
Structural Equivalence
varx: record id: integer; weight: real end;y: record id: integer; weight: real end;
The above are the same because their structure is the same
![Page 35: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/35.jpg)
35
But… Consider thistype
person = record id: integer; weight: real end;car = record id: integer; weight: real end;
The above are the same because their structure is the same, so:
car := person;
according to structural equivalency is legal!
![Page 36: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/36.jpg)
Name Equivalencevar
x: record id: integer; weight: real end;y: record id: integer; weight: real end;
Is actually ambiguous, Different versions of Name Equivalence Rule
differ on this example. If reinterpreted as follows, then they are different
typeT00029: record id: integer; weight: real end;T00030: record id: integer; weight: real end;
varx: T00029;y: T00030;
![Page 37: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/37.jpg)
37
Name Equivalence Issues
type age = 0 .. 150;var
n: integer;a: age;
Is n:= a legal? Pure name equivalence says no Logic says yes Revised Pascal Report says that a subrange
of a type is still that type
![Page 38: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/38.jpg)
38
Comparison Name Equivalence generally safer
More restrictive Name Equivalence is easier to
implement Simply a string comparison Structural equivalence requires a
recursive function ISO Pascal specifies name
Equivalence
![Page 39: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/39.jpg)
39
Name Structures Pascal provides six types
Constant bindings Type bindings Variable bindings Procedure and function bindings Implicit enumeration bindings Label bindings
![Page 40: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/40.jpg)
40
Constant bindings const MaxData = 100; MaxData can be used almost
anywhere All declarations Executable statements (for loops, etc.) Expressions BUT, not in other const declarations!
const MaxDataMinus1 = MaxData –1; is not allowed
![Page 41: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/41.jpg)
41
Constructors Record constructors Procedure/Function
The major scope defining construct
![Page 42: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/42.jpg)
42
Procedures
procedure <name> (<formal arguments>);
<declarations>begin
<statements>end;
![Page 43: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/43.jpg)
43
A Problemprocedure P (...);
...begin
...Q(...)...
end;procedure Q (...);
...begin
...P(...)...
end;
![Page 44: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/44.jpg)
44
A Problem Solvedprocedure Q(...) forward;procedure P (...);
...begin
...Q(...)...
end;procedure Q (...);
...begin
...P(...)...
end;
![Page 45: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/45.jpg)
45
Procedure Constructionprocedure <name> (<formal arguments>);
<label declarations><const declarations><type declarations><var declarations><procedure and function declarations>
begin<statements>
end;
![Page 46: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/46.jpg)
46
Pascal eliminates the block Simplifies name structure Complicates efficient use of
memory
![Page 47: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/47.jpg)
47
Control structures Reflects structured programming
ideas
![Page 48: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/48.jpg)
48
For Loop
for <name> := expression { to | downto } <expression> do <statement>
Bounds of the loop are computed once, at loop entry => definite iterator
![Page 49: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/49.jpg)
49
While Loop Also a “Leading Decision Indefinite
Iterator”while <condition> do <statement>
Checks at top of loop Can use “while true do....” for a
loop exiting from the middle (Mid-Decision Iterator)
![Page 50: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/50.jpg)
50
Repeat Loop Also “Trailing Decision Indefinite
Iterator”repeat <statement> until <condition>
Checks at bottom of loop
![Page 51: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/51.jpg)
51
Unlabeled Case Statement Modeled according to Fortran
computed gotocase <expression> of
<statement>,<statement>,...<statement>
end case;
![Page 52: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/52.jpg)
52
case I ofbegin ... S1 ... end;begin ... S2 ... end;
begin ... S3 ... end;begin ... S4 ... end;
end case;
No labels are provided.
![Page 53: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/53.jpg)
53
Labeled Case Statement Major contribution of Pascal
case <expression> of<case clause>;<case clause>;...<case clause>
end case;Designed by C.A. Hoare: the most important
of his many contributions to language design
![Page 54: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/54.jpg)
54
Labeled Case Statement
case I of1: begin ... S1 ... end;2: 3: begin ... S23 ... end;4: begin ... S4 ... end;end case;
Some dialects of Pascal add an otherwise case.
![Page 55: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/55.jpg)
55
Parameter Passing Passing by reference
Replaces Algol pass by name Intended to allow side effects (ie, I-O
parameter usage) Passes only the address
Passing by value Intended for input only parameters Side effects not allowed Done by copy-in
![Page 56: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/56.jpg)
56
Pass as Constant Original specification contained this
instead of pass by value Similar to C const parameter passing Allowed compiler to pass either
address or value Called procedure could not modify it Elimination encourages call by
reference
![Page 57: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/57.jpg)
57
Procedure type vector=array[1..100] of real; var A:vector … procedure P(x:vextor); \*pass by constant begin writeln(x[1]); A[1]:=0; writeln(x[1]) end; begin P(A) end;
![Page 58: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/58.jpg)
58
Pass as Constant Two orthogonal issues involved
Should copy its value or use its address?
Should be I or I-O parameter Approach violates the
Orthogonality Principle
![Page 59: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/59.jpg)
Procedure
Procedure difsq (function f:real; x:real):realbegin
difsq:= f(x*x) – f(-x*x)end
difsq(sin,theta)=sin(theta^2 ) – sin(-theta^2)
![Page 60: 1 CMPE 104 Pascal (Modified slides of Tom Rethard)](https://reader035.fdocuments.in/reader035/viewer/2022062518/56649edc5503460f94bed1df/html5/thumbnails/60.jpg)
Procedure arguments The arguments of a formal
procedure parameter shall be specified
Procedure difsq (function f(y:real):real; x:real):real