Professional Documents
Culture Documents
Problem-Solving and Design
Problem-Solving and Design
Problem-Solving and Design
• analysis
• design
• coding
• testing
The program development life cycle
Decomposition breaks down a complex problem into smaller parts, which can then
be subdivided into even smaller parts, that can be solved easily. Any daily task can
be divided into its constituent parts.
Design
The program specification from the analysis stage is used to show to how the
program should be developed. When the design stage is complete, the programmer
should know what is to be done, i.e. all the tasks that need to be completed, how
each task is to be performed and how the tasks work together. This can be formally
documented using structure charts, flowcharts and pseudocode.
The program development life cycle
Coding and iterative testing
The program or set of programs is developed. Each module of the program is
written using a suitable programming language and then tested to see if it works.
Iterative testing means that modular tests are conducted, code amended, and tests
repeated until the module performs as required.
Testing
The completed program or set of programs is run many times with different sets of
test data. This ensures that all the tasks completed work together as specified in the
program design.
Top-down design
TOP-DOWN DESIGN is the breaking down of a computer system into a
set of subsystems, then breaking each sub-system down into a set of
smaller sub-systems, until each sub-system just performs a single
action.
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
rithm
Start
algo
Total 0
his
ent t
INPUT Number1
basi m to imp
INPUT Number 2
INPUT Number 3
in vi a progra
c
OUTPUT Total
e
Writ
End
Task
1. Write an algorithm to accept 4 numbers and calculate the
average.
CASE OF <identifier>
<value 1> :
<statement1>
<statement2>
...
<value 2> :
<statement1>
<statement2>
...
...
ENDCASE
Case
• An OTHERWISE clause can be the last case:
CASE OF <identifier>
<value 1> :
<statement1>
<statement2>
...
<value 2> :
<statement1>
<statement2>
...
OTHERWISE :
<statement1>
<statement2>
...
ENDCASE
Case
Example
90 – 100 A*
70 – 89 A
60 – 69 B
50 – 59 C
45 – 49 D
35 – 44 E
0 – 34 U
Extension
80 to 100 Distinction
70 to 79 Merit
60 to 69 Credit
50 to 59 Pass
40 to 49 Resit
0 to 39 Repeat Level
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
Pseudocode to output largest out
of 3 inputs
Start
DECLARE Number1, Number2, Number3 : INTEGER
Number1, Number2, Number3 0
INPUT “Enter number 1” Number1
INPUT “Enter number 2” Number2
INPUT “Enter number 3” Number3
IF Number1 > Number2 AND Number1 > Number3 THEN
OUTPUT “Number1 is the largest”
ENDIF
IF Number2 > Number1 AND Number2 > Number3 THEN
OUTPUT “Number2 is the largest”
ENDIF
IF Number3 > Number2 AND Number3 > Number2 THEN
OUTPUT “Number1 is the largest”
ENDIF
Stop
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.
Write program / algorithm to output numbers from 1 to 10 using
a while (test on entry) loop
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
Write program / algorithm to output numbers from 1 to 10 using
a REPEAT UNTIL (test on exit) loop
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
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
?
Array Result is a global 2D array of type STRING. It has 100
rows and 2 columns.
Is a 5 x 4 dimensional array
Which reads declare array called StudentMarks which is 5 x 4 and is Integer type
Soccer
DECLARE Sport[1:4] OF STRING
Basketball
Sport[1] “Soccer” Hockey
Sport[2] “Basketball” Tennis
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
Manually adding data to
an array
Adding data manually 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
Re-write the task in the previous slide using a REPEAT loop
Task
Declare an array StudentMarks of integer type and 50 arbitrary marks.
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)
looking back
Using pseudocode,
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
To complete our sorting we use nested loops, with the outer loop defining the number of passes we are
supposed to make. For us to achieve a complete sort we have to do (n-1) passes. So the following algorithm will
input the 6 cities and then sort them.
1 2 3 4 (after 1
Pass)
Harare Bulawayo Bulawayo Bulawayo Bulawayo Bulawayo
0 1 2 3 4 0 1 2 3 4
Arundel Chisipite Petra Byo Convent
Homework
Write down the locations of these schools
0 1 2 3 4 0 1 2 3 4
0 Arund Gateway 0 Girls
el College
1 Byo Petra
1 Chisi Convent
pite
2 Falco
2 Heritage n
3 Conv 3 CBC
ent
Create the 2 arrays in VB and enter the schools. Output the schools.
Task 1
Using 2 D arrays create the following
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
Task 2
Using 2 D arrays create the following * * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Task 3
Using 2 D arrays create the following *
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Homework
Write an algorithm to create array called marks which will hold marks for 10 students. The algorithm must
(a) Declare the array [4]
(b) have a loop(s) to enter the marks [4]
(c) Have a loop(s) to sort the marks in descending order (a complete perfect sort) [8]
(d) Have a loop to output the results [4]
Task
Is a 5 x 4 dimensional array
To declare the array we will use the conventional way of declaring arrays in pseudocode and for consistency
we will stick to this declaration.
DECLARE <identifier> : ARRAY[<lbound1>:<ubound1>,<lbound2>:<ubound2>]
OF <datatype>
Which reads declare array called StudentMarks which is 5 x 4 and is of string type
Two dimensional arrays
?
Array Result is a global 2D array of type STRING. It has 100
rows and 2 columns.
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
6 6 6 6 6
7 7 7 7 7
8 8 8 8 8
34 25 85 41 14 32
23 52 14 78 52 45
63 42 15 52 63 58
74 52 52 62 85 95
75 74 85 25 36 36
25 62 85 85 41 95
14 52 62 65 25 87
95 24 25 35 62 41
Create a program code to swap the contents of column 1 with 6, 2 with 5 and 3 with
4.
String manipulation
String manipulation
- Arrays can be used together with string manipulation operations to carry out
many tasks.
(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]