0% found this document useful (0 votes)
11 views11 pages

Guide

The document discusses pseudocode notation that is approved for use in IB Computer Science examinations. It provides examples of standard data structures and methods like arrays, strings, and collections that may be used in exam questions without explanation. It also includes examples of pseudocode code.

Uploaded by

Shahzaib Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views11 pages

Guide

The document discusses pseudocode notation that is approved for use in IB Computer Science examinations. It provides examples of standard data structures and methods like arrays, strings, and collections that may be used in exam questions without explanation. It also includes examples of pseudocode code.

Uploaded by

Shahzaib Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Diploma Programme

Approved notation for developing


pseudocode

© International Baccalaureate Organization 2012


Approved notation for developing pseudocode

When developing pseudocode teachers must use the symbols below, which are those used in
mathematics.

This information should be distributed to candidates as close as possible to the commencement of teaching
of the course. This notation sheet will be available to candidates during the external examinations.

Conventions Variable names are all capitals, for example, CITY


Pseudocode keywords are lower case, for example, loop, if …
Method names are mixed case, for example, getRecord
Methods are invoked using the “dot notation” used in Java, C++, C#, and
similar languages, for example, BIGARRAY.binarySearch( 27 )
Variable names These will be provided and comments // used, for example:
N = 5 // the number of items in the array
SCOREHISTORY,getExam( NUM ) // get the student’s score on exam NUM
Assigning a value to a Values will be assigned using = , for example:
variable
N = 5 // indicates the array has 5 data items
VALUE[0] = 7 // assigns the first data item in the array a value of 7
Output of information Output—this term is sufficient to indicate the data is output to a printer,
screen, for example:
output COUNT // display the count on the screen

Symbol Definition Examples


= is equal to X = 4, X = K If X = 4
> is greater than X>4 if X > 4 then
is greater than or equal
>= X >= 6 loop while X >= 6
to
< is less than VALUE[Y] < 7 loop until VALUE[Y] < 7
<= is less than or equal to VALUE[] <=12 if VALUE[Y] <= 12 then
≠ not equal to X ≠ 4, X ≠ K
AND logical AND A AND B if X < 7 AND Y > 2 then
OR logical OR A OR B if X < 7 OR Y > 2 then
NOT logical NOT NOT A if NOT X = 7 then
mod modulo 15 mod 7 = 1 if VALUE[Y] mod 7 = 0 then
div integer part of quotient 15 div 7 = 2 if VALUE[Y] div 7 = 2 then
Operation Flowchart example Pseudocode example
sequential
operations
perform task1

perform task1
perform task2 perform task2

conditional
operations NO
MAX > 0?

if MAX > 0 then


YES
output “positive”
output “positive” output “not positive” else
output “not positive”
end if

while-loop

YES loop while COUNT < 15


COUNT < 15? COUNT = COUNT + 1
COUNT = COUNT + 1
end loop
NO
from/to-loop

COUNT = 0

loop COUNT from 0 to 5


SUM = SUM + COUNT
SUM = SUM + COUNT
end loop

COUNT = COUNT +1

NO
COUNT > 5?

YES
Computer Science
First Exams 2014

Pseudocode in Examinations
• Standard Data Structures
• Examples of Pseudocode

Candidates are NOT allowed a copy of this document during their examinations.
Introduction

The purpose of this document is to show the standard data structures and methods which
may be displayed in IB Diploma Programme Computer Science examinations.

These methods, in their pseudocode format, may be used without explanation or


clarification in examination questions. Teachers should ensure that candidates will be able
to interpret these methods when presented as part of an examination question.

This list is not exhaustive. Other methods may be used in examination questions;
however any additional methods will be fully explained within the examination.

This information is supported by a series of pseudocode examples, demonstrating how the


pseudocode will be formatted and displayed during IB examinations.

Where answers are to be written in pseudocode, the examiners will be looking for clear
algorithmic thinking to be demonstrated. In examinations, this type of question will be
written in the approved notation, so a familiarity with it is expected.

It is accepted that under exam conditions candidates may, in their solutions, use
pseudocode similar to a programming language with which they are familiar. This is
acceptable. The markscheme will be written using the approved notation. Provided the
examiners can see the logic in the candidate’s response, regardless of language, it will be
credited.

No marks will be withheld for syntax errors.

Candidates are not permitted to invoke a powerful command if it trivializes the question.
For example, in response to a question asking the candidate to “Construct an algorithm to
arrange the elements of an array in increasing order”, the pseudocode “sort the array” is
inappropriate and will receive no marks.
Higher Level and Standard Level

Arrays
An array is an indexed and ordered set of elements. Unless specifically defined in the question,
the index of the first element in an array is 0.

NAMES[0] // The first element in the array NAMES

Strings

A string can contain a set of characters, or can be empty. Strings can be used like any other
variable.

MYWORD = "This is a string"


if MYWORD = "the" then
output MYWORD
end if

Strings should be regarded as a class of objects. However no methods belonging to that class are
part of this standard. Instead, if a specialized method such as charAt() or substring() is to
be used in an examination, it will be fully specified as part of the question in which it is needed.
Collections
Collections store a set of elements. The elements may be of any type (numbers, objects, arrays,
Strings, etc.).

