Problem-Solving and Design AS - 1
Problem-Solving and Design AS - 1
design
Computational thinking & problem solving
Abstraction
We have used simple data types such as integer, character and Boolean. The
string data type is a composite type:
a sequence of characters. When we have groups of data items we used one-
dimensional (1D) arrays to represent linear lists and two-dimensional (2D)
arrays to represent tables or matrices.
Data modelling
Algorithm design
Algorithm design involves developing step-by-step finite set of instructions to
solve a problem
A
• Allows the subroutine code to be called from many/multiple places
• Subroutine code may be (independently) tested and debugged
• If the subroutine task changes the change needs to be made only once
• Reduces unnecessary duplication / program lines
• Enables sharing of development between programmers
Top-down design- Alarm app for a smart phone
• Consider the alarm app computer system for a smart phone. This
could be divided into three sub-systems,
• setting the alarm,
• checking for the alarm time,
• sounding the alarm.
• These sub-systems could then be further sub-divided; the structure
diagram makes the process clearer.
Top-down design- Alarm app for a smart phone
Flow chart
Pseudo code
Algorithms
An algorithm is a sequence of steps which when followed mechanically
will lead to a solution of a problem.
Flowcharts
A FLOWCHART shows diagrammatically the steps required for a task (sub-
system) and the order that they are to be performed. These steps
together with the order are called an ALGORITHM.
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?
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 1>
<Statement 1>
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.
A variable can be defined as an identifier or a named location which stores values which will/can change during the
execution of a program.
Notice that the name given to the constant is self-explanatory. We could have just called it Constant1. However, by giving it
a meaningful name it makes the code easier to work with as the program gets bigger. It also makes it easier for anyone else
that looks at the code, to work out what the program is doing.
Advantages
• It makes it easier to find and correct errors/bugs in code. This is called debugging.
• There are many occasions where there are several programmers working on the same program at the same time, so
having a sensible naming convention makes it easier for everyone to understand.
• It will be easier to update the code later on when further versions of the program are created.
The value of variables can change as a program is being run. For example, the same conversion program will require the
user to type in the number of miles they want to convert. This number will probably be different each time the user enters
data. Therefore, you need to have a variable that you could call NumberOfMiles.
A variable can be defined as an identifier or a named location which stores values which will/can change during the
execution of a program.
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.
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
Enables the programmer to identify errors before moving on with the code .
Useful features of IDE which help when coding
Highlighting undeclared variables // incorrect variable usage...
Parameter checking
Type checking
Text editor (for example a function such as copy & paste)
Built-in (library) functions
Identifier table
The value cannot accidentally get changed and as such will remain the
same throughout the program. When used in different places it will remain
the same minimizing errors.
When there is need for the constant to be changed a change to the value
requires changing in one place only.
You don’t have to repeatedly write out the same value throughout the
program.
Breakpoints
Run the code to a set point to find error
Variable watch
Check the contents of variables at specific points in the program
Stepping
Execute the code line by line
Procedure – Triangle
Function - Circle
Procedure - Rectangle
Introduction to modules
FUNCTION Circle (ByRef Radius : INTEGER) RETURNS REAL
CONSTANT Pi : REAL 3.14
DECLARE Area : REAL
Area Pi * Radius ^ 2
RETURN Area
ENDFUNCTION
Homework
Write a module to solve the quadratic equation given the values for A,B
an C.
Test your program with x2 – 5x + 6 = 0 ( 3 and 2 )
5x2 + 6x + 1 = 0
Those who are done can implement the number guessing game as a
programRWorked example 12.07 page 195 from the textbook-0.2 or -1)
Next up
Flowcharts
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
Call a function, can be used for input where the function will pass value or
reference to the calling program
Mark GetMark(StMark)
Tasks
FOR Index 1 TO 26
CharCount[Index] 0
FOR Index 1 TO 26
CharCount[Index] 0
A program will:
• input 50 unique integer values
• output the largest value
• output the average of the values excluding the largest value.
Draw a program flowchart to represent the algorithm.
Variable declarations are not required.
It is not necessary to check that each input value is unique. ( -0.2 or -1)