Chapter 9. Algorithm Design and Problem Solving Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Comments

A

// This is a comment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Datatypes

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Literals

A
  • 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).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Variable declarations

A

DECLARE <identifier> : <data>
DECLARE Counter : INTEGER
DECLARE TotalToPay : REAL
DECLARE GameOver : BOOLEAN</data></identifier>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Constants

A

CONSTANT <identifier> = <value>
CONSTANT HourlyRate = 6.50
CONSTANT DefaultText = "N/A"</value></identifier>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Assignments

A

<identifier> ← <value>
Counter ← 0
Counter ← Counter + 1
TotalToPay ← NumberOfHours * HourlyRate
</value></identifier>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Declaring arrays

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Using arrays

A

StudentNames[1] ← “Ali”
NoughtsAndCrosses[2,3] ← ꞌXꞌ
StudentNames[n+1] ← StudentNames[n]
FOR Index ← 1 TO 30
StudentNames[Index] ← “”
NEXT Index

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Defining user-defined data types

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Using user defined data types

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Input and output

A

INPUT <identifier>
OUTPUT <value(s)>
INPUT Answer
OUTPUT Score
OUTPUT "You have ", Lives, " lives left"</identifier>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Arithmetic operations

A

+ 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Relational operations

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

String functions and operations

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Numeric functions

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If statements

A

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

17
Q

CASE statements

A

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>

18
Q

FOR loops

A

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

19
Q

REPEAT loops

A

REPEAT
<statement(s)>
UNTIL <condition></condition>

REPEAT
OUTPUT “Please enter the password”
INPUT Password
UNTIL Password = “Secret”

20
Q

WHILE loops

A

WHILE <condition>
<statement(s)>
ENDWHILE</condition>

WHILE Number > 9
Number ← Number – 9
ENDWHILE

21
Q

Defining and calling procedures

A

PROCEDURE <identifier>(<param1> : <data>, <param2> : <data>...)
<statement(s)>
ENDPROCEDURE</data></param2></data></param1></identifier>

CALL <identifier>(value1, value2, ...)</identifier>

22
Q

Defining and calling functions

A

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>

23
Q

By value or by reference

A

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

24
Q

Handling text files

A

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>

25
Q

Handling random files

A

Random files are opened using the RANDOM file mode as follows:
OPENFILE <file> FOR RANDOM
As with text files, the file identifier will normally be the name of the file.
The SEEK command moves the file pointer to a given location:
SEEK <file>, <address>
The address should be an expression that evaluates to an integer which indicates the location of a record to be
read or written. This is usually the number of records from the beginning of the file. It is good practice to explain
how the addresses are computed.
The command GETRECORD should be used to read the record at the file pointer:
GETRECORD <file>, <variable>
When this command is executed, the record that is read is assigned to the variable which must be of the
appropriate data type for that record (usually a user-defined type).
The command PUTRECORD is used to write a record into the file at the file pointer:
PUTRECORD <file>, <variable>
When this command is executed, the data in the variable is inserted into the record at the file pointer. Any data
that was previously at this location will be replaced.</variable></file></variable></file></file></file>

DECLARE Pupil : Student
DECLARE NewPupil : Student
DECLARE Position : INTEGER
NewPupil.LastName ← “Johnson”
NewPupil.Firstname ← “Leroy”
NewPupil.DateOfBirth ← 02/01/2005
NewPupil.YearGroup ← 6
NewPupil.FormGroup ← ꞌAꞌ
OPENFILE “StudentFile.Dat” FOR RANDOM
FOR Position ← 20 TO 10 STEP -1
SEEK “StudentFile.Dat”, Position
GETRECORD “StudentFile.Dat”, Pupil
SEEK “StudentFile.Dat”, Position + 1
PUTRECORD “StudentFile.Dat”, Pupil
NEXT Position
SEEK “StudentFile.Dat”, 10
PUTRECORD “StudentFile.Dat”, NewPupil
CLOSEFILE “StudentFile.dat”

26
Q

Methods and properties

A

PRIVATE Attempts : INTEGER
Attempts ← 3
PUBLIC PROCEDURE SetAttempts(Number : INTEGER)
Attempts ← Number
ENDPROCEDURE
PRIVATE FUNCTION GetAttempts() RETURNS INTEGER
RETURN Attempts
ENDFUNCTION
Methods will be called using object methods, for example:
Player.SetAttempts(5)
OUTPUT Player.GetAttempts()

27
Q

Constructors and inheritance

A

Constructors will be procedures with the name NEW.
CLASS Pet
PRIVATE Name : STRING
PUBLIC PROCEDURE NEW(GivenName : STRING)
Name ← GivenName
ENDPROCEDURE
ENDCLASS
Inheritance is denoted by the INHERITS keyword; superclass/parent class methods will be called using the
keyword SUPER, for example:
CLASS Cat INHERITS Pet
PRIVATE Breed: INTEGER
PUBLIC PROCEDURE NEW(GivenName : STRING, GivenBreed : STRING)
SUPER.NEW(GivenName)
Breed ← GivenBreed
ENDPROCEDURE
ENDCLASS

To create an object, the following format is used:

<object> ← NEW <class>(<param1>, <param2> ...)
For example:
MyCat ← NEW Cat("Kitty", "Shorthaired")
</param2></param1></class></object>