2.1.
1 Problem solving and design
An algorithm is a plan, a logical step-by-step process for solving a problem. Algorithms are
normally written as a flowchart or in pseudocode.
The key to any problem-solving task is to guide your thought process. The most useful thing to
do is keep asking ‘What if we did it this way?’ Exploring different ways of solving a problem can
help to find the best way to solve it.
When designing an algorithm, consider if there is more than one way of solving the problem.
When designing an algorithm there are two main areas to look at:
The big picture - What is the final goal?
The individual stages – What hurdles need to be overcome on the way to the goal?
Understanding the problem
aq
Before an algorithm can be designed, it is important to check that the problem is completely
ht
understood. There are a number of basic things to know in order to really understand the
problem:
us
• What are the inputs into the problem?
• What will be the outputs of the problem?
• In what order do instructions need to be carried out?
M
• What decisions need to be made in the problem?
• Are any areas of the problem repeated?
ad
Once these basic things are understood, it is time to design the algorithm.
Create a solution
m
Top-down design
m
A technique to any big or complex problem is to use top-down design, which means breaking
down the solution to a problem or task into a number of steps that can be considered as
a
separate sub-solution or sub-task. We can continue this process of breaking down the sub-
uh
solution into smaller solution or tasks, until we reach simple steps. A good strategy is to
breakdown the solution according to the input-storage-processing-output.
M
Top-down design is also known as stepwise refinement.
Advantage of top-down design
• Making the design well-structured and easier to understand, modify and debug.
• Speeding up development
• Each sub-solution or module can be given to a different programmer in the team.
Teacher:03215275281 [Link]
Example problem
Write a program to input 10 numbers and output their total
We can use a structure diagram to represent how this problem has been broken-down into sub-
solutions, sequence from left to right
find total of 10
numbers
aq
initialize values for process sum of 10
ht
running totals display answer
numbers
us
M
ad
First two steps can be further divided into smaller sub-solutions
m
m
sum of 10
numbers
a
uh
process numbers
initialize values while numbers display answer
M
are 10 or less
N <---- 0 sum <---- 0 add 1 to N add N to sum
After breaking down the solution into smaller, more manageable sub-solutions, now we can devise an
algorithm for each part
Teacher:03215275281 [Link]
Program flowchart
The above problem can be shown the form of program flowchart
aq
ht
us
M
ad
m
a m
A flowchart is a type of diagram that represents
uh
an algorithm, workflow or process, showing the
steps as boxes of various kinds, and their order by
M
connecting them with arrows. This diagrammatic
representation illustrates a solution model to a
given problem. Flowcharts are used in analyzing,
designing, documenting or managing a process or
program in various fields
A simple flowchart representing a process for
dealing with a non-functioning lamp.
Teacher:03215275281 [Link]
Symbols used in program flowchart
aq
ht
us
M
Pseudocode
ad
pseudocode is an informal high-level description of a computer program or algorithm.
m
It uses the structural conventions of a programming language, but is intended for human reading rather
than machine reading. Pseudocode typically omits details that are essential for machine understanding
m
of the algorithm, such as variable declarations, system-specific code and some subroutines.
a
The above flowchart can be expressed in the form of pseudocode as follows
uh
M
Set sum to zero
Set N to 1
Repeat
Sum sum + N
N N+1
Until N >= 50
Print sum
Teacher:03215275281 [Link]
Library routines and sub-routines
In computer programming, a subroutine is a sequence of program instructions that perform a specific
task, packaged as a unit. This unit can then be used in programs wherever that particular task should be
performed. Subprograms may be defined within programs, or separately in libraries that can be used by
multiple programs. In different programming languages, a subroutine may be called a procedure,
a function, a routine, a method, or a subprogram.
Purpose of algorithm/flowchart
Study the flowchart very carefully
aq
ht
us
M
ad
m
a m
uh
M
What is the purpose of above flowchart?
Answer: To find the average of all positive numbers.
Teacher:03215275281 [Link]
The value of count starts from 1 so only 999 iterations work.
aq
Line 1 can be changed to count=0
ht
Or
us
Line 5 can be changed to (until count=1001)
M
Or
ad
Until count>1000
m
a m
uh
M
Teacher:03215275281 [Link]
Validation and verification checks on input data
Validation include (range check, length check, type check, check digit, consistency check and presence
check)
aq
ht
us
M
ad
m
a m
uh
M
Answer: Length check
Teacher:03215275281 [Link]
Trace tables to find the value of variables at each step in algorithm
aq
ht
us
M
ad
m
a m
uh
M
Teacher:03215275281 [Link]
aq
ht
us
M
ad
m
a m
uh
M
Teacher:03215275281 [Link]
Identifying error in a given algorithm and suggest ways of removing these errors
aq
ht
Answer
us
M
ad
m
a m
uh
M
Teacher:03215275281 [Link]
aq
ht
us
M
ad
m
m
Answer
a
uh
M
Teacher:03215275281 [Link]
2.1.2 Pseudocode
pseudocode is an informal high-level description of a computer program or algorithm.
It uses the structural conventions of a programming language, but is intended for human reading rather
than machine reading. Pseudocode typically omits details that are essential for machine understanding
of the algorithm, such as variable declarations, system-specific code and some subroutines.
Conditional operators
Operator Meaning
= Equal to
> More than
aq
< Less Than
ht
>= More than or equal
<= Less than or equal
us
<> Not Equal to
M
Logical operators
ad
Operator Meaning
m
And Both sides must be true
or One side or other must be true
m
Xor One side or other must be true but not both
a
Not Negates truth
uh
M
Teacher:03215275281 [Link]
Use of conditional statements
Flow Diagram for (if…..else )statement
aq
ht
us
M
ad
m
If [your condition here]
Your code here
m
Else
a
Your code here
uh
End If
M
Teacher:03215275281 [Link]
If you want o check more than one condition at the same time, you can use ElseIf .
If [your condition here]
Your code here
ElseIf [your condition here]
Your code here
ElseIf [your condition here]
Your code here
Else
Your code Here
End If
aq
ht
us
M
ad
m
a m
uh
M
Teacher:03215275281 [Link]
Just take a real-time example - When we want to analyze a mark lists we have to apply some
conditions for grading students depends on the marks.
Following are the grading rule of the mark list:
1) If the marks is greater than 80 then the student get higher first class
2) If the marks less than 80 and greater than 60 then the student get first class
3) If the marks less than 60 and greater than 40 then the student get second class
4) The last condition is, if the marks less than 40 then the student fails.
Now here implementing these conditions in a VB program.
aq
1. If totalMarks >= 80 Then
ht
2. MsgBox("Got Higher First Class ")
3. ElseIf totalMarks >= 60 Then
us
4. MsgBox("Got First Class ")
5. ElseIf totalMarks >= 40 Then M
6. MsgBox("Just pass only")
7. Else
ad
8. MsgBox("Failed")
9. End If
m
Explanation of above program
a m
Line 1 : Checking the total marks greaterthan or equal to 80
uh
Line 2 : If total marks greater than 80 show message - "Got Higher First Class "
M
Line 3 : Checking the total marks greaterthan or equal to 60
Line 4 : If total marks greater than 60 show message - "Got First Class "
Line 5 : Checking the total marks greaterthan or equal to 40
Line 6 : If total marks greater than 40 show message - "Just pass only"
Line 7 : If those three conditions failed program go to the next coding block
Line 8 : If all fail shows message "Failed"
Line 9 : Ending the condition block
Teacher:03215275281 [Link]
CASE ………OF ……OTHERWISE……..ENDCASE statement
It is quite tedious to program an extra IF statement for each extra route required. When the
multiple conditions all involve a similar expression it is quicker and clearer to use a
CASE….OF…..OTHERWISE……ENDCASE Statement.
Consider the following program in which the user inputs a number representing a day of the
week (1=Monday, 2=Tuesday…etc.)And the pseudocode algorithm assigns the name of the day
to the variable DayName.
Input DayName
CASE DayNumber OF
aq
1:DayName “Monday”
ht
2:DayName “Tuesday”
us
3:DayName “Wednesday” M
4:DayName “Thursday”
5:DayName “Friday”
ad
6:DayName “Saturday”
m
7:DayName “Sunday”
m
OTHERWISE
a
uh
WRITE “you have not entered number
in the range 1 to 7”
M
ENDCASE
OUTPUT “Today is “,DayName
Teacher:03215275281 [Link]
Loops
1. FOR…..TO….NEXT
2. REPEAT…….UNTIL
3. WHILE…..DO…..ENDWHILE
The following illustration shows a loop structure that runs a set of statements until a condition
becomes true.
aq
ht
us
M
ad
m
m
1. For…….to………Next
a
The FOR NEXT Loop , execute the loop body (the source code within For ..Next code block) to a
uh
fixed number of times.
For var=[startValue] To [endValue] [Step]
M
[loopBody]
Next [var]
var : The counter for the loop to repeat the steps.
starValue : The starting value assign to counter variable .
endValue : When the counter variable reach end value the Loop will stop .
loopBody : The source code between loop body
Let’s take a simple real time example , If you want to show a messagebox 5 times and each time
you want to see how many times the message box shows.
Teacher:03215275281 [Link]
1. startVal=1
2. endVal = 5
3. For var = startVal To endVal
4. show message
5. Next var
Line 1: Loop starts value from 1
Line 2: Loop will end when it reaches 5
Line 3: Assign the starting value to var and inform to stop when the var reach endVal
Line 4: Execute the loop body
aq
Line 5: Taking next step, if the counter not reach the endVal
ht
While ..End While
us
While .. End While Loop execute the code body (the source code within While and End while
statements ) until it meets the specified condition. The expression is evaluated each time the
M
loop is encountered. If the evaluation result is true, the loop body statements are executed.
ad
While [condition]
[loop body]
m
End While
m
Condition : The condition set by the user
a
1. counter = 1
uh
2. While (counter <= 10)
M
3. Message
4. counter = counter + 1
5. End While
Line 1: Counter start from 1
Line 2: While loop checking the counter if it is less than or equal to 10
Line 3: Each time the Loop execute the and shows message
Line 4: Counter increment the value of 1 each time the loop execute
Line 5: End of the While End While Loop body
Teacher:03215275281 [Link]
Repeat……until
aq
ht
repeat
us
M
sum := sum + number;
number := number - 2;
ad
until number = 0;
m
Difference between WHILE …DO……ENDWHILE & REPEAT…….UNTIL Loop Structure
m
WHILE …DO
a
1. The condition is tested before the body is executed.
uh
2. It is possible that the body may never be executed. (This occurs if the condition is true on
the first test)
M
3. The loop exits when the condition is false.
REPEAT…….UNTIL
1. The condition is tested after the body is executed.
2. The body is always executed at least once.
3. The loop exits when the condition is true.
Note: candidates are advised to try out solutions to a variety of different
problems on a computer using a language of their choice, no particular
programming language will be assumed in this syllabus
Teacher:03215275281 [Link]