Al-Mustaqbal University
College of Engineering & Technology
Biomedical Engineering Department
Computer
Lecture 2
Algorithms Development – Pseudo-code and Flowchart
PhD. BEng. Ahmed Hasan Janabi
PhD in Cybersecurity
Email: Ahmed.Janabi@uomus.edu.iq
What is an algorithm?
❖ In computer programming, an algorithm is a set of well-defined
instructions to solve a particular problem.
❖ It takes a set of input and produces a desired output.
Step 1: Start
Step 2: Define the variables
Step 3: Read values of
variables
Step 4: Process
▪ math operations
▪ logic operations
▪ comparisons
Step 5: Display result
Step 6: End
Algorithm to add two numbers
❖We need to first write the steps (sequence of actins) that lead to
performing the task.
❖For example, an algorithm to add two numbers:
➢Take two number inputs
➢Add numbers using the + operator
➢Display the result
Step # Description
Step1 Start
Step2 Declare or define variables
num1, num2, sum;
Step3 Read values num1 and num2
Step4 Add num1 and num2 and assign the results to sum
sum num1 + num2;
Step5 Print sum
Step6 End
Flowchart Symbols
➢ A Flowchart is a graphical representation that shows the behavior (workflow) of
an algorithm.
Flowcharts use standard shapes including the following:
Adding two numbers
✓ Write the pseudo-code for algorithm to sum two numbers. Then draw the equivalent flowchart.
➢ Flowcharts use standard shapes including the following:
Ans:
Pseudo-code Algorithm Flowchart
Step 1: START
Step 2: DECLARE variables num1, num2, sum;
Step 3: READ variables num1, num2;
Step 4: CALCULATE sum
sum = num1 + num2;
Step 5: PRINT sum
Step 6: END
Note:
➢ The pseudo-code and flowchart of an algorithm is not written for a particular programming language.
➢ They can be used to plan a solution before coding it.
Print if the condition met
❖ Draw the Flowchart to print the square of a number if the number is less than 5.
Note:
➢ The lines and arrows in it show the sequence of steps and the relationship among them.
Difference between pseudo-code and flowchart
Main differences between Pseudo-code and Flowchart
❖ The pseudo-code is a high-level description of an algorithm while the flowchart is a
graphical representation of an algorithm.
❖ An algorithm is a set of instructions for solving a problem or accomplishing a task.
❖ Every computerized device uses algorithms, which cut the time required to do things
manually.
❖ There are three main ways or scenarios used in the programming. These are:
1) Sequencing – putting things in the right order
2) Conditions – performing different things depending on some rules
3) Iteration (Looping) – repeating operations
Sequencing
❖ Calculate the average of three input numbers.
Pseudo-code Algorithm
Step 1: Start
Step 2: Declare variables Ave;
Step 3: Read variables num1, num2, num3;
Step 4: Calculate Ave
Ave = (num1 + num2 + num3)/3;
Step 5: Print Ave
Step 6: End
Conditions
❖ Conditions are very important to understand the flow-control of the code being
executed.
❖ Conditions are used to make a decision (choose between two or more alternatives
based on the condition)
➢ For example, conditions become very essential to use when facing different directions, so
based on the condition should choose the thing to be executed.
Use the if statement
If (condition) then
run this statement;
Else
run the other statement;
Conditions
Nested if structure
if … then … elseif – Flowchart
Condition – Example
✓ Write the pseudo-code to check if a student has passed the exam or failed. Then draw the equivalent
flowchart?
Ans:
Step 1: Start
Step 2: Declare Result
Step 3: Read the student’s grade
Step 4: If (Grade >= 50)
Result = “passed”
else
Result = “failed”
Step 5: Print result
Step 6: End
Classwork #1 – Student’s Grade
✓ Send a message to a student depending on the obtained score?
Ans:
Step 1: Start
Step 2: Declare Result, message
Step 3: Read the student’s grade
Step 4: If (Grade >= 50)
Result = “pass”
message = “Well Done!”
else
Result = “fail”
message = “See me!”
Step 5: Print result
Step 6: End
Multiple Conditions – Example
✓ Is it possible to have multiple conditions in an algorithm?
❖ For example, print the result of a student according to the conditions listed in the table
below?
Multiple Conditions – Example solution
Ans:
Classwork #2 – Ticket price
✓ Write the pseudo-code algorithm to display the price of a cinema ticket as follow: (age < 12) then
(price = 5K IQD), (age > 65) then (price = 7K IQD); otherwise (price = 10K IQD)?
Ans:
Step 1: Start
Step 2: Declare price
Step 3: Read the customer’s Age
Step 4: If (Age < 12)
price = 5K
Else if (Age > 65)
price = 7K
else
price = 10K
Step 5: Display price
Step 6: End
Iteration –> Repetition
❖ Let’s say we need to print all the numbers from 1 to 1000.
Repeat the operation or sequence of
❖ Would we do this by sequencing? operations until the condition is met
❖ It is much better to do tasks like that using iteration
Iteration – printing the numbers from 1 to 1000
❖ Flowchart
➢ print the numbers from 1 to 1000
Iteration – Summing five numbers
✓ Write the pseudo-code for algorithm to sum five numbers. Then draw the equivalent flowchart?
Ans:
Step 1: Start
Step 2: Initialization
Initialize: sum = 0 and count = 0
Step 3: Enter 𝒏 numbers
Step 4: Calculate sum and increment count
sum = sum + 𝒏
count = count +1
Step 5: while (count < 5)
go to step 3
Step 6: Print sum
Step 7: End
Average of ten numbers – Example
Qualities of Good Algorithms
1. Precision – الدقةthe steps of input and output should be precisely defined.
2. Clarity – الوضوحeach step in the algorithm should be clear and unambiguous.
3. Inputs/Outputs – المدخالت والمخرجاتhas inputs that lead to the production of the output
4. Effectiveness – الفعالیةAlgorithms should solve problems in effective ways such that getting an
5. output from each step based on its inputs and outputs of the previous step
6. Generality – الشمول/ العمومیةthe algorithm should be written in such a way that it can be used in
7. different programming languages.
Thank
You