1 / 9

PROGRAM Factorial; VAR Number, Index: integer; Result: longint;

Midterm 2: problem 1. PROGRAM Factorial; VAR Number, Index: integer; Result: longint; BEGIN writeln('Enter an integer'); readln(Number); Result := 1; IF Number > 1 THEN FOR Index := 1 TO Number DO

cpitts
Télécharger la présentation

PROGRAM Factorial; VAR Number, Index: integer; Result: longint;

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Midterm 2: problem 1 PROGRAM Factorial; VAR Number, Index: integer; Result: longint; BEGIN writeln('Enter an integer'); readln(Number); Result := 1; IF Number > 1 THEN FOR Index := 1 TO Number DO Result := Result * Index; writeln ('The factorial is ', Result); END. Declaration: 2 Input: 2 Overall layout: 2 Initialization: 3 Calculation: 2 IF THEN: 2 FOR LOOP: 2 overall correct: 4 Output: 1

  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. Declarations: 3 initializations: 2 initialize Ch: 1 WHILE loop:2 CASE: 2 vowels:1 overall correctness: 6 output: 2 Overall layout: 1

  3. PROCEDURE Second_Largest (VAR Second : integer); VAR First, Current : integer; 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} initialize values: 1 overall logic of WHILE loop: 1 logic for largest value: 1 logic for second largest value: 1 If the entire program would work correctly:1

  4. Midterm 2 • 90-100 20 • 80-89 10 • 70-79 9 • 65-69 3 • 0-64 14

  5. 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.

  6. 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.

  7. 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).

  8. 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.

  9. 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).

More Related