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