0% found this document useful (0 votes)
1 views181 pages

CH 7 Algorithm Design and Problem Solving

Chapter 7 discusses the Program Development Life Cycle, which includes stages such as analysis, design, coding, and testing that developers follow when creating software. It emphasizes the importance of requirement specification, decomposition, and abstraction in the analysis phase, as well as the use of structured diagrams, flowcharts, and pseudocode in the design phase. The chapter also highlights the iterative nature of software development, where testing may lead to revisiting earlier stages of the cycle.

Uploaded by

ameya.saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views181 pages

CH 7 Algorithm Design and Problem Solving

Chapter 7 discusses the Program Development Life Cycle, which includes stages such as analysis, design, coding, and testing that developers follow when creating software. It emphasizes the importance of requirement specification, decomposition, and abstraction in the analysis phase, as well as the use of structured diagrams, flowcharts, and pseudocode in the design phase. The chapter also highlights the iterative nature of software development, where testing may lead to revisiting earlier stages of the cycle.

Uploaded by

ameya.saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 181

Chapter 7: Algorithm Design and Problem Solving

Analysis Design
C7.1
Program
Development
Life Cycle
Coding Testing

IGCSE Computer Science


DEFINITION

Program Development Life Cycle


A process that developers follow when creating

a new program.
Every software that has been developed follows the

same process (with alteration according to company).

Analysis Coding Launch

Design Testing
Mini software that you will be doing in the exam (Taken from the specimen paper).

Scenario

Your task
Chapter 7: Algorithm Design and Problem Solving

Design
C7.1

Analysis Program
Development
Life Cycle
Coding Testing

IGCSE Computer Science


ANALYSIS
REQUIREMENT SPECIFICATION

Problem needs to be clearly

defined and set out so anyone

working on the solution understand

what is needed.
ANALYSIS
UNDERSTANDING REQUIREMENTS FROM THE CLIENT’S
PERSPECTIVE. CLIENT CAN BE A PERSON OR AN END USER.
WE NEED TO DISCUSS WITH THEM, COLLECT DATA AND
SEVERAL INFORMATION EXCHANGE. IF NOT, THE PROCESS
CAN BE ENDLESS REPETITIONS AND CORRECTIONS.

Is this what my
client want?
TWO STEPS OF ANALYSIS

Decomposition Abstraction
DECOMPOSITION
Decomposition breaks down a complex problem into smaller parts, which

can be subdivided into even smaller parts, that can be solved easily.

EXAMPLE - ORGANIZE A HANG OUT


Sub Sub Sub
Task 1 Task 2 Task 3

Confirmation
Find a place Arranging
on who is
to hang out Transport
going
ABSTRACTION
Abstraction keeps the key elements required for the solution to the

problem and discards any unnecessary details and information

that is not required (temperorily).

EXAMPLE - ORGANIZE A HANG OUT


Sub
Sub
Task 1
Task 2

Confirmation
The most important Find a place

on who is to hang out


sub task
going
ABSTRACTION
Abstraction keeps the key elements required for the solution to the

problem and discards any unnecessary details and information

that is not required (temperorily).

EXAMPLE - ORGANIZE A HANG OUT


Sub
Sub
Task 1 Before knowing if anyone is
Task 2
going, the details of where to
Confirmation
The most important Find a place
hang out can be temporarily
on who is to hang out
abstracted. Eg. Just have a
sub task
going brief idea of where to go
ABSTRACTION - EXAMPLE 2 (TEACHING THIS LESSON)

LEVEL 1
ABSTRACTION - EXAMPLE 2 (TEACHING THIS LESSON)

LEVEL 2
ABSTRACTION - EXAMPLE 2 (TEACHING THIS LESSON)

LEVEL 3
AND SO
ON...
Chapter 7: Algorithm Design and Problem Solving

Analysis

C7.1
Program Design

Development
Life Cycle
Coding Testing

IGCSE Computer Science


DESIGN
In this stage, the developers of the

program will continue the decomposition

phase from the analysis and begin to

draw up visuals of the end product.


DESIGN

W1: Structured W2: Flowchart W3: Pseudocode


• Show how a program's • Enable complex
diagram
• Visualise end goals decomposed parts will run, problems and

including any decisions or instructions to be read in

inputs that are needed plain English


DESIGN

Structured diagram Flowchart Pseudocode


• Visualise end goals • Show how a program's • Enable complex

decomposed parts will run, problems and

including any decisions or instructions to be read in

inputs that are needed plain English


Chapter 7: Algorithm Design and Problem Solving

Analysis Design
C7.1
Program
Development
Coding
Life Cycle
Testing

IGCSE Computer Science


CODING
The implementation stage

The programmers work on the design

modules, such as input, output and

processes, and write code to make

each part of the program functional.


CODING
Example: E-commerce App
CODING
Example: E-commerce App

Module 1: Update sold count automatically


CODING
Example: E-commerce App
Module 2: Login and

SignUp page
CODING
Example: E-commerce App
Module 3: Notification

centre
CODING
Example: E-commerce App
Module 4: Search bar
CODING
An established app can have more than a million lines of code.
CODING
The implementation stage

The programmers work on the

design modules, such as input,

output and processes, and write

code to make each part of the

program functional.
Chapter 7: Algorithm Design and Problem Solving

