November30 powerpoint notes

Download Report

Transcript November30 powerpoint notes

Midterm 2: problem 1
Overall layout: 2
PROGRAM Factorial;
VAR Number, Index: integer;
Declaration: 2
Result:
longint;
BEGIN
writeln('Enter an integer');
Input: 2
readln(Number);
Initialization: 3
Result := 1;
Calculation: 2
IF Number > 1 THEN
IF THEN: 2
FOR Index := 1 TO Number DO
FOR LOOP: 2
Result := Result * Index;
overall correct: 4
writeln ('The factorial is ', Result);
Output: 1
END.
Declarations: 3
initializations: 2
Overall layout: 1
initialize Ch: 1
WHILE loop:2
CASE: 2
vowels:1
overall
correctness: 6
output: 2
PROGRAM LetterCount;
{Program counts the number of letters in a sentence}
CONST Period = '.';
VAR letters, vowels, blanks: integer;
Ch:
char;
BEGIN
letters := 0;
vowels := 0;
blanks := 0;
writeln('Write a sentence and end it with a period.');
read(Ch);
WHILE Ch <> Period DO BEGIN
CASE Ch OF
' ': inc(blanks); {inc(blank) is the same as blank := blank +1}
'a'..'z','A'..'Z': BEGIN
inc(letters);
IF CH IN ['a','e','i','o','u','A','E','I','O','U'] THEN
inc (vowels);
END;
END; {CASE}
read(Ch);
END; {WHILE}
writeln('The number of blanks is ', blanks);
writeln('The number of letters is ', letters);
writeln('The number of vowels is ', vowels);
END.
PROCEDURE Second_Largest
(VAR Second : integer);
VAR First, Current : integer;
initialize values:
1
If the
entire
program
would
work
correctly:1
overall logic of
WHILE loop: 1
logic for largest
value: 1
logic for second
largest value: 1
BEGIN
First := 0; {initialize }
Second := 0;
read (Current);
WHILE NOT (Current = 0) DO
BEGIN
IF Current > First THEN
BEGIN
Second := First;
First := Current
END
ELSE IF Current > Second THEN
Second := Current;
read(Current);
END {while}
END; {procedure second_largest}
Midterm 2
•
•
•
•
•
90-100
80-89
70-79
65-69
0-64
20
10
9
3
14
Complex data types
• Complex data types: a data type made of a
complex of smaller pieces. Pascal has four
very commonly used complex data types:
strings, array, records and objects.
• We have already covered strings, will cover
arrays in the next few calsses and will not
cover records or objects.
TYPE
• The reserved word TYPE introduces new
types in Pascal. One new type you can
define is an array.
• The format of a type definition is:
TYPE IntArray = ARRAY[1..5] OF integer;
This definition would produce a new type
‘IntArray’ which would consist of an array
of memory locations (5 of them) which
store integers.
TYPE IntArray = ARRAY[1..5] OF integer;
• TYPE: tells compiler we are defining a new type
• IntArray: The name of the new type (name must
conform to rules for naming an identifier)
• ARRAY: tells compiler the new type will be an
array
• [1..5]: 1 will be lowest subscript; 5 will be
highest. Both numbers must be same ordinal type.
(Called dimensioning an array)
• OF integer: means the array will store values of
type integer (an array can only store one type).
• Once a new type is defined, you can declare
a variable to hold the type like you do for a
simple type. For example:
VAR MyArray, YourArray:
IntArray;
would create two new identifiers (MyArray
and YourArray) both of type IntArray. Each
identifier can now hold 5 integers.
How do we access the values?
• To access the first value of MyArray, we
would write:
MyArray[1]
• This would be used as any other simple
variable would be used.
• The 1 between the brackets is called a
subscript. You can also use variable
identifiers as subscripts (this makes arrays
very useful).