Unit 7-AlgodesignAndProbsolv
Unit 7-AlgodesignAndProbsolv
•Speeds up development process as more than one person can work to solve the
problem. This speeds up the development of the program.
•Different designers can work on modules individually then bring the sub-
problems together at the end.
•Each programmer can be given a module that is suitable for their area of
expertise. For example, an expert in interface design could be given the
interface modules and an expert in file-handling could be given the file-handling
modules.
•As each module is a shorter program they are easier to write, test and de-bug
than the full system.
Smartphone System Top-Down
design
The complex problem of the smartphone system was broken down
into sub-problems:
• a sub-system to manage inputs and outputs via the touch screen
• a sub-system to manage file storage
• a sub-system to manage radio transmissions.
Computer System :Smart Phone
These sub-systems also have sub-systems of their own:
• The input/output sub-system has sub-systems to update the display and
detect user input.
• The file handling sub-system has sub-systems to write data to storage and
read data from storage.
• The radio transmissions sub-system has sub-systems to manage outgoing
and incoming transmissions.
Stepwise Refinement
Example
Procedure Error_msg()
Print “Invalid Data: Enter Again”
Return
Function
Function CalculateSum(a,b)
Declare c:integer
c=a+b
Return(c)
Decomposing a problem
• The structure diagram shows how different modules relate to each other.
This can be important in helping to visualise how a program works.
• Modules can be re-used in different programs that require the same
structure.
• Structure diagrams make it easier for another designer to understand the
logic of an algorithm.
Structure Diagram : Smart Phone
Smart Phone Sub System
• inputs and outputs via the touch screen
• file storage
• radio transmissions.
This can be represented diagrammatically as a
structure diagram (also known as a top-down
diagram). A structure diagram shows how each
system (problem) and sub-system (sub-problem)
is broken down from top to bottom. Each box is
referred to as a module.
Structure Diagram : Smart
Phone
Example Structure Diagram:
Alarm App For A Smart Phone
Alarm app
Sound
Set alarm Check time
Alarm
Disadvantages Advantages
• They can take a lot of time to • The sequence of steps can easily seen.
produce. • Paths through the algorithm can be
easily followed.
• Changes to the algorithm mean
sections of the flowchart have to • The logic of algorithm can often be
be re-drawn understood better when represented.
• It helps others to understand how a
• Large flowcharts can become program works.
extremely complicated and
difficult to follow
Flowchart : Example
Flowchart describing
the procedure
involved in preparing
a set of envelopes for
posting.
Tasks of Drawing Flowcharts
• Draw a flowchart that will input two numbers X and Y, calculates the
sum of X and Y in Z, and display the value of Z.
• Draw a flowchart that will input two numbers in Num1 and Num2,
Compares the two numbers and outputs the bigger number.
• Draw a flowchart that will print “Hello World” 10 times.
Flow charting guidelines
Requirements should be listed in logical order.
Only one flow line should come from a symbol, unless it is a decision
symbol.
Only one flow line should enter a decision symbol, but two or three flow
lines, one for each possible answer, may leave the decision symbol.
the flowchart should has a logical start and finish. It is useful to test if a
flowchart is correct by working through it with simple test data.
Drawing Flowcharts : Tasks
Draw a flowchart that will input radius (r) and find the area of a circle(𝐴 =
𝜋𝑟 2 ) and print A.
Draw a flowchart that will input length (L) and Breath(B) of a rectangle and
calculate Area (A=L*B). And Print A
Draw a flowchart that will print first ten odd numbers
Draw a flowchart that will input a number in Num and prints its table
Draw a flowchart that will input a 10 numbers in Num and prints their sum
Draw a flowchart that will input some numbers . If a number is >0 then it
prints “positive” if it is <0 then print “Negative”. If user enter Zero(0) then it
will end the program
Introduction
34
Variables and Constants
35
Naming a variable
Variables can be given almost any name, but sensible names should be
given that are connected to the data that will be stored in
The names must not include spaces.
Variables Examples
Variables
A variable will be created and
assigned a value at a certain point
in a program. This means the
variable can then be used
repeatedly throughout the
program, even changing its value
if it is reassigned a new one
38
Constants
39
Difference between a Variable
and a Constant
40
Data types
44
What is a Pseudocode?
Pseudocode and flowcharts are tools that a programmer may use to help
design a program, or understand one that already exists.
Pseudocode is a method of describing the design of a system in words
It is a list of instructions that show how the program will work.
It uses words similar to those found in programming languages , but not
in actual strict rules of structure and Syntax.
Syntax - the rules
of a Programming
language
45
Pseudocode
Input
Algorithm Num1,Num
2
GET
OBTAIN
READ
Output Pseudocode Terms
The variable on the left of the is assigned the value of the
expression on the right. For assignment either or = sign is used.
For example cost 10 or cost =10.
COMPUTE, CALCULATE, DETERMINE and = are use to
carry out calculations.
SET and INIT are used to initialize variables.
INCREMENT is used to increment a variable.
Assignment
The variable on the left of the is assigned the value of the
expression on the right. For assignment either or = sign is used.
For example cost 10 or cost =10.
COMPUTE, CALCULATE, DETERMINE and = are use to
carry out calculations.
SET and INIT are used to initialize variables.
INCREMENT is used to increment a variable.
Mathematical Operators
Symbol Action
+ Plus
- Minus
* Multiply
/ Divide
^ Exponent
PSEUDOCODE Assignment
Cost 10
Price Cost * 2
Tax Price * 0.12
SellingPrice Price + Tax
Gender “M”
Chosen False
55
Activity 7.3 pg.266
56
Pseudocode writing Tasks
Q 1: Write pseudocode that takes hours(H) and minutes(M) as input, and calculates
the total number of minutes(TOTMINS) and outputs total number of minutes.
Q 2 : Write pseudocode that will
Input two angles(ANG1 and ANG2)
Finds and prints the third angle(ANG3) of a triangle
Using the formula
ANG3=180-(ANG1+ANG2)
Q 3 : Write pseudocode that will input temperature in centigrade (C) and converts
to Fahrenheit and prints F, using the following formula
F=1.8*C+32
57
Comparison operators
58
59
Examples of conditions
67
Nested IF-Tasks
Q 3. Write the Python program that will input the percentage marks
and print the student’s grade using the for criteria
A 80 and above
B 70 to 79
C 60 to 69
D 50 to 59
E 40 to 49
U below 40
68
CASE .. OF .. OTHERWISE ..
ENDCASE
71
CASE .. OF .. OTHERWISE .. ENDCASE
VISUAL BASIC
INPUT DayNumber
CASE DayNumber OF
1: PRINT “Monday”
2: PRINT “Tuesday”
3: PRINT “Wednesday”
4: PRINT “Thursday”
5: PRINT “Friday”
6: PRINT “Saturday”
7: PRINT “Sunday”
OTHERWISE
PRINT “Invaild number”
ENDCASE
CASE .. OF .. OTHERWISE ..
ENDCASE : Q1
A 75 – 100
B 65 – 74
C 55 – 64
D 45 -54
E 35 – 44
U BELOW 35
73
C.W
74
CASE .. OF .. OTHERWISE ..
ENDCASE – Q 3
75
Q 4 2210/23/O/N/19
Q The following pseudocode algorithm uses nested IF statements.
IF Response = 1 THEN
X =X + Y
ELSE
IF Response = 2 THEN
X =X – Y
ELSE
IF Response = 3 THEN
X =X * Y
ELSE
IF Response = 4 THEN
X= X / Y
ELSE
OUTPUT "No response"
ENDIF
ENDIF
ENDIF
ENDIF
(a) Name the type of statement demonstrated by the use of IF … THEN … ELSE … ENDIF [1]
(b) Re-write the pseudocode algorithm using a CASE statement.[4]
Solution
Iteration is the term given to the repetition of a block of statements (code) within a
computer program.
There are 3 types of iteration Iteration/Loops
FOR .. TO .. NEXT
{Counter controlled loop}
REPEAT .. UNTIL (Post test)
{Condition controlled loop}
WHILE .. DO .. ENDWHILE(Pre test)
{Condition controlled loop}
When to use a loop ?
80
FOR…… NEXT Loop
81
FOR .. TO .. NEXT(count-
controlled loop
FOR loop is used when the loop is to be repeated a For COUNT = 1 to 10
fixed number of times. Example : Add 10
numbers together Input NUM
TOTAL = TOTAL +
NUM Next
Output TOTAL
Example FOR…… NEXT Loop
83
Example FOR…… NEXT Loop
ALGORITHM:
FOR NUMBER 1 to 10
Print 13 x NUMBER
NEXT
84
Use of Step Key Word
1. Write an algorithm using pseudocode and flowchart, that will print first 10
consecutive numbers.
2. Write an algorithm using pseudocode and flowchart, that will print first 10
Odd numbers.
3. Write an algorithm using pseudocode and flowchart, that will print first 10
Even numbers.
4. Write an algorithm using pseudocode and flowchart, that will print
numbers from 20 to 10 in reverse order
86
Tasks
5. Write an algorithm, using pseudocode which inputs 10 numbers and outputs how many of the
numbers were > 100
6. Write an algorithm using pseudocode, that will input 10 numbers and only finds the largest
number.
7. Write an algorithm using pseudocode, that will input 10 numbers and only finds the smallest
number.
8. Write an algorithm using pseudocode and flowchart, that will input 5 numbers and calculates
their total.
87
REPEAT…… UNTIL
Loop(Condition Controlled Loop
88
REPEAT .. UNTIL Loop
.
1. START
2. NUM = 10
3. REPEAT
4. PRINT NUM
5. NUM=NUM+ 1
6. UNTIL NUM>20
7. END
92
Task 4
Small =99999
Repeat
Input Num
if Num<Small AND Num>=0 then
Small =Num
end if
Until Num <0
Print Small
93
Activity
94
WHILE… DO…ENDWHILE Loop
(Condition Controlled Loop
95
WHILE… DO…ENDWHILE Loop
Flowchart 96
WHILE .. DO .. ENDWHILE Loop
.
Example: Add 10 numbers together
Count = 1
While count<= 10
Input NUM
TOTAL= TOTAL + NUM
COUNT = COUNT + 1
End While
Output TOTAL
Example WHILE…… DO Loop
98
Task 1
99
Task 2
100
Wrap Up Activity
Do While – End
While
Repeat - Until
FOR…… NEXT Loop Key Features
Initial and final values of counter are specified in the beginning of loop
The commands to be repeated are sandwiched between the FOR and NEXT
103
Repeat –Until Key features
It is a Condition Controlled Loop
It can also be used when we don’t know how many times the loop
is to be performed.
The Loop is ended when certain condition is true
104
Therefore REPEAT LOOP has to be performed at least once
WHILE… DO…ENDWHILE Loop
It can also be used when we don’t know how many times the loop is to be
performed.
Loop is executed only when condition is true
End Statements
While End While
Repeat Repeat
Until Statements
Until <condition>
Examples Of Three Loops
When to use a Loops?
111
114
Homework
116
Example 1: Output an alarm sound
117
• Waiting 10 seconds,
• Getting the current time,
• Checking the current
time with the alarm
time, and
• Outputting the alarm
The purpose of the sound when the times
following pseudocode is to match
output the alarm sound at
the appropriate time.
The processes are:
Activity 7.6 : Page 271
118
7.4 Standard methods of solution
119
The ability to repeat existing methods is very important in the design of
algorithms; when an algorithm is turned into a program the same
methods maybe repeated many thousands of times.
Standard methods used in algorithms:
Totalling
Counting
Finding maximum, minimum, and average (mean) values
Searching using a linear search
Sorting using a bubble sort.
7.4.1 Totalling
Totalling means keeping a total that values are
added to
126
7.5.1 Validation
• Range checks
• Length checks
• Format checks
• Limit Check
• Type checks
• Character checks
• Consistency Check
• Presence check
• Check digit
Type Check
A Type Check ensures that the correct type of
data is entered into a field. It rejects characters or
symbols entered that are inconsistent with the
type of data.
A type check checks that the data entered is of a
given data type, for example, that the number of
brothers or sisters would be an integer (whole
number).
Example
The characters ‘0’ to ‘9’and ‘.’will be acceptable
for a decimal number, but alphabets and symbols
will not be.
Range Check
A Presence Check ensure that some data has been entered and the
value has not been left blank, for example, an email address for an online
transaction must be completed..
Example
In online form you have to fill the field marked with *. The forms will
not be submitted if you leave any of these fields empty.
Consistency Check
137
Verification
Verification is checking that data has been accurately copied onto the
computer or transferred from one part of the computer to another.
Verification methods include:
1. Double entry : data entered twice by different operators
2. Screen / visual check: after data entry data is rechecked from screen
before continuing
3. Parity check
4. Checksum
138
Double Entry
For double entry the data is entered twice, sometimes by
different operators.
The computer system compares both entries and if they
are different outputs an error message requesting that the
data is entered again.
Screen/Visual check
142
Match
143
Test Data
145
Types Test Data
146
Types Test Data
147
Testing of Data
148
Testing
149
7.7 Trace tables to document dry run
of algorithms
150
Trace Table
Trace tables and test data can be used to identify and correct
errors.
7.9 Writing and amending
algorithms
There are a number of stages when producing an algorithm for a given
problem:
1. Problem Definition
2. Breaking Down of Problem
3. Input, Output, Process and Storage
4. Designing the Structure of Algorithm
5. Decision on Construction of the Algorithm
6. Construction of the Algorithm
7. Dry running of the Algorithm
8. Removing Errors from algorithms
•Make sure that the problem is clearly specified – the purpose of the
Problem Definition algorithm and the tasks to be completed by the algorithm
Input, Output, Process and •Decide on how any data is to be obtained and stored, what is
going to happen to the data and how any results are going to be
Storage displayed.
Decision on Construction of the •construct your algorithm, either using a flowchart or pseudocode
Algorithm
Decision on
• construct your algorithm, either using a flowchart
Construction of or pseudocode
the Algorithm
• Use several sets of test data (Normal, Abnormal
Dry running of and Boundary) to dry run your algorithm and show
the Algorithm the results in trace tables, to enable you to find
any errors
Removing Errors • If any errors are found, correct them and repeat
the process until you think that your algorithm
from algorithms works perfectly.
Homework :
Act 7.20 pg 292
Q 2,3
Example 1: Writing algorithms in
pseudocode
1-inputs – the data used by the system that needs to be entered while the
system is active
2- processes – the tasks that need to be performed using the input data and any
other previously stored data
3- outputs – information that needs to be displayed or printed for the users of
the system
4- storage – data that needs to be stored in files on an appropriate medium for
use in the future.
Exam-Style Questions pg. 295-298
Q 1 A solution to a problem is decomposed into its component parts.
Name and describe the component parts. [8]
1-inputs – the data used by the system that needs to be entered while the
system is active
2- processes – the tasks that need to be performed using the input data and any
other previously stored data
3- outputs – information that needs to be displayed or printed for the users of
the system
4- storage – data that needs to be stored in files on an appropriate medium for
use in the future.
Q 3. A phone app is being developed to split the cost of a restaurant bill between a given number of people
It is being designed to work for up to 12 diners and for bills from $10 to $500.
a. What validation checks should be used for the number of diners and the size of the bill? [2]
b. Provide two sets of normal data and their expected results. [4]
c. Provide some abnormal/erroneous data. [1]
d. Identify the boundary data required and the expected results. [4
Q 5. The following data is to be entered onto an online form:
– Name
– Date of birth
– Password
– Phone number.
For each item state, with reasons, the validation and verification
checks that should be used on the input data
Q6 Counter Length Breadth Size Accept Reject Output
Solution Q 6Length
Counter Breadth Size Accept Reject Output
0 0 0
1 15 10 150 1
2 20 17 340 2
3 32 10 1
4 30 35 2
5 30 15 450 3
6 30 28 840 3
7 25 25 625 4
8 20 15 300 4
9 40 20 5
10 12 10 120 5 5,5,