Problem Solving & Design
Problem Solving & Design
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
• 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 iteach.pk
Example problem
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
sum of 10
numbers
a
uh
process numbers
initialize values while numbers display answer
M
are 10 or less
After breaking down the solution into smaller, more manageable sub-solutions, now we can devise an
algorithm for each part
Teacher:03215275281 iteach.pk
Program flowchart
The above problem can be shown the form of program flowchart
aq
ht
us
M
ad
m
a m
Teacher:03215275281 iteach.pk
Symbols used in program flowchart
aq
ht
us
M
Pseudocode
ad
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
Set N to 1
Repeat
Sum sum + N
N N+1
Until N >= 50
Print sum
Teacher:03215275281 iteach.pk
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
Teacher:03215275281 iteach.pk
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 iteach.pk
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
Teacher:03215275281 iteach.pk
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 iteach.pk
aq
ht
us
M
ad
m
a m
uh
M
Teacher:03215275281 iteach.pk
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 iteach.pk
aq
ht
us
M
ad
m
m
Answer
a
uh
M
Teacher:03215275281 iteach.pk
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
aq
< Less Than
ht
>= More than or equal
us
<> Not Equal to
M
Logical operators
ad
Operator Meaning
m
Teacher:03215275281 iteach.pk
Use of conditional statements
Flow Diagram for (if…..else )statement
aq
ht
us
M
ad
m
Else
a
End If
M
Teacher:03215275281 iteach.pk
If you want o check more than one condition at the same time, you can use ElseIf .
aq
ht
us
M
ad
m
a m
uh
M
Teacher:03215275281 iteach.pk
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
Line 2 : If total marks greater than 80 show message - "Got Higher First Class "
M
Teacher:03215275281 iteach.pk
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
ENDCASE
OUTPUT “Today is “,DayName
Teacher:03215275281 iteach.pk
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
[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 iteach.pk
1. startVal=1
2. endVal = 5
3. For var = startVal To endVal
4. show message
5. Next var
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
1. counter = 1
uh
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 iteach.pk
Repeat……until
aq
ht
repeat
us
M
sum := sum + number;
number := number - 2;
ad
until number = 0;
m
WHILE …DO
a
2. It is possible that the body may never be executed. (This occurs if the condition is true on
the first test)
M
REPEAT…….UNTIL
1. The condition is tested after the body is executed.
Teacher:03215275281 iteach.pk