Problem-Solving and Design
Problem-Solving and Design
design
Candidates should be able to:
• show understanding that every computer system is made up of sub-
systems, which in turn are made up of further sub-systems
• use top-down design, structure diagrams, flowcharts, pseudocode,
library routines and sub-routines
• work out the purpose of a given algorithm
• explain standard methods of solution
• suggest and apply suitable test data
• understand the need for validation and verification checks to be made
on input data (validation could include range checks, length checks,
type checks and check digits)
Candidates should be able to:
• use trace tables to find the value of variables at each step in an
algorithm
• identify errors in given algorithms and suggest ways of removing
these errors
• produce an algorithm for a given problem (either in the form of
pseudocode or flowchart)
• comment on the effectiveness of a given solution
What is a computer system?
A COMPUTER SYSTEM is made up of software, data, hardware,
communications and people; each computer system can be divided up
into a set of sub-systems.
Pseudocode
PSEUDOCODE is a simple method of showing an algorithm, using English-
like words and mathematical operators that are set out to look like a
program.
Algorithms
Library routines
A LIBRARY ROUTINE is a set of programming instructions for a given
task that is already available for use. It is pre-tested and usually
performs a task that is frequently required. For example, the task ‘get
time’ in the checking-for-the-alarm time algorithm would probably be
readily available as a library routine.
Sub-routines
A SUB-ROUTINE is a set of programming instructions for a given task that
forms a subsystem, not the whole system. Sub-routines written in high-
level programming languages are called ‘procedures’ or ‘functions’
depending on how they are used.
Algorithms
A TRACE TABLE is used to record the results from each step in an
algorithm; it is used to record the value of an item (variable) each time
that it changes. This manual exercise is called a DRY RUN. A trace table
is set up with a column for each variable and a column for any output.
For example:
Test data is then used to dry run the flowchart and record the results on
the trace table.
• Test data: 9, 7, 3, 12, 6, 4, 15, 2, 8, 5
Structure of an algorithm
An algorithm must always consist of 3 parts
1. Input
The algorithm must take in some form of input for it to work.
Whenever you design an algorithm ensure you have made a provision
for some input to be entered.
2. Process
An algorithm must perform some of calculations, which could be
addition, subtraction, comparisons.
3. Output
An algorithm must produce some form of output, which can be printed
or displayed.
Flow chart symbols
Symbol Description Example
Off page connector, used to link part of a flow chart to the other part on
another page, if it fails to fit on one page
Flow charts
Activity 9.4
• Have a look at the flowchart and pseudocode on the next slide. What
is the purpose of the algorithm that they both represent?
0478_w18_qp_22-pages-10-11
Flow charts
(a) Complete the trace tables for each set of input data.
Input data: 6, 2.5, 2
Draw a line to link each term with its most appropriate description.
Homework
Term Description
Top-down design Pre-written code to include in your own program to carry out a common
task.
Structure diagram Shows the steps representing an algorithm using various shapes of boxes.
Flowchart Shows the hierarchy of the different components which make up a system.
Pseudocode Shows the values of variables as you manually test your program.
(b) Complete the trace table for the input data: 6 (2) Flag TestNum Num OUTPUT
1. Sequence
2. Selection
3. Iteration
Sequence
1. In sequence a block of statements is
executed in the order they are given and
each statement is executed once.
Structure
Start
<Statement 1>
<Statement 2>
<Statement 3>
End
Sequence
1. Example
Start
Total 0
Number1, Number 2 , Number 3, 0
INPUT Number1
INPUT Number 2
INPUT Number 3
Total Number1 + Number 2 + Number3
OUTPUT Total
End
Sequence
1. An algorithm to accept 4 numbers and
calculate the total
m
orith
Start
lg
his a
Total 0
nt t
INPUT Number1
basi m to imp
INPUT Number 2
INPUT Number 3
in vi a progra
c
OUTPUT Total
e
Writ
End
Solution
1. An program to accept 4 numbers and calculate the total
Private Sub Add4Numbers()
Dim Total as integer
Dim Number1, Number2 , Number3, Number4 as integer
Total = 0
Number1 = val(InputBox(“Enter number 1”))
Number2 = val(InputBox(“Enter number 2”))
Number3 = val(InputBox(“Enter number 3”))
Number4 = val(InputBox(“Enter number 4”))
Total = Number1 + Number2 + Number3 + Number4
MsgBox(“Total is “ & Total)
End Sub
Task
1. Write an algorithm to accept 4 numbers and calculate the
average.
90 – 100 A*
70 – 89 A
60 – 69 B
50 – 59 C
45 – 49 D
35 – 44 E
0 – 34 U
Iteration - Loop
In iteration a statement or block of statements is
executed for a number of times until a condition
becomes true or false.
Structure
START
<Statement 1>
WHILE <Condition> DO
<Statement 2>
<Statement 3>
ENDWHILE
<Statement 4>
END
Iteration
In this case Statement 1 and Statement 4 are
outside the condition so they will be executed
regardless of the condition.
Dress up
WHILE <you are hungry> DO
eat some food
drink water
ENDWHILE
Clean up
END
Iteration / loop
There are 3 types of iteration constructs
1. Loop for a specified number of times
2. Test on entry
3. Test on exit
can be executed 0 times if the condition is not true at the start of the loop
Example:
START
Dress up
WHILE <you are hungry> DO
eat some food
drink water
ENDWHILE
Clean up
END
In this case if you are not hungry at the start of the loop you will not enter the loop you go to
ENDWHILE.
Test on exit
This loop will test a condition after executing the statements in the loop at least once. With this
loop, statements in the loop will be executed at least once.
Example:
START
START Dress up
DO
Dress up eat some food
REPEAT drink water
eat some food UNTIL <you are satisfied>
drink water Clean up
END
UNTIL <you are satisfied>
Clean up
END
Theory information
Topic Pair Topic Pair
Real Time Processing Chloe and Rudairo Error handling Maryam and Stacey
Since the number of numbers to be compared is already known, we will use a FOR
… NEXT loop.
1. Declare variables – in this case 2 variables will be used:
• Number
• Largest
2. Initialise Largest to a very small number e.g. 0 (why ?)
Pseudocode to output largest out
of 100 inputs
The solution
Start Data 56,5,18,70, 30,68
DECLARE Number : INTEGER I Number Largest Output
DECLARE Largest : INTEGER
Largest 0
FOR I 1 to 100
INPUT “ENTER Number” Number
IF Number > Largest THEN
Largest Number
ENDIF
NEXT I
OUTPUT “The largest number is “ Largest What would have been the output if we had
Stop initialised our variable Largest 90.
Pseudocode to output smallest
out of 10 inputs
Task
IS Yes B A
A > 0 ? B B + A C C – 1
C A
No
IS
C <= 1 ?
OUTPUT Yes
'Exit'
No
END
(a) Rewrite the following pseudocode algorithm using a
WHILE … DO … ENDWHILE loop.
INPUT Num
FOR Counter ← 1 TO 12
Num ← Num * Counter
A[Counter] ← Num
NEXT
A one dimensional array is the simplest way of representing related data in a table format for
example cities in Zimbabwe can be represented as follow
0 1 2 3 4 5
Cities Harare Bulawayo Mutare Gweru Chitungwiza Kwekwe
Cities(0) = Harare
Cities(3) = Gweru
Cities (1+1) = ?
Arrays
Before using an array, one must consider the type of data to be stored and also the size of the
array which is determined by the amount of data to be stored. An appropriate meaningful name
related to the data to be stored should be given. In the example given in the previous slide the
name Cities was given because the array stores cities.
Usually arrays begin at element 0 so the maximum number of elements to be stored is 1 more
than the actual number.
Arrays
Declaring an array
It is possible to declare an array in some programming languages by declaring the lower limit
and the upper limit for example Cities[1:6], this forces the computer to set the lower bound as 1
and the upper bound as 6 and in such a situation you cannot add an element at position 7 or 0
because these positions are outside the bounds set in the declaration.
Research on programming languages which allow a programmer to set the bounds and those
which does not allow setting of the bounds.
Two dimensional arrays
A two dimensional array has rows and columns
For example the array
Is a 5 x 4 dimensional array
Which reads declare array called StudentMarks which is 5 x 4 and is Integer type
Two dimensional arrays
?
Array Result is a global 2D array of type STRING. It has 100
rows and 2 columns.
Sport[3] “Hockey”
Sport[4] “Tennis”
OR
For I = 1 to 4
OUTPUT “Enter sport Name”
Sport[I] INPUT Sport
Next I
Adding data to an array
There are three ways to add data to an array.
1. Add on declaration – this method is applicable when the data to be added to the
array is limited (not many records) and the added items will be added to the
array every time the array is created and will stay there.
2. Manually add after declaration – Just like in 1 (add on declaration) this method is
for arrays with few items.
3. Interactive add – A dynamic method which requires data to be entered every
time the program is run. This is suitable for simulating adding data to large
arrays e.g. an array with over 20 items.
Adding on declaration
To add on Declaration
In Visual Basic
• Declare an array StudentMarks of integer type
• add 10 arbitrary marks to the array.
• Create a loop to list the 10 marks
Using a loop to add data
Using a loop to add data to an array
In Visual Basic
• Declare an array Cars of String type
• add 5 arbitrary models to the array.
• Create a loop to list the 5 models
Task
Re-write the task in the previous slide using a REPEAT loop
Task
Re-write the task in the previous slide using a WHILE loop
Write a program to create an array called marks and assign the following marks on
declaration {67,87,23,90,65,38,73,66,82,39}
This one pass process described in steps 1 to 6 does not produce a perfect sort, the process has to be
repeated (n-1) times to fully sort the array where n is highest index number in the array
? How many passes are needed to make a perfect sort for the cities array? (containing 6 cities)
? How many passes are needed to make a perfect sort for the marks array? (Containing 10 marks)
Arrays
Swapping data elements
Like I said in the previous slide swapping data is never easy
0 1
Cities[1] Cities[0] gives Bulawayo Bulawayo
ENDIF
0 1 TEMP
Cities[0] Cities[1] gives
Bulawayo Bulawayo Harare
0 1 TEMP
Cities[1] TEMP gives
Bulawayo Harare Harare
Arrays
However for proper running of arrays we use loops to assign values and in this case the number of times a loop
is going to be executed is known so the most appropriate type of a loop to use is the FOR… NEXT loop which is
going to run (n-1) times where n is the last index number for the array.
? Why did we run the loop n-1 times (why FOR x = 0 to 4 and not FOR x = 0 to 5)
Arrays
Original Sorted
1 2 3 4 (after 1
Pass)
Harare Bulawayo Bulawayo Bulawayo Bulawayo Bulawayo
(a) Declare the arrays and assign the Names and marks on entry [4]
(c) Have a loop(s) to sort the marks and student names in descending order (a complete perfect sort)
[8]
(d) Have a loop to output the sorted names and marks (ensure a student stays with her mark) [4]