Chapter 9. Algorithm Design and Problem Solving Flashcards
Comments
// This is a comment
Datatypes
- INTEGER a whole number
- REAL a number capable of containing a fractional part
- CHAR a single character
- STRING a sequence of zero or more characters
- BOOLEAN the logical values TRUE and FALSE
- DATE a valid calendar date
Literals
- Integer Written as normal in the denary system, e.g. 5, –3
- Real Always written with at least one digit on either side of the decimal point, zeros being added if
necessary, e.g. 4.7, 0.3, –4.0, 0.0 - Char A single character delimited by single quotes e.g. ꞌxꞌ, ꞌCꞌ, ꞌ@ꞌ
- String Delimited by double quotes. A string may contain no characters (i.e. the empty string)
e.g. “This is a string”, “” - Boolean TRUE, FALSE
- Date This will normally be written in the format dd/mm/yyyy. However, it is good practice to state
explicitly that this value is of data type DATE and to explain the format (as the convention for
representing dates varies across the world).
Variable declarations
DECLARE <identifier> : <data>
DECLARE Counter : INTEGER
DECLARE TotalToPay : REAL
DECLARE GameOver : BOOLEAN</data></identifier>
Constants
CONSTANT <identifier> = <value>
CONSTANT HourlyRate = 6.50
CONSTANT DefaultText = "N/A"</value></identifier>
Assignments
<identifier> ← <value>
Counter ← 0
Counter ← Counter + 1
TotalToPay ← NumberOfHours * HourlyRate
</value></identifier>
Declaring arrays
A one-dimensional array is declared as follows:
DECLARE <identifier>:ARRAY[<lower>:<upper>] OF <data>
A two-dimensional array is declared as follows:
DECLARE <identifier>:ARRAY[<lower1>:<upper1>,<lower2>:<upper2>] OF <data>
Example – array declarations
DECLARE StudentNames : ARRAY[1:30] OF STRING
DECLARE NoughtsAndCrosses : ARRAY[1:3,1:3] OF CHAR</data></upper2></lower2></upper1></lower1></identifier></data></upper></lower></identifier>
Using arrays
StudentNames[1] ← “Ali”
NoughtsAndCrosses[2,3] ← ꞌXꞌ
StudentNames[n+1] ← StudentNames[n]
FOR Index ← 1 TO 30
StudentNames[Index] ← “”
NEXT Index
Defining user-defined data types
Non-composite-Enumerated data types
TYPE <identifier> = (value1, value2, value3, ...)
TYPE Season = (Spring, Summer, Autumn, Winter)</identifier>
Pointer data types
TYPE TIntPointer = ^INTEGER
DECLARE MyPointer : TIntPointer
Composite datatype-Record
TYPE <identifier1>
DECLARE <identifier2> : <data>
DECLARE <identifier3> : <data>
...
ENDTYPE</data></identifier3></data></identifier2></identifier1>
TYPE StudentRecord
DECLARE LastName : STRING
DECLARE FirstName : STRING
DECLARE DateOfBirth : DATE
DECLARE YearGroup : INTEGER
DECLARE FormGroup : CHAR
ENDTYPE
Set
TYPE <identifier1> = SET OF <data>
DEFINE <identifier2> (value1, value2, value, … ) : <identifier1></identifier1></identifier2></data></identifier1>
TYPE LetterSet = SET OF CHAR
DEFINE Vowels (‘A’,’E’,’I’,’O’,’U’): LetterSet
Using user defined data types
DECLARE Pupil1 : StudentRecord
DECLARE Pupil2 : StudentRecord
DECLARE Form : ARRAY[1:30] OF StudentRecord
DECLARE ThisSeason : Season
DECLARE NextSeason : Season
DECLARE MyPointer : TIntPointer
Pupil1.LastName ← “Johnson”
Pupil1.Firstname ← “Leroy”
Pupil1.DateOfBirth ← 02/01/2005
Pupil1.YearGroup ← 6
Pupil1.FormGroup ← ꞌAꞌ
Pupil2 ← Pupil1
FOR Index ← 1 TO 30
Form[Index].YearGroup ← Form[Index].YearGroup + 1
NEXT Index
ThisSeason ← Spring
MyPointer ← ^ThisSeason
NextSeason ← MyPointer^ + 1
// access the value stored at the memory address
Input and output
INPUT <identifier>
OUTPUT <value(s)>
INPUT Answer
OUTPUT Score
OUTPUT "You have ", Lives, " lives left"</identifier>
Arithmetic operations
+ Addition
– Subtraction
* Multiplication
/ Division (The resulting value should be of data type REAL, even if the operands are integers.)
DIV Integer division: Used to find the quotient (integer number before the decimal point) after division.
MOD or Modulus: The remainder that is left over when one number is divided by another
Relational operations
> Greater than
< Less than
= Greater than or equal to
<= Less than or equal to
= Equal to
<> Not equal to
The result of these operations is always of data type BOOLEAN.
String functions and operations
RIGHT(ThisString : STRING, x : INTEGER) RETURNS STRING
returns rightmost x characters from ThisString
Example: RIGHT(“ABCDEFGH”, 3) returns “FGH”
LENGTH(ThisString : STRING) RETURNS INTEGER
returns the integer value representing the length of ThisString
Example: LENGTH(“Happy Days”) returns 10
MID(ThisString : STRING, x : INTEGER, y : INTEGER) RETURNS STRING
returns a string of length y starting at position x from ThisString
Example: MID(“ABCDEFGH”, 2, 3) returns “BCD”
LCASE(ThisChar : CHAR) RETURNS CHAR
returns the character value representing the lower-case equivalent of ThisChar
If ThisChar is not an upper-case alphabetic character, it is returned unchanged.
Example: LCASE(ꞌWꞌ) returns ꞌwꞌ
UCASE(ThisChar : CHAR) RETURNS CHAR
returns the character value representing the upper-case equivalent of ThisChar
If ThisChar is not a lower-case alphabetic character, it is returned unchanged.
Example: UCASE(ꞌhꞌ) returns ꞌHꞌ
In pseudocode, the operator & is used to concatenate (join) two strings.
Example: “Summer” & “ “ & “Pudding” produces “Summer Pudding”
Numeric functions
INT(x : REAL) RETURNS INTEGER
returns the integer part of x
Example: INT(27.5415) returns 27
RAND(x : INTEGER) RETURNS REAL
returns a random real number in the range 0 to x (not inclusive of x)
Example: RAND(87) may return 35.43
If statements
IF <condition> THEN
<statement(s)>
ENDIF
IF <condition> THEN
<statement(s)>
ELSE
<statement(s)>
ENDIF</condition></condition>
*There are no elseif statements you have to use case or nest if statements
CASE statements
CASE OF <identifier>
<value 1> : <statement1></statement1></identifier>
<statement2>
...
<value 2> : <statement1>
<statement2>
...
OTHERWISE : <statement1>
<statement2>
...
ENDCASE
Each value may be represented by a range, for example:
<value1> TO <value2> : <statement1>
<statement2>
INPUT Move
CASE OF Move
ꞌWꞌ : Position ← Position − 10
ꞌSꞌ : Position ← Position + 10
ꞌAꞌ : Position ← Position − 1
ꞌDꞌ : Position ← Position + 1
OTHERWISE : CALL Beep
ENDCASE
</statement2></statement1></value2></value1></statement2></statement1></statement2></statement1></statement2>
FOR loops
FOR <identifier> ← <value1> TO <value2>
<statement(s)>
NEXT <identifier></identifier></value2></value1></identifier>
FOR <identifier> ← <value1> TO <value2> STEP <increment>
<statement(s)>
NEXT <identifier></identifier></increment></value2></value1></identifier>
Total ← 0
FOR Row ← 1 TO MaxRow
RowTotal ← 0
FOR Column ← 1 TO 10
RowTotal ← RowTotal + Amount[Row, Column]
NEXT Column
OUTPUT “Total for Row “, Row, “ is “, RowTotal
Total ← Total + RowTotal
NEXT Row
OUTPUT “The grand total is “, Total
REPEAT loops
REPEAT
<statement(s)>
UNTIL <condition></condition>
REPEAT
OUTPUT “Please enter the password”
INPUT Password
UNTIL Password = “Secret”
WHILE loops
WHILE <condition>
<statement(s)>
ENDWHILE</condition>
WHILE Number > 9
Number ← Number – 9
ENDWHILE
Defining and calling procedures
PROCEDURE <identifier>(<param1> : <data>, <param2> : <data>...)
<statement(s)>
ENDPROCEDURE</data></param2></data></param1></identifier>
CALL <identifier>(value1, value2, ...)</identifier>
Defining and calling functions
FUNCTION <identifier>() RETURNS <data>
<statement(s)>
ENDFUNCTION</data></identifier>
FUNCTION <identifier>(<param1> : <data>,</data></param1></identifier>
<param2> : <data>...) RETURNS <data>
<statement(s)>
ENDFUNCTION
FUNCTION Max(Number1 : INTEGER, Number2 : INTEGER) RETURNS INTEGER
IF Number1 > Number2 THEN
RETURN Number1
ELSE
RETURN Number2
ENDIF
ENDFUNCTION
OUTPUT "Penalty Fine = ", Max(10, Distance*2)
</data></data></param2>
By value or by reference
If there are several parameters passed by the same method,
the BYVAL or BYREF keyword need not be repeated.
Example – passing parameters by reference
PROCEDURE SWAP(BYREF X : INTEGER, Y : INTEGER)
Temp ← X
X ← Y
Y ← Temp
ENDPROCEDURE
Handling text files
OPENFILE <file> FOR <file>
The file identifier may be a literal string containing the file names, or a variable of type STRING that has been
assigned the file name.
The following file modes are used:
* READ for data to be read from the file
* WRITE for data to be written to the file. A new file will be created and any existing data in the file will
be lost.
* APPEND for data to be added to the file, after any existing data.</file></file>
A file should be opened in only one mode at a time.
Data is read from the file (after the file has been opened in READ mode) using the READFILE command as
follows:
READFILE <file>, <variable>
The variable should be of data type STRING. When the command is executed, the next line of text in the file
is read and assigned to the variable.
The function EOF is used to test whether there are any more lines to be read from a given file. It is called as
follows:
EOF(<file>)
This function returns TRUE if there are no more lines to read (or if an empty file has been opened in READ
mode) and FALSE otherwise.
Data is written into the file (after the file has been opened in WRITE or APPEND mode) using the WRITEFILE
command as follows:
WRITEFILE <file>, <data>
Files should be closed when they are no longer needed using the CLOSEFILE command as follows:
CLOSEFILE <file></file></data></file></file></variable></file>