Analysis Design
C7.1
Program
Development
Testing
Life Cycle
Coding

IGCSE Computer Science


TESTING Test strategy

The system is divided into small sections

that are tested for input, output and

validation rules. Test data

is used for all of the tests to be carried

out.
TESTING

Unit Testing Integration Testing


• Testing each part of the • All code is combined to

code individually form a program. Then,

the program is tested.


TESTING
Eg. Test that when an item is sold, the count is indeed increased by

1
TESTING
What do you think?
Chapter 7: Algorithm Design and Problem Solving

Analysis Program Design

Coding Development
Testing
Life Cycle
The program
development cycle is
a cycle and not a
linear process. After
developing a
Program Design
software, it is Development
Testing
necessary to go back
to the start of the
Life Cycle
process again if
something has been
discovered during
C7.2 - System, sub-system and
Structured Diagram
Computer system
• Made up of software, data, hardware,
communications and people.
• Each computer system can be divided up
into a set of sub-systems
• Each sub-systems can be further divided
into sub-systems and so on until each
sub-system just performs a single action.
Computer
system
• Made up of software, data,
Example
hardware, communications
and people.
• Each computer system can
be divided up into a set of
sub-systems
• Each sub-systems can be
further divided into
sub-systems and so on until
each sub-system just
performs a single action.
Example
Computer
system
• Made up of software, data,
hardware, communications
and people.
• Each computer system can
be divided up into a set of
• Software - the code
sub-systems
• Data - users' preference (like and follow)
• Each sub-systems can be
further divided into
• Hardware - server to store all the data
sub-systems and so on until (posts, stories, etc)
each sub-system just • Communication and people - instagram's
performs a single action. employees
Computer Example

system
• Made up of software, data,
hardware, communications
and people.
• Each computer system can
be divided up into a set of
sub-systems
• Each sub-systems can be
further divided into
sub-systems and so on until
each sub-system just
performs a single action.
post story reel
Computer
system
• Made up of software, data, Normal
hardware, communications
and people.
• Each computer system can
be divided up into a set of
sub-systems
• Each sub-systems can be
further divided into
sub-systems and so on until story
Close
each sub-system just
friends
performs a single action.
Other examples of
a computer
• Alarm App
system
• Weather App
• Games!!
to aspiring
programmer
Before you build something, figure out
what you want to build first.
Components of a
computer
• Inputs
system
• Outputs
• Processes
• Storage
Understanding the components of a
computer system will help us to break
down problem easier.
if i were to create a software
today...
How do I divide my system into
sub-system and sub-system?

How do I then start developing a


sub-system using an appropriate
programming language?
Use software design tools
Structured
How do I divide my system into
diagram
sub-system and sub-system?

How do I then start developing a


sub-system using an appropriate
programming language?
Flowchart Pseudocode
Structured diagram
• Show top-down design in a diagrammatic
form.
• Hiearchical, showing how a solution can
be divided into sub-systems with each
level giving a more detailed breakdown.

Top-down
design
Decomposition of a computer system into a set of sub- systems, then breaking
each sub-system down into a set of smaller sub-systems, until each sub-system
just performs a single action.
Structured diagram in application
(alarm clock)
Alarm Clock
Structured diagram in application
(alarm clock)
Alarm Clock

Inputs Outputs
Processes
Structured diagram in application
(alarm clock)
Alarm Clock

Inputs Outputs
Processes

Set Time
Turn alarm
Check Time Sound Turn off
on and off
Alarm alarm
Try it yourself
A security system on a door allows a user to type a
code into a keypad to enter the room. If an incorrect
code is input, an error message is shown and a short
alarm sound is played.

Draw a structured diagram for the security system.


Definition of algorithm

An algorithm is a set of
instructions for solving a problem
or accomplishing a task.
Example of an algorithm (Real Life)
Example of an algorithm (Real Life) - Driving a car

Walk to the parking


space Letgoooo!

Open the door


Put on your
seatbelt
Turn on the engine
Example of an algorithm (Software) - Alarm clock

For every 1 second,


check if time match

Alarm will ring


