Vous êtes sur la page 1sur 43

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 Before understood. problem: Once Top-down A down separate solution breakdown 
Top-down technique What What In What Are individual what these the any an are decisions will 
into sub-solution design solution algorithm order areas design basic the the be to There smaller 
stages any the solution inputs is do of M things also need to are the big outputs instructions can 
solution a – or known u a or problem problem into What to are according be number sub-task. 
complex be h Understanding designed, the understood, of as made hurdles or the a stepwise 
problem? repeated? or need tasks, of problem? We m problem to Create task basic in the need it 
to the can until into is refinement. m be it input-storage-processing-output. things important 
problem? continue is to carried is we a time to a number be a reach to solution use overcome to 
the d know out? this to design top-down simple of check problem process M in steps order the on 
steps. that u that the design, algorithm. of to the s way breaking A can really good problem h 
which to be the understand ta considered strategy down means goal? is q 
completely 
the is breaking to 
the 
as sub- 
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 iteach.pk 
 
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 
First two steps can be further divided into smaller sub-solutions 
After breaking down the solution into smaller, more manageable sub-solutions, now we can devise an 
algorithm for each part 

find total of 10 


initialize running values initialize totals M 
values for 



a process m 
numbers 
numbers 
m process sum while are sum numbers a 
10 numbers of of numbers d 
or 10 less 

10 



display s h 
ta display answer q 
answer 
N <---- 0 sum <---- 0 
add 1 to N 
add N to sum 
Teacher:03215275281 iteach.pk 
 
Program flowchart 
The above problem can be shown the form of program flowchart 
A  flowchart  is  a  type  of  diagram  that  represents an algorithm, workflow or process, showing the 
steps  as  boxes  of  various  kinds,  and  their  order  by  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 iteach.pk 













ta 
 
Symbols used in program flowchart 

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. 
The above flowchart can be expressed in the form of pseudocode as follows 
Set sum to zero 
Set N to 1 
Repeat 
Sum ← sum + N 
N ← N+1 
Until N >= 50 
Print sum 


Teacher:03215275281 iteach.pk 













ta 
 
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 


What is the purpose of above flowchart? 
Answer: To find the average of all positive numbers. 


Teacher:03215275281 iteach.pk 













ta 
 
The value of count starts from 1 so only 999 iterations work. 
Line 1 can be changed to count=0 
Or 
Line 5 can be changed to (until count=1001) 
Or 
Until count>1000 


Teacher:03215275281 iteach.pk 













ta 
 
Validation and verification checks on input data 
Validation include (range check, length check, type check, check digit, consistency check and presence 
check) 
Answer: Length check 


Teacher:03215275281 iteach.pk 













ta 
 
Trace tables to find the value of variables at each step in algorithm 


Teacher:03215275281 iteach.pk 













ta 
 

Teacher:03215275281 iteach.pk 













ta 
 
Identifying error in a given algorithm and suggest ways of removing these errors 

Answer 


Teacher:03215275281 iteach.pk 













ta 
 

Teacher:03215275281 iteach.pk 

Answer 













ta 
 
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 
< Less Than 
>= More than <= Less than <> Not Equal And or Xor Not Operator M u 
h Both One One Negates a 
side side Logical sides m 
truth or or must other other m 
be must must operators a true 
d or to or equal 
equal 





ta 

Meaning 
be true 
be true but not both 
Teacher:03215275281 iteach.pk 
 
Use of conditional statements 
Flow Diagram for (if.....else )statement 

If [your condition here] 


Your code here Else 
Your code here End If 


Teacher:03215275281 iteach.pk 













ta 
 
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 
End If 
Your M code u h 
Here 










ta 

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 1. 2. 3. 4. 5. 6. 7. 8. 9. Explanation 
Line Line here 1 : 2 : Checking If If ElseIf 
ElseIf Else End total implementing marks 
totalMarks the MsgBox("Got MsgBox("Got 
MsgBox("Just MsgBox("Failed") 
If 
of total greater u above totalMarks totalMarks these marks h than a conditions greaterthan 
program 80 m show >= m in Higher First message a 80 pass or VB a >= >= equal program. d 
Then 

- 60 40 "Got to Class only") M 


80 First Higher Then Then u First s ") 
Class h Class ta " 


") 
Line 3 : Checking M 
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 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 CASE 1:DayName 2:DayName 3:DayName 


4:DayName 5:DayName 6:DayName 7:DayName 
OTHERWISE 
ENDCASE 
M DayNumber DayName 
WRITE in u h the a 
m “you ← ← ← ← ← ← ← range OF m “Monday” 
“Tuesday” 
“Wednesday” 
“Thursday” 
“Friday” 
“Saturday” 
“Sunday” have a 1 d to not M 7” 
u entered s h 
ta 

number 
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. 
1. For.......to.........Next The FOR NEXT Loop , execute the loop body (the source 
code within For ..Next code block) to a fixed number of times. 

For var=[startValue] To [endValue] [Step] 


[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 













ta 
 
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 Line 4: Execute Line 5: Taking next While ..End While .. End While 
statements ) until loop is encountered. Condition : the The M While starting it Loop step, loop 
meets If While End 1. 2. 3. 4. condition u the execute body if value the h the evaluation [loop 
counter While counter While a specified to the m [condition] 
Message counter var set code result and not condition. m body] (counter by body reach inform is 
= the true, a (the the 1 to d The user = the source stop endVal expression counter loop M when 
code <= body u the within 10) statements s is var evaluated h reach While + ta 1 
endVal are and each q 
executed. End time while the 
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 
repeat 
sum := sum + number; number := number - 2; 
until number = 0; 
Difference between WHILE ...DO......ENDWHILE & REPEAT.......UNTIL Loop 
Structure 

WHILE ...DO 
1. The condition is tested before the body is executed. 
2. It is possible that the body may never be executed. (This occurs if the condition is true on 
the first test) 
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 iteach.pk 













ta 

Vous aimerez peut-être aussi