2 Pseudocodes
2 Pseudocodes
1
ALGORITHM
▪ Algorithms for making things will often be
divided into sections
▪ the parts/components/ingredients (inputs)
required to accomplish the task.
▪ actions/steps/methods (processing) to
produce the required outcome (output).
2
PROBLEM SOLVING EXAMPLE
▪ How many stamps do you use when mailing a letter?
▪ One rule of thumb is to use one stamp for every five sheets of
paper or fraction thereof.
3
ALGORITHM
1. Request the number of sheets of paper; call it Sheets. (input)
2. Divide Sheets by 5. (processing)
3. Round the quotient up to the next highest whole number; call it
Stamps. (processing)
4. Reply with the number Stamps. (output)
4
PROGRAMMING TOOLS
▪ Tools are used to convert algorithms into
computer programs:
▪ Flowchart - Graphically depicts the logical
steps to carry out a task and shows how the
steps relate to each other.
▪ Pseudocode - Uses English-like phrases with
some Visual Basic terms to outline the
program.
5
PSEUDOCODE
▪ Pseudocode (which means fake code, because it is
not really programming code) specifies the steps
required to accomplish the task.
▪ Pseudocode is a type of structured English that is
used to specify an algorithm.
▪ Pseudocode cannot be compiled nor executed, and
there are no real formatting or syntax rules.
6
AREA OF A CIRCLE PSEUDOCODE
▪ Program: area of a circle
7
ANALYZING THE PROBLEM
▪ To write an algorithm, we first analyze the problem:
- What is the input(s)
- What is the output(s)
- What is the relation between the input and output
- How we can reach from the input to the output
8
WRITE A PSEUDOCODE TO TAKE YOUR BIRTH
YEAR AND PRINT YOUR AGE
Analyze the Problem
Input: year
Output: age
Relation: current year-birth year
1. Start
2. Read birth year in b and current year in c
3. Set d = c - b
4. Print d
5. End
9
WRITE PSEUDOCODE STATEMENTS
▪ There are six basic computer operations
10
WRITE PSEUDOCODE STATEMENTS
▪ A computer can perform arithmetic
Use actual mathematical symbols or the words for the
symbols
+, -, *, /
Calculate, Compute also used
▪ Example:
Add number to total
Total = total + number
11
WRITE PSEUDOCODE STATEMENTS
▪ A computer can assign a value to a piece of data
▪ to give data an initial value (Initialize, Set)
▪ to assign a value as a result of some processing (x=5+y)
▪ to keep a piece of information for later use (Save, Store)
12
WRITE PSEUDOCODE STATEMENTS
▪ A computer can compare two piece of
information and select one of two alternative
actions
IF condition THEN
some action
ELSE
alternative action
ENDIF
13
WRITE PSEUDOCODE STATEMENTS
▪ A computer can repeat a group of actions
14
ADVANTAGES OF PSEUDOCODE
▪ Reduced complexity.
▪ Increased flexibility.
▪ Ease of understanding.
15
WHY IS PSEUDOCODE NECESSARY
▪ The programming process is a complicated one.
▪ You must break the main tasks that must be accomplished into
smaller ones in order to be able to eventually write fully
developed code.
16
TIPS AND TRICKS OF PSEUDOCODE
▪ There are many styles of pseudocode
17
TWO DIFFERENT STYLES
Write a pseudocode to find the average of two numbers.
1. Start 1. Start
2. Read two numbers 2. Read two numbers in x and y
3. Add the two numbers 3. Set w = x + y
4. Divide the result by 2 4. Set z = w / 2
5. Write the result 5. Print z
6. End 6. End
18
EXAMPLE-1
▪ Write an algorithm to take your age and print
it in seconds.
19
EXAMPLE-1
▪ Write an algorithm to take your age and print
it in seconds.
1. Start
2. Read age in a
3. Set s = a ⨯ 365 ⨯ 24 ⨯ 60 ⨯ 60
4. Print s
5. End
20
EXAMPLE-2
▪ Write an algorithm to take the sides (length
and width) of a rectangle and calculate its
perimeter and area
21
EXAMPLE-2
▪ Write an algorithm to take the sides (length and
width) of a rectangle and calculate its perimeter
and area
1. Start
2. Read width and length in w and l
3. Set a = w ⨯ l
4. Set p = 2 ⨯ (w + l)
5. Print “perimeter is p and area is a”
6. End
22
STATEMENT STRUCTURES
▪ Sequence – follow instructions from one line
to the next without skipping over any lines
▪ Decision - if the answer to a question is
“Yes” then one group of instructions is
executed. If the answer is “No,” then another
is executed
▪ Repetition (Loop) – a series of instructions
are executed over and over
23
PSEUDOCODE FOR STATEMENT STRUCTURES
24
CONDITIONS IN ALGORITHMS
▪ Most of the times we need to do
different actions in different cases.
25
IF STATEMENT
▪ If statement is the simplest decision-making statement. It is
used to decide whether a certain statement or block of
statements will be executed or not.
▪ If a certain condition is true then a block of statement is
executed otherwise not.
If condition then
statements
End If
26
IF-ELSE STATEMENT
▪ The if statement alone tells us that if a condition is true it will
execute a block of statements and if the condition is false it won’t.
But what if we want to do something else if the condition is false.
▪ Here comes the else statement. We can use the else statement with
if statement to execute a block of code when the condition is false.
If condition then
statements
Else
statements
End If
27
MULTIPLE IF AND IF-ELSE STATEMENTS
• It is possible to write multiple if or if-else statements according to
problem requirements.
• Difference: In the usage of multiple if statements, if the condition is true,
all will be executed. However in if-else-if combination, only one meeting
the condition will be executed and none of the remaining else ifs or elses
will be tested.
1. Start
2. Read number in p We write the commands
3. If p modulo 2 = 0 then inside the if or else body with
4. print even some indent
5. Else
6. print odd
7. End If
8. End
29
EXAMPLE-1
30
EXAMPLE-1
▪ Write an algorithm to take two numbers and print the minimum and
maximum number.
1. Start
2. Read numbers in x and y
3. If x > y then
4. print “maximum is x”
5. print “minimum is y”
6. Else
7. print “maximum is y”
8. print “minimum is x”
9. End If
10.End
31
EXAMPLE-2 - PROBLEM
▪ Write an algorithm to take the required input of an employee
and calculates the net salary based on the following rules: (Let
min salary = 2000 TL)
3) Additional: for each child add 100 TL (Add it to the net salary)
32
EXAMPLE-2- SOLUTION I
1. Start
2. Read GrossSal and NumChild
3. If GrossSal = 2000 then
4. Set Tax = GrossSal * 5 / 100
5. End If
6. If 2000 < GrossSal < 6000 then
7. Set Tax = GrossSal * 7 / 100
8. End If
9. If GrossSal ≥ 6000 then
10. Set Tax = GrossSal * 10 / 100
11. End If
12. Set Insur = GrossSal * 14 / 100
13. Set ChildExtra = NumChild * 100
14. Set Total = GrossSal - Tax - Insur + ChildExtra
15. print “Net Salary is Total”
16. End 33
EXAMPLE-2- SOLUTION II
1. Start
2. Read GrossSal and NumChild
3. If GrossSal = 2000 then
4. Set Tax = GrossSal * 5 / 100
5. Else If 2000 < GrossSal < 6000 then
6. Set Tax = GrossSal * 7 / 100
7. Else
8. Set Tax = GrossSal * 10 / 100
9. End If
10. Set Insur = GrossSal * 14 / 100
11. Set ChildExtra = NumChild * 100
12. Set Total = GrossSal - Tax - Insur + ChildExtra
13. print “Net Salary is Total”
14. End
34
EXAMPLE-3
35
EXAMPLE-3
Write an algorithm to take three numbers and print the maximum of
them
1. Start
2. Read x, y, z
3. If x ≥ y then
4. Set Max = x
5. Else Think about
more numbers -
6. Set Max = y need for an
7. End If iteration!!!
8. If z ≥ Max then
9. Set Max = z
10.End If
11.print “The maximum number is Max”
12.End
36
EXAMPLE-4
37
EXAMPLE-4
▪ Write an algorithm to take two numbers and compare them in
terms of greatness.
1. Start
2. Read num1, num2
3. If num1 > num2 then
4. print “num1 is greater”
5. Else If num2 > num1 then
6. print “num2 is greater”
7. Else
8. print “num1 is equal to num2”
9.
10.End If
11.End
38
NESTED IF-ELSE STATEMENT
▪ When a series of decisions are involved, we may have to use more
than one if-else statement in nested form.
▪ The then and else block of an if statement can contain any valid
statements, including other if statements. An if statement
containing another if statement is called a nested-if statement.
If condition1 then
If condition2 then
statements-1
Else
statements-2
End If
statements-3
End If
39
EXAMPLE
▪ Write an algorithm to take a student’s grade point average (GPA) and
completed hours in a term, and check whether s/he is a high honor and
honor student.
▪ Note that to be both a honor and high honor student, completed hours in
the term should be 15 at least. In addition to this, GPA should be 3.5 and
3.0 at least to be a high honor and honor student respectively.
1. Start
2. Read GPA, hours
3. If hours ≥ 15 then
4. If GPA ≥ 3.5 then
5. print “High Honor Student”
6. Else If GPA ≥ 3.0 then
7. print “Honor Student”
8. Else
9. print “Standard Student”
10. End If
11.Else
12. print “Standard Student”
13.End If 40
14.End
LOOPS IN ALGORITHMS
▪ In many cases we need to repeat some
actions for a specific number of times or until
a specific condition
▪ Conditions are important part of loops
▪ Loops may need a counter to control the number of
iterations
41
WHILE STATEMENT
▪ A while loop is the most straightforward looping
structure.
While condition
statements
End While
42
WHILE STATEMENT
▪ After the body of a loop is executed then
control again goes back at the beginning,
and the condition is checked if it is true, the
same process is executed until the condition
becomes false.
▪ Once the condition becomes false, the
control goes out of the loop.
▪ The body of a loop can contain more than
one statement.
▪ After exiting the loop, the control goes to the
statements which are immediately after the
loop.
43
EXAMPLE
Write an algorithm to print integers 1 to N
44
Finding the largest integer among
five positivie integers
45
Finding the largest integer among
five positivie integers
46
Defining actions in FindLargest algorithm
47
FindLargest refined
48
Generalization of FindLargest
49
EXAMPLE OF FINDING THE LARGEST INTEGER
Write an algorithm to take 5 positive integer numbers and print
the maximum of them
Using while statement
1. Start 1. Start
2. Set Max = 0, Count = 1 2. Set Max = 0, Count = 1
3. Read N 3. While Count ≤ 5
4. If N > Max then 4. Read N
5. Set Max = N 5. If N > Max then
6. End If 6. Set Max = N
7. If Count < 5 then 7. End If
8. Set Count = Count + 1 8. Set Count = Count + 1
9. Goto Line 3 9. End While
10.End If 10.print “The max number is Max”
11.print “The max number is Max” 11.End
12.End
50
TRACING AN ALGORITHM
▪ To find possible bugs in the algorithm we debug it
▪ One way to debug the algorithm is tracing it with
some input
▪ Trace:
▪ Follow the steps from the start to the end
▪ Execute the steps with the given input
▪ Keep the track of variables values
▪ Check the results in each step until the end of the algorithm
51
EXAMPLE - TRACING THE ALGORITHM
Write an algorithm to take 5 integer numbers and print the
maximum of them
Trace the algorithm for:
1. Start 12, 8, 13, 9, 11
2. Set Max = 0, Count = 1
3. While Count ≤ 5 Max | Count | N (input) | output
4. Read N 12 1 12
5. If N > Max then 2 8
6. Set Max = N 13 3 13
7. End If 4 9
8. Set Count = Count + 1 5 11
9. End While 6 13
10.print “The max number is Max”
11.End
52
EXAMPLE
53
EXAMPLE
Write an algorithm to take integer N print N!
Using while statement
1. Start 1. Start
2. Read N 2. Read N
3. Set Fact = 1, Count = 1 3. Set Fact = 1, Count = 1
4. Set Fact = Fact * Count 4. While Count ≤ N
5. Set Count = Count + 1 5. Set Fact = Fact * Count
6. If Count ≤ N then 6. Set Count = Count + 1
7. Goto Line 4 7. End While
8. print “The Factorial of N is Fact”
8. End If 9. End
9. print “The Factorial of N is Fact”
10.End
54
EXAMPLE - TRACING THE ALGORITHM
Write an algorithm to take integer N print N!
55