if time match,check if day
match (eg. weekday

If both matches, send


signal to sound system
Flowchart and pseudocode are an effective way to
communicate how the algorithm that makes up a system
or a sub-system work.

Flowchart Pseudocode
Flowchart and pseudocode are an effective way to
communicate how the algorithm that makes up a system
or a sub-system work.

Flowchart Pseudocode
Have a look at a
flowchart showing how
the checking for the
alarm time
subsystem works, can
you understand what is
it doing?
Components of a flow chart
Terminator process Input/output
Used at the Show the input of
Represent an
beginning and end data and output
instruction
of each flowchart No matter how small of information
Protecting the
the environment is
ecosystem on the same as
earth, everything protecting the
is interconnected ecosystem
Components of a flow chart
Decision
• Used to decide which action is to be taken
next
• There are always two outputs from a decision
flowchart symbol
Components of a flow chart

subroutine Flowline
• Represents a smaller • Use arrows to show the
procedure running within direction of flow
the large program.
• Abstraction in action

• Could be another
flowchart defined
elsewhere
Input/
output

Terminator
subroutine

process
Decision
A computer program allows a user to input
two numbers. It multiplies the numbers
Analysis
together and if the answer is larger than
Input:
100 it outputs the message "Greater than
100". The program loops until the user does
not want to input any more numbers.
Output:

Draw a flowchart that represents an


Process:
algorithm for the computer program.
A computer program allows a user to input
Analysis
two numbers. It multiplies the numbers
together and if the answer is larger than
Input: Number 1 & 2
100 it outputs the message "Greater than
100". The program loops until the user does
Output: Message >100
not want to input any more numbers.
Process:
Draw a flowchart that represents an
algorithm for the computer program.
- if N1 + N2 > 100,
output message >100
- process continue until
user does not want to
A computer program allows a user to input
two numbers. It multiplies the numbers
together and if the answer is larger than
100 it outputs the message "Greater than
100". The program loops until the user does
not want to input any more numbers.

Draw a flowchart that represents an


algorithm for the computer program.
Tickets are sold for a concert at $20 each,
if 10 tickets are bought then the discount
is 10%, if 20 tickets are bought the
discount is 20%. No more than 25 tickets
can be bought in a single transaction.

You are asked to design a program that


can automatically calculate the cost of the
purchase. Draw a flowchart for this
program.
Analysis
Tickets are sold for a concert at $20 each,
Input:
if 10 tickets are bought then the discount is
10%, if 20 tickets are bought the discount is Output:
20%. No more than 25 tickets can be
bought in a single transaction. Process:
You are asked to design a program that
can automatically calculate the cost of the
purchase. Draw a flowchart for this
program.
Analysis
Tickets are sold for a concert at $20 each,
Input: Number of tickets
if 10 tickets are bought then the discount is
10%, if 20 tickets are bought the discount is (limit = 25)
20%. No more than 25 tickets can be
bought in a single transaction. Output: Total cost
You are asked to design a program that Process:
can automatically calculate the cost of the
purchase. Draw a flowchart for this 10 tickets = 10% discount
program.
20 tickets = 20% discount
Input/
output

Terminator

Tickets are sold for a concert at $20 each,


if 10 tickets are bought then the discount is
10%, if 20 tickets are bought the discount is
Decision 20%. No more than 25 tickets can be
bought in a single transaction.

You are asked to design a program that


subroutine can automatically calculate the cost of the
purchase. Draw a flowchart for this
program.
process

Flow chart components rec


Tickets are sold for a concert at $20 each, if
10 tickets are bought then the discount is 10%,
if 20 tickets are bought the discount is 20%.
No more than 25 tickets can be bought in a
single transaction.

You are asked to design a program that can


automatically calculate the cost of the
purchase. Draw a flowchart for this program.
C7.4

Algorithm &
Pseudocode

IGCSE Computer Science


Pseudocode

fake, pretentious

Pseudocode to sort an array of numbers


Pseudocode
• Standard method of showing an
algorithm (a series of steps and
instructions to achieve a task)
• It uses English key words that
are very similar to those used in
a high-level programming
language
• It is not bound by the strict syntax
rules of a programming language
Pseudocode to sort an array of numbers

What is Pseudocode?
Pseudocode
• Person reading the pseudocode
does not need to have
knowledge in a programming
language to understand the
algorithm
• Commonly used in Computer
Science research paper

Pseudocode to sort an array of numbers

Why do we need pseudocode?


Elements of pseudocode

Input
Assignment Conditional Iterative
and Statement Statement
statement
Output
Assignment statement

• A value is assigned to item/variable

Variable Value

2. Variable is a container for storing a value.

Value
3.The values on the right can have one value or multiple value
combined with mathematical operators.
Assignment statement

One value Multiple values

x 5 x 5 *6

name "Adam" name "Hello" + " Adam"

The values on the right can have one value or multiple value combined
with mathematical operators.
Mathematical operators
Operator Action

+ Addition

- Subtraction

* Multiply

/ Divide
^ Raise to the power of

() Group
Test your understanding - Assignment statement questions

Given that Cost 5

Your task, create the following variables:


• Price - twice the value of cost
• Tax - 12% of the total price
• Selling price - Price plus tax
• Gender variable containing the value "M"
• Chosen variable containing the value FALSE
Test your understanding - Assignment statement questions

Given that Cost 5

Answer
• Price ← Cost * 2
• Tax ← Price * 0.12
• Selling price ← Price + Tax
• Gender ← "M"
• Chosen ← FALSE
Assignment statement - ARRAY

Assigning an array to a variable

DECLARE MyClass : ARRAY[1:10] OF STRING

Store value

We can also assign an array to a variable.


Array = A container that can store multiple items
To access a particular element of an array, we can put a
[position] after the variable name.

Assigning an array to a variable

DECLARE MyClass : ARRAY[1:10] OF STRING

1 2 3 4 5 6 7 8 9 10

Peter Jane

MyClass[3] -> Peter


MyClass[8] -> Jane
Test your understanding - Assignment statement questions

Your task, create an array variable named markList. The array size is 20
and it stores the INTEGER data type. How to access the 12th element?

Previous examples:
Assigning an array to a variable

DECLARE MyClass : ARRAY[1:10] OF STRING

1 2 3 4 5 6 7 8 9 10
Peter Jane
Input and Output

Input Output
Used for data entry; Used to display
it is usually followed information on a
by a variable where screen
the data input is
stored

Input variable Output variable


Input and Output - Application

INPUT cost
Price ← Cost * 2

Tax ← Price * 0.12

Selling price ← Price + Tax


selling price
OUTPUT
Conditional
• An element of pseudocode which allows the algorithm to do different
things depending on the input/scenario

IF ... THEN ... ELSE ... ENDIF

CASE OF ... OTHERWISE ... ENDCASE


IF ... THEN ... ELSE ... ENDIF

INPUT Age
IF Age < 18
THEN
OUTPUT "Child"
ELSE
OUTPUT "Adult"
ENDIF
IF ... THEN ... ELSE ... ENDIF

If condition is INPUT Age Condition


true (eg. Age IF Age < 18
< 18) THEN
OUTPUT "Child"

If condition is ELSE
false (eg. OUTPUT "Adult"
End of if
Age not < 18) ENDIF
statement
Comparison operators
Operator Action

> Greater Than


< Less Than
= Equal to
>= Greater Than or equal to
<= Less Than or equal to
<> Not Equal to
AND Both
OR Either
NOT Not
Test your understanding - Conditionals questions
Write a program in pseudocode that does the following.

Ask a user to INPUT his/her height and weight. If the height is


greater than 170 AND weight is lighter than 85, then OUTPUT the
message "You meet the requirement", else OUTPUT the message
"You need to be taller and lighter.
Test your understanding - Conditionals answer

INPUT Height
INPUT Weight
IF Height > 170 AND Weight < 85
THEN
OUTPUT "YOU MEET THE REQUIREMENT"
ELSE
OUTPUT "YOU NEED TO BE TALLER OR LIGHTER".
ENDIF
CASE OF ... OTHERWISE ... ENDCASE

• For a CASE statement the value of the variable decides the path to be
taken. Several values are usually specified.

INPUT ItemCode The data inside the this variable will be


CASE OF ItemCode checked in the below statements.

B101 : OUTPUT "Mineral Water”


B102 : OUTPUT "Biscuit"
B103 : OUTPUT “Rice”
If none of the data inside the variable
OTHERWISE : OUTPUT "Invalid Code" matches, it will display the output inside
ENDCASE OTHERWISE statement.
CASE OF ... OTHERWISE ... ENDCASE

INPUT FoodToBuy
CASE OF FoodToBuy
Milo : OUTPUT "Here's your milo"
Biscuit : OUTPUT "Here's your biscuit"
OTHERWISE : OUTPUT "The canteen does not have this food"
ENDCASE

Declare the end of case statement


Test your understanding - Conditionals questions

Write a program in pseudocode that does the following (Use the


CASE Statement).

Ask a user to INPUT his/her grades. If the grade entered is "A",


OUTPUT "excellent". If the grade entered is "B", OUTPUT "Not bad
huh". If the grade entered is "C", OUTPUT "Fair". If the grade entered
is "D" , OUTPUT "Why you sleep in my class?" . For all the other
value, OUTPUT "Invalid input".
Test your understanding - Conditionals answer

INPUT Grade
CASE OF Grade
A : OUTPUT "Excellent"
B : OUTPUT "Not bad huh"
C : OUTPUT "Fair"
D : OUTPUT "Why you sleep in my class?"
OTHERWISE : OUTPUT "Invalid input"
ENDCASE
Iterative statement
• An element of pseudocode which allows the algorithm to repeat certain
actions.

FOR... WHILE...
REPEAT...
TO... DO...
UNTIL...
NEXT... ENDWHILE

A repetition, where the


A repetition, where the
number of repeats is
A set number of number of repeats is not
not known, that may
repetitions known, that is completed
never be completed
at least once
FOR...
TO...
NEXT... FOR Counter ← 1 TO 10
A set number of
repetitions OUTPUT Counter
NEXT
Beginning value
FOR... Ending value
TO...
NEXT... FOR Counter ← 1 TO 10
A set number of
repetitions OUTPUT Counter
NEXT

Counter +1
What does the above pseudocode do?
What does the below pseudocode do?

Beginning value
FOR... Ending value
TO...
NEXT... FOR Counter ← 1 TO 10
A set number of
OUTPUT "Enter Name of Student "
repetitions
INPUT StudentName[Counter]
NEXT

Counter +1
ANSWER: Ask the user for 10 names iteratively, and
then place each name into the array.

FOR... FOR Counter ← 1 TO 10


TO... OUTPUT "Enter Name of Student "
NEXT...
INPUT StudentName[Counter]
A set number of
repetitions NEXT

1 2 3 4 5 6 7 8 9 10
REPEAT...
UNTIL... REPEAT
OUTPUT "Hello"
A repetition, where
the number of INPUT Option
repeats is not
known, that is Until Option = -1
completed at least
once
REPEAT...
UNTIL... REPEAT
OUTPUT "Hello"
A repetition, where
INPUT Option
the number of
repeats is not Until Option = -1
known, that is
completed at least
once Condition to determine whether we want
to continue our program
What does the code below do?

Total ← 0
Mark ← 0
REPEAT... REPEAT
UNTIL... Total ← Total + Mark
OUTPUT "Enter value for mark, -1 to finish "
INPUT Mark
UNTIL Mark = -1
Answer: In each iteration, ask the user for a mark. The mark given
will be added to the total. The program stops when the user enter
the value -1.
Total ← 0

REPEAT... Mark ← 0
REPEAT
UNTIL...
Total ← Total + Mark
OUTPUT "Enter value for mark, -1 to finish"
A repetition, where
the number of INPUT Mark
repeats is not UNTIL Mark = -1
known, that is
completed at least
once
WHILE... Total ← 0
DO... OUTPUT "Enter value for mark, -1 to finish "
ENDWHILE INPUT Mark
WHILE Mark <> -1 DO
A repetition, where
Total ← Total + Mark
the number of
OUTPUT "Enter value for mark, -1 to finish"
repeats is not
known, that may INPUT Mark
never be completed ENDWHILE
WHILE... Total ← 0
DO... OUTPUT "Enter value for mark, -1 to finish "
ENDWHILE INPUT Mark
WHILE Mark <> -1 DO
Total ← Total + Mark
OUTPUT "Enter value for mark, -1 to finish"
Block that
INPUT Mark
will be
ENDWHILE
executed by
Condition to decide
the DO
whether the DO block
keyword
will be run
Signify the end of the while loop.
Test your understanding - Iterative questions
Write a program in pseudocode that does the following.

Ask the user to enter a number iteratively. Then, the square of (power
of two) the number will need to be stored in an array. The program
shall halt when the user enters the value 0.

Hint: You can declare a dynamic (non-fixed size array) like this

DECLARE SquareNumArray : ARRAY[1: ] OF INTEGER


Test your understanding - Iterative answer

DECLARE SquareNumArray : ARRAY[1: ] OF INTEGER


COUNTER ← 1
INPUT Number
WHILE Number <> 0 DO
SquareNumArray[COUNTER] ← Number ^ 2
COUNTER ← COUNTER + 1
INPUT Number
END WHILE
C7.5

Standard methods of
solutions
Totalling
• Totalling means keeping a total that values are added to. For
example, keeping a running total of the marks awarded to each
student in a class.
StudentMark:
70 75 80 60 70 65 75 90 95 100
Totalling
StudentMark[1:10]
70 75 80 60 70 65 75 90 95 100

Pseudocode to find the total mark:


Total ← 0
ClassSize ← 10
FOR Counter ← 1 TO ClassSize
Total ← Total + StudentMark[Counter]
NEXT Counter
Counting
• Keeping a count of the number of times an action is performed is
another standard method. For example, counting the number of
students that were awarded a pass mark.

StudentMark[1:10]
70 75 80 60 7 65 75 25 95 100
Counting
70 75 80 60 7 65 75 25 95 100

PassCount ← 0
ClassSize ← 10
FOR Counter ← 1 TO ClassSize
IF StudentMark[Counter] > 50
THEN
PassCount ← PassCount + 1
NEXT Counter
Test your understanding
StudentName:
Ali Zak Bb Ali ZZ J

Write an algorithm (in pseudocode) to find the total number of students


whose name is "Ali" .
Test your understanding
StudentName:
Ali Zak Bb Ali ZZ J

AliCount ← 0
ClassSize ← 6
FOR Counter ← 1 TO ClassSize
IF StudentName[Counter] = "Ali"
THEN
AliCount ← AliCount + 1
NEXT Counter
Maximum and minimum
• Finding the maximum and minimum of a given array. Eg. Find the
highest and lowest mark awarded to a class of students.

StudentMark:
70 75 80 60 7 65 75 25 95 100
Maximum and minimum
• Method 1: Set the MaximumMark to be 0. Set the MinimumMark to
be 100.
MaximumMark ← 0
MinimumMark ← 100
ClassSize ← 10
FOR Counter ← 1 TO ClassSize
IF StudentMark[Counter] > MaximumMark
THEN
MaximumMark ← StudentMark[Counter]
ENDIF
IF StudentMark[Counter] < MinimumMark
THEN
MinimumMark ← StudentMark[Counter]
ENDIF
NEXT Counter
Maximum and minimum
• Method 2: Set the MaximumMark and MinimumMark to be
StudentMark[1]
MaximumMark ← StudentMark[1]
MinimumMark ← StudentMark[1]
ClassSize ← 10
FOR Counter ← 1 TO ClassSize
IF StudentMark[Counter] > MaximumMark
THEN
MaximumMark ← StudentMark[Counter]
ENDIF
IF StudentMark[Counter] < MinimumMark
THEN
MinimumMark ← StudentMark[Counter]
ENDIF
NEXT Counter
Average
• Finding the average value of a given array. Eg. Find the average
mark awarded to a class of students.

StudentMark:
70 75 80 60 7 65 75 25 95 100
Average
• Finding the average value of a given array. Eg. Find the average
mark awarded to a class of students.

Total ← 0
FOR Counter ← 1 TO ClassSize
Totalling
Total ← Total + StudentMark[Counter] NEXT
Counter
Average ← Total / ClassSize
Average
• Finding the average value of a given array. Eg. Find the average
mark awarded to a class of students.

Total ← 0
FOR Counter ← 1 TO ClassSize
Total ← Total + StudentMark[Counter] NEXT
Counter
Average ← Total / ClassSize
Linear search
• A type of search that is used to check if a value is stored in a list,
performed by going through the items in the list one at a time. Eg.
Searching for a name in a class list of students name, where all the
names stored are different.

StudentName:
ZK Lyn Tam YW Dan Joh Abd
Linear search
StudentName:
ZK Lyn Tam YW Dan Joh Abd

Say,we want to search for a


student called Lyn ..
Linear search
StudentName:
ZK Lyn Tam YW Dan Joh Abd

Found
!
The algorithm will go through the value in the
array one by one until it finds the target OR it
reaches the end of the list.
Linear search
OUTPUT "Please enter name to find "
INPUT Name
Found ← FALSE
Counter ← 1

REPEAT
IF Name = StudentName[Counter]
THEN
Found ← TRUE
ELSE
Counter ← Counter + 1
ENDIF
UNTIL Found ← TRUE OR Counter > ClassSize

IF Found
THEN
OUTPUT Name, " found at position ", Counter, " in the list."
ELSE
OUTPUT Name, " not found."
ENDIF
Bubble sort
• Lists can be more useful if the items are sorted in a meaningful order. For
example, names could be sorted in alphabetical order, or numbers could be
sorted in ascending or descending order.

Sorting Names:
ZK Lyn Tam YW Dan Joh Abd

Abd Dan Joh Lyn Tam YW ZK


Bubble sort
• Lists can be more useful if the items are sorted in a meaningful order. For
example, names could be sorted in alphabetical order, or numbers could be
sorted in ascending or descending order.

Sorting Numbers:
7 6 5 4 3 2 1

1 2 3 4 5 6 7
Bubble sort

Find this video on YouTube for


a thorough explanation!
Bubble sort
First ← 1
Last ← 10

REPEAT
Swap ← FALSE
FOR Index ← First TO Last - 1
IF Temperature[Index] > temperature[Index + 1]
THEN
Temp ← Temperature[Index]
Temperature[Index] ← Temperature[Index + 1]
Temperature[Index + 1] ← Temp
Swap ← TRUE
ENDIF
NEXT Index
Last ← Last - 1
UNTIL (NOT Swap) OR Last = 1
C7.6

VALIDATION
AND
VERIFICATION
IN ORDER FOR COMPUTER SYSTEMS TO ONLY ACCEPT DATA INPUTS THAT ARE
REASONABLE AND ACCURATE, EVERY ITEM OF DATA NEEDS TO BE EXAMINED
BEFORE IT IS ACCEPTED BY THE SYSTEM.

VALIDATION VERIFICATION
FOR DATA VERIFICATION IS
ENTRY, USED TO CHECK
VALIDATION THAT THE DATA
ENSURES THAT DOES NOT
ONLY DATA THAT CHANGE AS IT IS
IS REASONABLE BEING ENTERED.
IS ACCEPTED.
VALIDATION

VALIDATION IS THE AUTOMATED CHECKING BY A PROGRAM THAT DATA IS


REASONABLE BEFORE IT IS ACCEPTED INTO A COMPUTER SYSTEM.

IF THE DATA IS REJECTED A MESSAGE SHOULD BE OUTPUT EXPLAINING


WHY THE DATA WAS REJECTED AND ANOTHER OPPORTUNITY GIVEN TO
ENTER THE DATA.
Range VALIDATION
DEFINITION
Check
A RANGE CHECK CHECKS THAT THE
VALUE OF A NUMBER IS BETWEEN AN
UPPER VALUE AND A LOWER VALUE
SAY, WE WANT A PROGRAM THAT ONLY ACCEPTS A NUMBER FROM 0 -100

54 35 102
Range VALIDATION
THE CODE WILL REPEATEDLY
Check ASK THE USER FOR MARK INPUT
AS LONG AS THE MARK IS NOT
WITHIN THE RANGE 0-100

OUTPUT "PLEASE ENTER THE STUDENT'S MARK "


REPEAT
INPUT STUDENTMARK
IF STUDENTMARK < 0 OR STUDENTMARK > 100
THEN
OUTPUT "THE STUDENT'S MARK SHOULD BE IN THE RANGE 0 TO 100,
PLEASE RE-ENTER THE MARK "
ENDIF
UNTIL STUDENTMARK >= 0 AND STUDENTMARK <= 100
length VALIDATION

Check
DEFINITION
A LENGTH CHECK EITHER CHECKS THAT DATA CONTAINS
- AN EXACT NUMBER OF CHARACTERS (EG. 8)
- THE DATA ENTERED IS A REASONABLE NUMBER OF
CHARACTERS (EG. 8-20)

SAY, WE WANT A PROGRAM THAT ONLY ACCEPTS A PASSWORD WITH LENGTH


BETWEEN 8 - 20
ABCDEFGHIJKLMN
ABCD12345 ABCD OPQRSTUVWXYZ
length VALIDATION
THE CODE WILL REPEATEDLY
Check ASK THE USER FOR NAME INPUT
AS LONG AS THE LENGTH OF
THE NAME IS NOT WITHIN 2 TO 30
CHARACTERS.

OUTPUT "PLEASE ENTER YOUR NEW PASSWORD"


REPEAT
INPUT password
IF LENGTH(password) > 20 OR LENGTH(password) < 2
THEN
OUTPUT "Too short or too long, please re-enter "
ENDIF
UNTIL LENGTH(password) <= 20 AND LENGTH(password) >= 2
TYPE VALIDATION
DEFINITION
Check A TYPE CHECK CHECKS THAT THE DATA ENTERED IS OF
A GIVEN DATA TYPE, FOR EXAMPLE, THAT THE NUMBER
OF MEALS ORDERED MUST BE A NUMBER

SAY, WE WANT A PROGRAM THAT ONLY ACCEPTS AN INTEGER (NUMBER) AS


INPUT (EG. NUMBER OF MCD MEAL YOU ORDER).

3 3.2 THRE
E
TYPE VALIDATION
IT CHECKS WHETHER THE
Check NUMBER IS A WHOLE NUMBER. IF
NOT, USER WILL BE PROMPTED
TO INPUT AGAIN!

OUTPUT "HOW MANY MEALS YOU WANT TO ORDER? "


REPEAT
INPUT numberofmeals
IF numberofmeals <> DIV(numberofmeals, 1)
THEN
OUTPUT "This must be a whole number, please re-enter"
ENDIF
UNTIL numberofmeals = DIV(numberofmeals, 1)
Presence VALIDATION

check
DEFINITION
A PRESENCE CHECK CHECKS TO 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.

ABC@GMAIL.COM ""
Presence VALIDATION

check
DEFINITION
A PRESENCE CHECK CHECKS TO 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.
OUTPUT "PLEASE ENTER YOUR EMAIL ADDRESS "
REPEAT
INPUT EmailAddress
IF EmailAddress = ""
THEN
OUTPUT "please enter something!"
ENDIF
UNTIL EmailAddress <> ""
Format VALIDATION

check DEFINITION
A FORMAT CHECK CHECKS THAT THE CHARACTERS
ENTERED CONFORM TO A PREDEFINED PATTERN.

FOR EXAMPLE, WE WANT TO CREATE A PROGRAM THAT ONLY


ACCEPTS TAX CODE STARTING WITH "TC"

TC10212 TC12212 AC1212


3
Check
digit CHECK DIGIT
Check digits are used to identify errors in data entry
caused by mis-typing or mis-scanning a barcode.

A check digit is the final


digit included in a code; it
is calculated from all the
other digits in the code.
TEST YOUR
UNDERSTANDING
Discuss, what checks are required for the following validations

– Entering a telephone number


– Entering a pupil’s name
– Entering a part number in the form XXX999, when X must be a
letter and 9 must be a digit.
TEST YOUR
UNDERSTANDING
Discuss, what checks are required for the following validations

– Entering a telephone number


(answer: length check, type check, format check)
– Entering a pupil’s name
(answer: type check)
– Entering a part number in the form XXX999, when X must be a
letter and 9 must be a digit.
VERIFICATION
VERIFICATION IS CHECKING THAT DATA HAS BEEN ACCURATELY COPIED FROM ONE
SOURCE TO ANOTHER

VERIFICATION METHODS FOR INPUT DATA INCLUDE:

• DOUBLE ENTRY
• SCREEN/VISUAL CHECK
VERIFICATION(1) - DOUBLE ENTRY
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.
VERIFICATION(2) - SCREEN/VISUAL CHECK

A manual check completed by the user who is entering the data. When the data entry is
complete the data is displayed on the screen and the user is asked to confirm that it is
correct before continuing.
C7.7
TESTING
TESTING
• A program needs to be tested thoroughly to ensure that it is
working as intended.
• Before a whole system is tested, each sub-system is tested
separately (eg. Pseudocode, flowchart).
• However, in order to test a solution thoroughly it may need to be
worked through several times with different sets of test data.
DATA THAT CAN BE USED FOR
TESTING
NORMAL ABNORMAL EXTREME BOUNDARY
DATA DATA DATA DATA
DATA THAT A PROGRAM WOULD EXPECT
NORMAL TO RECEIVE, AND THE RESULT THAT
DATA ARE EXPECTED FROM THE DATA.

EG. PROGRAM THAT CALCULATES THE AVERAGE


SCORE
CORRECT
70 75 80 60 70 65 AVERAGE
SCORE

NORMAL DATA THAT WOULD 70


BE EXPECTED
ABNORMAL DATA THAT A PROGRAM SHOULD
DATA
REJECT. OUR PROGRAM NEEDS TO
REJECT THE ERRORNEOUS DATA
EG. PROGRAM THAT CALCULATES THE AVERAGE
SCORE
INCORRECT
WE NEED -5 75 80 60 70 65 AVERAGE
TO TEST SCORE

THAT OUR
PROGRAM
WILL
REJECT
NORMAL DATA THAT WOULD 57.5
THIS DATA
BE EXPECTED
ABNORMAL DATA THAT A PROGRAM SHOULD
DATA
REJECT. OUR PROGRAM NEEDS TO
REJECT THE ERRORNEOUS DATA
EG. PROGRAM THAT CALCULATES THE AVERAGE
SCORE

WE NEED -5 75 80 60 70 65 INCORRECT
AVERAGE
TO TEST SCORE
THAT OUR
PROGRAM
WILL
REJECT
NORMAL DATA THAT WOULD 57.5
THIS DATA
BE EXPECTED
EXTREME EXTREME DATA ARE THE LARGEST
DATA AND SMALLEST VALUES THAT
NORMAL DATA CAN TAKE
EG. PROGRAM TAKES IN USER'S INPUT FOR STUDENT'S MARK

70 75 80 60 100 0

NORMAL DATA THAT WOULD


EXTREME
BE EXPECTED
DATA
BOUNDARY AT EACH BOUNDARY TWO VALUES ARE
DATA REQUIRED: ONE VALUE IS ACCEPTED
AND THE OTHER VALUE IS REJECTED
SCENARIO: A PROGRAM THAT ACCEPTS
VALUE FROM RANGE 1 - 10

EXAMPLE 1 EXAMPLE 2
10 11 1 0

NORMAL + REJECTED NORMAL + REJECTED


EXTREME DATA EXTREME DATA
BOUNDARY AT EACH BOUNDARY TWO VALUES ARE
DATA REQUIRED: ONE VALUE IS ACCEPTED
AND THE OTHER VALUE IS REJECTED
SCENARIO: A PROGRAM THAT ACCEPTS
VALUE FROM RANGE 4 - 15

EXAMPLE 1 EXAMPLE 2

NORMAL + REJECTED NORMAL + REJECTED


EXTREME DATA EXTREME DATA
BOUNDARY AT EACH BOUNDARY TWO VALUES ARE
DATA REQUIRED: ONE VALUE IS ACCEPTED
AND THE OTHER VALUE IS REJECTED
SCENARIO: A PROGRAM THAT ACCEPTS
VALUE FROM RANGE 4 - 15

EXAMPLE 1 EXAMPLE 2
4 3 15 16

NORMAL + REJECTED NORMAL + REJECTED


EXTREME DATA EXTREME DATA
0 0 100

1 9 9 9

2 9 7 7

3 9 3 3

4 9 1 1 91
0 0 100

1 19 19 19

2 27 19 27

3 31 19 31

4 31 14 14 31 14
0 0 100

1 9 9 9

2 9 7 7

3 9 3 3

4 9 1 1 91
0 0 100
1 13 13 13

2 17 13 17

3 21 13 21

4 21 11 11 21 11
C7.9

Writing and amending


algorithms
1) Identify the error A B C X Output