A collection provides a mechanism to iterate through all of the elements that it contains. The
following code is guaranteed to retrieve each item in the collection exactly once.

// STUFF is a collection that already exists


STUFF.resetNext()
loop while STUFF.hasNext()
ITEM = STUFF.getNext()
// process ITEM in whatever way is needed
end loop

Method Brief description Example: Comment


name HOT, a collection of
temperatures
addItem() Add item HOT.addItem(42) Adds an element that
HOT.addItem("chile") contains the argument,
whether it is a value,
String, object, etc.
getNext() Get the next item TEMP = HOT.getNext() getNext() will return
the first item in the
collection when it is first
called.
Note: getNext() does
not remove the item from
the collection.
resetNext() Go back to the start HOT.resetNext() Restarts the iteration
of the collection HOT.getNext() through the collection.
The two lines shown will
retrieve the first item in
the collection.
hasNext() Test: has next item if HOT.hasNext() then Returns TRUE if there are
one or more elements in
the collection that have
not been accessed by the
present iteration: The
next use of getNext()
will return a valid element.
isEmpty() Test: collection is if HOT.isEmpty() then Returns TRUE if the
empty collection does not
contain any elements.
Higher Level Only

Stacks
A stack stores a set of elements in a particular order: Items are retrieved in the reverse order in
which they are inserted (Last-in, First-out). The elements may be of any type (numbers, objects,
arrays, Strings, etc.).

Method Brief description Example: Comment


name OPS, a stack of integers
push() Push an item onto OPS.push(42) Adds an element that
the stack contains the argument,
whether it is a value,
String, object, etc. to the
top of the stack.
pop() Pop an item off the NUM = OPS.pop() Removes and returns
stack the item on the top of
the stack.
isEmpty() Test: stack if OPS.isEmpty() then Returns TRUE if the
contains no stack does not contain
elements any elements.

Queues
A queue stores a set of elements in a particular order: Items are retrieved in the order in which they
are inserted (First-in, First-out). The elements may be of any type (numbers, objects, arrays,
Strings, etc.).

Method Brief description Example: Comment


name WAIT, a queue of Strings
enqueue() Put an item into the WAIT.enqueue("Mary") Adds an element that
end of the queue contains the argument,
whether it is a value,
String, object, etc. to
the end of the queue.
dequeue() Remove an item CLIENT = WAIT.dequeue() Removes and returns
from front of the the item at the front of
queue the queue.
isEmpty() Test: queue if WAIT.isEmpty() then Returns TRUE if the
contains no queue does not
elements contain any elements.
Examples of Pseudocode
AVERAGING AN ARRAY

The array STOCK contains a list of 1000 whole numbers (integers). The following pseudocode
presents an algorithm that will count how many of these numbers are non-zero, adds up all those
numbers and then prints the average of all the non-zero numbers (divides by COUNT rather than
dividing by 1000).

COUNT = 0
TOTAL = 0

loop N from 0 to 999


if STOCK[N] > 0 then
COUNT = COUNT + 1
TOTAL = TOTAL + STOCK[N]
end if
end loop

if NOT COUNT = 0 then


AVERAGE = TOTAL / COUNT
output "Average = " , AVERAGE
else
output "There are no non-zero values"
end if

COPYING FROM A COLLECTION INTO AN ARRAY

The following pseudocode presents an algorithm that reads all the names from a collection,
NAMES, and copies them into an array, LIST, but eliminates any duplicates. That means each
name is checked against the names that are already in the array. The collection and the array are
passed as parameters to the method.

COUNT = 0 // number of names currently in LIST

loop while NAMES.hasNext()

DATA = NAMES.getNext()

FOUND = false
loop POS from 0 to COUNT-1
if DATA = LIST[POS] then
FOUND = true
end if
end loop

if FOUND = false then


LIST[COUNT] = DATA
COUNT = COUNT + 1
end if
end loop
FACTORS

The following pseudocode presents an algorithm that will print all the factors of an integer. It prints
two factors at a time, stopping at the square root. It also counts and displays the total number of
factors.

// recall that
// 30 div 7 = 4
// 30 mod 7 = 2

NUM = 140 // code will print all factors of this number


F = 1
FACTORS = 0

loop until F*F > NUM //code will loop until F*F is greater than NUM

if NUM mod F = 0 then

D = NUM div F
output NUM , " = " , F , "*" , D

if F = 1 then
FACTORS = FACTORS + 0
else if F = D then
FACTORS = FACTORS + 1
else
FACTORS = FACTORS + 2
end if

end if

F = F + 1

end loop

output NUM , " has " , FACTORS , " factors "


COPYING A COLLECTION INTO AN ARRAY IN REVERSE

The following pseudocode presents an algorithm that will read all the names from a collection,
SURVEY, and then copy these names into an array, MYARRAY, in reverse order.

// MYSTACK is a stack, initially empty

COUNT = 0 // number of names

loop while SURVEY.hasNext()


MYSTACK.push( SURVEY.getNext() )
COUNT = COUNT + 1
end loop

// Fill the array, MYARRAY, with the names in the stack

loop POS from 0 to COUNT-1


MYARRAY[POS] = MYSTACK.pop()
end loop

You might also like