Algorithm and Pseudo Codes
Algorithm and Pseudo Codes
Algorithm and Pseudo Codes
1. Problem solving
Define : Clearly describe a problem
Design its solution: Produce an ordered sequence of
steps that describe solution to the problem;
2. Implementation phase
Implement the program in some programming
language
write code, compile, link, Test & Debug
Developing a program
Problem solving
Phase
Implementation
phase
Defining a Problem:
Break the definition of the problem down into manageable
steps. Example; input, Processing; Output
• Pseudo Code
• Flowchart
Process
Input/Output
Decision
Connector
Flowlines
Figure 8-7
Flowcharts for three constructs
Build Flowchart
to Design Program
DISPLAY SUM
18
Algorithm
Pseudo code
• Pseudo code is a method of designing a program using
English like statement to describe the logic and
processing flow.
Solution
See Algorithm 8.1 on the next slide.
Algorithm 8.1: Average of two
BEGIN
Input: Two numbers
1. Add the two numbers
2. Divide the result by 2
3. Return the result by step 2
END
Example 2
Solution
See Algorithm 8.2 on the next slide.
Algorithm 8.2: Pass/no pass Grade
BEGIN
Input: One number
1. if (the number is greater than or equal to 70)
then
1.1 Set the grade to “pass”
else
1.2 Set the grade to “nopass”
End if
2. Return the grade
END
Example 3
Solution
See Algorithm 8.3 on the next slide.
Algorithm 8.3: Letter grade
BEGIN
Input: One number
1. if (the number is between 90 and 100, inclusive)
then
1.1 Set the grade to “A”
End if
2. if (the number is between 80 and 89, inclusive)
then
2.1 Set the grade to “B”
End if
Continues on the next slide
Algorithm 8.3: Letter grade (continued)
3. if (the number is between 70 and 79, inclusive)
then
3.1 Set the grade to “C”
End if
4. if (the number is between 60 and 69, inclusive)
then
4.1 Set the grade to “D”
End if
Solution
See Algorithm 8.5 on the next slide.
Algorithm 8.5: Find largest of 1000 numbers
BEGIN
Input: 1000 positive integers
1. Set Largest to 0
2. Set Counter to 0
3. while (Counter less than 1000)
3.1 if (the integer is greater than Largest)
then
3.1.1 Set Largest to the value of the integer
End if
3.2 Increment Counter
End while
4. Return Largest
END
Activity
BEGIN
Set sum 0
While (x < 2)
sum sum + x
x x + 2
END FOR
Display sum
END