of an algorithm using
a Trace Table
The algorithm on the left will ouput out the maximum
and minimum value, but does it work?
A B C X Output

Try it yourself!
Test Data: 400, 101, 200, 700
Max Min

A B C X Output

Try it yourself!
Test Data: 400, 101, 200, 700
Explanation
By declaring C to 100, we can only detect a minimum value that is
below 100. Eg. All test data are (400, 101, 200, 300), even though 101
is the minimum value among all the other values, it still cannot be
detected as it is more than 100.
The algorithm on the left will ouput out the maximum
and minimum value, but does it work?
A B C X Output

Try it yourself!
Test Data: -7, -5, -3,-6
Max Min

A B C X Output

Try it yourself!
Test Data: -7, -5, -3,-6
Explanation
By declaring B to 0, we can only detect a maximum value that is above
0. Eg. All test data are (-5 , -3, -2 , -7), even though -2 is the maximum
value among all the other values, it still cannot be detected as it is not
more than 0.
We can fix it - quick fix

Identify two numbers where the algorithm


will still fail.
We can fix it - quick fix

1000001 will not be identified as the minimum


-1000001 will not be identified as the maximum
Ultimate
solution
Instead of initialising B and C
as some random value, we let
the first number to be B
(maximum) and C (minimum)
2) Identify the error
of an algorithm using
self-generated
test data
Problem statement
Tickets are sold for a concert at $20 each, if 10 tickets are bought then the
discount is 10%, if 20 tickets are bought the discount is 20%. No more
than 25 tickets can be bought in a single transaction

