P2 Unit 8 - Programming Concepts
P2 Unit 8 - Programming Concepts
Row
Row
For Row 1 to 3
For Col 1 to 4
print Marks [Row][Col]
Next Col
Next Row
Row
Total =0
For Row 1 to 3
For Col 1 to 5
Total = Total + Marks [Row] [ Col ]
Next Col
Next Row
Print ‘ Total of all the values is’ , Total
Total of the values in each row in a 2D array
Total= 0
For Row 1 to 3
RTotal =0
For Col 1 to 5
RTotal = RTotal + Marks [Row] [ Col ]
Next Col
Output ‘ Row total is’, Rtotal
Total = Total + RTotal
Next Row
Print ‘ Total of all the values is’ , Total
The bubble sort
• Start with the leftmost item
• Compare this item with the one next to it
• If the one next to it is less, swap the items
• Repeat for all the other items
• At the end of one pass through the list, the largest item is at
the end of the list
• Repeat the process until the items are sorted
• Suppose you have a list of numbers to be sorted:
9 5 4 15 3 8 11 2
Bubble sort – First pass
9 5 4 15 3 8 11 2 • Each item is compared
5 9 4 15 3 8 11 2 with the one on its right,
and swapped if it is
5 4 9 15 3 8 11 2
larger
5 4 9 15 3 8 11 2
• At the end of the first
5 4 9 3 15 8 11 2 pass the largest item
5 4 9 3 8 15 11 2 bubbles through to the
end of the list
5 4 9 3 8 11 15 2
• (Orange indicates
5 4 9 3 8 11 2 15
sorted items)
First pass Second pass
9 5 4 15 3 8 11 2 5 4 9 3 8 11 2 15
5 9 4 15 3 8 11 2 4 5 9 3 8 11 2 15
5 4 9 15 3 8 11 2 4 5 9 3 8 11 2 15
5 4 9 15 3 8 11 2 4 5 3 9 8 11 2 15
5 4 9 3 15 8 11 2 4 5 3 8 9 11 2 15
5 4 9 3 8 15 11 2 4 5 3 8 9 11 2 15
5 4 9 3 8 11 15 2 4 5 3 8 9 2 11 15
5 4 9 3 8 11 2 15
Second pass Third pass
5 4 9 3 8 11 2 15 4 5 3 8 9 2 11 15
4 5 9 3 8 11 2 15 4 5 3 8 9 2 11 15
4 5 9 3 8 11 2 15 4 3 5 8 9 2 11 15
4 5 3 9 8 11 2 15 4 3 5 8 9 2 11 15
4 5 3 8 9 11 2 15 4 3 5 8 9 2 11 15
4 5 3 8 9 11 2 15 4 3 5 8 2 9 11 15
4 5 3 8 9 2 11 15
Third pass Fourth pass
4 5 3 8 9 2 11 15 3 4 5 8 2 9 11 15
4 5 3 8 9 2 11 15 3 4 5 8 2 9 11 15
4 3 5 8 9 2 11 15 3 4 5 8 2 9 11 15
4 3 5 8 9 2 11 15 3 4 5 8 2 9 11 15
4 3 5 8 9 2 11 15 3 4 5 2 8 9 11 15
4 3 5 8 2 9 11 15
Fourth pass Fifth pass
3 4 5 8 2 9 11 15 3 4 5 2 8 9 11 15
3 4 5 8 2 9 11 15 3 4 5 2 8 9 11 15
3 4 5 8 2 9 11 15 3 4 5 2 8 9 11 15
3 4 5 8 2 9 11 15 3 4 2 5 8 9 11 15
3 4 5 2 8 9 11 15
Fifth pass Sixth pass
3 4 5 2 8 9 11 15 3 4 2 5 8 9 11 15
3 4 5 2 8 9 11 15 3 4 2 5 8 9 11 15
3 4 5 2 8 9 11 15 3 2 4 5 8 9 11 15
3 4 2 5 8 9 11 15
3 4 2 5 8 9 11 15 2 3 4 5 8 9 11 15
Dice ← ROUND(RANDOM()*6,0)
OUTPUT Dice
• Python:
import random
dice = random.randint(0, 6)
print(dice)
MOD and DIV
• Some languages will use MOD and DIV as library
routines.
• For instance
MOD(5,2) returns 1 // Divides 5 by 2 and return remainder
DIV(7,3) returns 2 // Divides 7 by 3 and return
quotient
DIV ( 3, 10 ) returns 0 // Divides 3 by 10 and returns quotient
MOD ( 3,10 ) returns 3 // Divides 3 by 10 and returns remainder
• In IGCSE pseudocode the MOD and DIV are operators:
5 MOD 2 evaluates to 1
7 DIV 3 evaluates to 2
Functions and Procedures
Subroutines :Functions and procedures
In contrast to library routines functions and procedures are
subroutines which programmers can code. Subroutines
allow code that you intend to use a number of times to
be grouped together under one name
• Both functions and procedures are subroutines
• Values can be passed to both procedures and functions -
parameters
• Functions returns a value after processing has taken place
• Functions and procedures are activated ( starts execution when
they are called)
• Procedure doesn’t return a value
PROCEDURE Doesn’t Return A Value
PROCEDURE greatest(a: Integer, b: Integer, c: Integer)
IF a > b AND a > c Three parameters are declared in
THEN this procedure heading
output a
IF b > a AND b > c
THEN
output b
ELSE
Output c
END PROCEDURE
Procedure is called by using the
CALL greatest( 45, 35, 40) keyword Call
Functions
Functions work just like procedures, except they return a value
FUNCTION sum(a: Integer, b:Integer) return Integer
Declare total: Integer
total ← a + b
RETURN total This is a function as it
returns a value
ENDFUNCTION
RETURN total
ENDFUNCTION
Decision –
Input / Output change flow
based on a
Process – Maths decision
operations and
assignment of variables
Sub program –
Line – shows call a different
direction of flow function or
procedure
Terminal – for
start and stop
Sub programs
• Sub programs are used when you wish to call
another procedure or function
• They use a box
with lines either side Start
findVAT(n)
• In this example,
findVAT is called INPUT total VAT n * 0.2
from the
sub program box total total + Return VAT
• The findVAT sub findVAT(total)
program then runs
OUTPUT
and returns the total
answer to where
it was called Stop
total = total + count
File Handling
Why use files , why not
variables and arrays
• Variables and arrays are deleted after program execution is complete, data in
files is permanent and it remain in the files after the program execution is
completed
• data can be used by more than one program or reused when a program is run
again
• data can be backed up or archived
• data can be transported from one place / system to another.
Three steps of Reading and writing to files
• Reading from and writing to files requires the
following steps:
• Open the file
• Read from, or write to, the file
• Close the file
OPEN marks.txt FOR READ Loop through each line in the file
OUTPUT line
ENDWHILE Close “marks.txt”
CLOSEFILE marks.txt
Creating/ open a file to write
• To create a new file use the code:
OPENFILE FileA.txt FOR WRITE
• A file needs to be opened
before it can be written to
• FileA.txt can now be written
to in the rest of the program
• When creating the file (in
IGCSE pseudocode) opening
the file in WRITE mode will
create a new file, losing any
original data if the file
already existed
Writing data to a text file
• Here is an algorithm which writes to a text file:
OPENFILE marks.txt FOR WRITE
INPUT name
INPUT mark
record name + "," + mark + "\n"
WRITEFILE marks.txt, record
CLOSEFILE marks.txt
• If the user’s name is “Sandra” and their mark is 85
the following will be added to the file:
Sandra, 85
• \n means create a new line in the file
Closing a file
• Once you have finished reading or writing to a file it
is important to close it
• This frees up any memory used by having it open
• Once closed, the file cannot be read from or written to without
opening it again
• To close a file, use:
CLOSEFILE filename
How to make program
maintainable and readable
Maintainable programs
• Programs are easy to maintain (read, understand
and make changes to) when
• They use meaningful identifiers for variables, constants,
arrays, procedures and functions
• They have useful comments that are relevant and appropriate
to each block of code
• They make use of procedures and functions to make program
modular
• Good use of indentation and whitespace, makes program
more readable