You are asked to design a program that can automatically calculate the
cost of the purchase. Output the cost. The program will keep repeating
until a user INPUT a value “stop”. Write the pseudocode for this program.
REPEAT
OUTPUT "How many tickets would you like to buy? "
INPUT NumberOfTickets
UNTIL NumberOfTickets > 0 OR NumberOfTickets < 26

IF NumberOfTickets < 10
THEN
Discount ← 0
ELSE

Answer IF NumberOfTickets < 20


THEN
Discount ← 0.1
ELSE
Discount ← 0.2
ENDIF
ENDIF
Cost ← NumberOfTickets * 20 * (1 – Discount)
OUTPUT "Your tickets cost ", Cost
REPEAT
OUTPUT "How many tickets would you like to buy? "
INPUT NumberOfTickets
UNTIL NumberOfTickets > 0 OR NumberOfTickets < 26

IF NumberOfTickets < 10
create some test THEN

data to check the Discount ← 0


ELSE
correctness of an IF NumberOfTickets < 20
THEN
algorithm
Discount ← 0.1
ELSE
Discount ← 0.2
ENDIF
ENDIF
Cost ← NumberOfTickets * 20 * (1 – Discount)
OUTPUT "Your tickets cost ", Cost
eg. REPEAT
OUTPUT "How many tickets would you like to buy? "
INPUT NumberOfTickets
Test data = 12 UNTIL NumberOfTickets > 0 OR NumberOfTickets < 26

Discount = 10%
IF NumberOfTickets < 10
Calculation= THEN
Discount ← 0
ELSE
(12 x 20) * 0.9 = IF NumberOfTickets < 20

$216 THEN
Discount ← 0.1
ELSE
Now, go through the Discount ← 0.2
ENDIF
algorithm to check if it
ENDIF
works as expected Cost ← NumberOfTickets * 20 * (1 – Discount)
OUTPUT "Your tickets cost ", Cost
with the test data.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy