Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Programming (1)

Lecture 2

Dr. Esraa M. Elhariri


Computer Science Department,
Faculty of Computers and Information
Fayoum University
2021-2022
Flowcharts and Pseudocode

Programming (1) 2021-2022


Algorithm Design
Algorithm can be described in three ways.
1) Natural language like English: 2) Graphic representation called
When this way is chosen care flowchart: This method will work
should be taken, we should well when the algorithm is
ensure that each & every small& simple.
statement is definite.

- Starting with the first item in S,


- compare x with each item in S in
sequence until x is found or until
S is exhausted.
- If x is found, answer yes; if x is
not found, answer no.

Programming (1) 2021-2022


Algorithm Design

• 3) Pseudo-code Method: In this method, we should typically


describe algorithms as program.
 The pseudocode is semi-formal similar, but not identical, to
C++.
 Not discuss the implementation of algorithms in any
particular programming language.
 It aims to present algorithms clearly so they can be readily
understood and analyzed.

Programming (1) 2021-2022


Pseudocodes

 A pseudocode can also be defined as a semiformal, English-like


language with limited vocabulary that can be used to design and
describe algorithms.

 Criteria of a good pseudocode:

 Easy to understand, precise and clear

 Gives the correct solution in all cases

 Eventually ends

Programming (1) 2021-2022 5


Flowcharts

 Flowcharts is a graph used to depict or show a step by step


solution using symbols which represent a task.

 The symbols used consist of geometrical shapes that are


connected by flow lines.

 It is an alternative to pseudocoding; whereas a pseudocode


description is verbal, a flowchart is graphical in nature.

Programming (1) 2021-2022 6


Flowchart Symbols
Terminal symbol - indicates the beginning
and
end points of an algorithm.
Process symbol - shows an instruction other
than input, output or selection.
Input-output symbol - shows an input or an
output operation.

Disk storage I/O symbol - indicates input


from or output to disk storage.
Printer output symbol - shows hardcopy
printer output.
Programming (1) 2021-2022 7
Flowchart Symbols cont…
Selection symbol - shows a selection
process for two-way selection.

Off-page connector – provides continuation


of a logical path on another page.

On-page connector - provides continuation


of logical path at another point in the same
page.

Flow lines - indicate the logical sequence of


execution steps in the algorithm.

Programming (1) 2021-2022 8


Flowchart Symbols cont…
Function / Subroutine: Used to identify an
operation in a separate flowchart segment
(module). One flow line enters and one flow
line exits.
Comment: Used to add descriptions or
clarification.
Control Structure
 An algorithm can be represented using Pseudocode or Flowchart.

 Any algorithm can be described using only 3 control structures:


sequence, selection and repetition.

Programming (1) 2021-2022 10


Comments or description

Start

Read N, N = The number of students


M M = The number of subjects

Yes

No

Stop
Start

1 2

1- connection on the same


flowchart portion

2- connection on the different


flowchart portion
Stop

2
Connectors on a different page
Page 1 Page 2

Start

2
1

Stop
Yes 1

No

2
The detail of how the function works
is put in another flowchart.

This is known as Function-Definition


Function
Page 1 Start terminal for a Page 2
Function is different.
Start Do not use “Start”
AVRG ( result,n1, n2,n3)

Read
n1, n2 , n3

sum = n1+ n2+n3

Body of a function is
AVRG (result, n1, n2,n3) the same with
normal flowchart

At this point, result = sum/3


we only focus on what
to do. How to do it,
it comes later. Print
result
This part is known as
Function-Call Return

Stop
End terminal
must be a “Return”

This flowchart calculates the average of three numbers


Example:
What is the output of the following flowchart when the input is N = 6
10
Page 1 Page 2 5
average

Start
N=6
AVRG ( result,n1, n2,n3)

Read
N Sum = 10 + 5 + 6

sum = n1+ n2+n3

AVRG (average, 10, 5, N) average =


21/3

result = sum/3

Print
average
Output:
Average: 7
Return

Stop
Example:

Start

Read
Length,
Input:
Width Length <- 5
Width <- 3

Calculate Area Process:


Area=Length * Width Area = 5 * 3 = 15

Calculate Perimeter Process:


Perimeter= Perimeter =
2 * (Width+Length) 2* (5+3) = 16
Output

Area: 15
Print
Area, Perimeter: 16
Perimeter

Stop
Control Structure
 Sequence: A series of steps or statements that are executed in
the order they are written in an algorithm.

 Selection: Defines two courses of action depending on the


outcome of a condition. A condition is an expression that is, when
computed, evaluated to either true or false.

 Repetition: Specifies a block of one or more statements that are


repeatedly executed until a condition is satisfied.

Programming (1) 2021-2022 17


You may have more than
one control structure in
one program in order to
solve a problem.

Programming (1)
18 2021-2022
The Sequence control structure

 A series of steps or statements that are executed in the order they


are written in an algorithm.

 The beginning and end of a block of statements can be optionally


begin
marked with the keywords begin and end.
begin
statement 1
statement 1.
statement 2. statement 2

… …
statement n. statement n
end
end
Programming (1) 2021-2022 19
The Sequence control structure

Problem: calculate a person’s age begin

Begin read birth year


read birth year
age = current year – birth year
display age Age = current year –
birth year
End

Display age

end

Programming (1) 2021-2022 20


The Selection control structure

 Defines two courses of action depending on the outcome of a


condition. A condition is an expression that is, when computed,
evaluated to either true or false.

 The keyword used are if and else. No


Condition?
Yes

 Format:
if (condition)
then-part
else- then-
else statement(s) statement(s)
else-part
end_if

Programming (1) 2021-2022


22
The Selection control structure
Begin Begin

read age
if (age is greater than 55) Read age
print "Retired"
else
print "Still working" end_if YES
age > 55?
NO
End

print "Retired" print "Still working"

Begin
read age
if (age > 55) End
print "Retired"
else
print "Still working"
end_if End

Programming (1) 2021-2022


Pseudocodes: One way Selection

• Sometimes in certain situation, we may omit the else-part.


• In one-way selection:

 The statements are executed if the condition is satisfied (true).

 Does nothing when condition is not satisfied (false).

Programming (1) 2021-2022


Pseudocodes: The Selection control
structure
if (number is positive) if (number is multiple of 5)
print number Add 1 to number
end_if print number
end_if

Programming (1)
24 2021-2022
Pseudocodes: The Selection control structure

 Nested selection structure: basic selection structure that


contains other if/else structure in its then-part or else-part.
if (number is equal to 1)
print "One"
else if (number is equal to 2)
print "Two"
else if (number is equal to 3)
print "Three"
else
print "Other"
end_if

Programming (1) 2021-2022 25


Exercise

Draw the flowchart diagram for a program which


compares three numbers

Programming (1)
26 2021-2022
The Repetition control structure
 Specifies a block of one or more statements that are repeatedly
executed until a condition is satisfied.

 The keyword used is while.

Format:
while (condition)
loop-body
end_while yes Loop
Condition?
Statement(s)

no

Programming (1) 2021-2022


Problem: Write a program that reads and displays the age of
10 people (one after another).

For this problem, we need a way to count how many people


whose age have been processed (read and displayed).
Therefore, we introduce a concept of counter, a variable used
to count the number of people whose age have been
processed by the program.

Programming (1)
28 2021-2022
Begin Counter initialisation
number of users giving his age = 1
while (number of users giving his age <= 10)
read the age from the user.
print the user age. Loop condition
number of user giving his age + 1
end_while
Updating counter
End

Begin
users = 1
while (users <= 10)
read age
print age.
users = users + 1
end_while
End

Programming (1)
29 2021-2022
Begin

users = 1

NO
End users <= 10?

YES

read age

print age

users =users + 1

Programming (1)
30 2021-2022
Subsequently..
Begin You can start the
counter with ZERO
number of users giving his age = 0
while (number of users giving his age < 10)
read the age from the user.
print the user age.
number of user giving his age + 1 The loop condition
must less than the
end_while value it requires to
End stop

Begin
users = 0
while (users < 10)
read age
print age. Be
users = users + 1 consistent
end_while
End

Programming (1)
31 2021-2022
Exercise time!!!

Programming (1) 2021-2022 32


Volume calculation

• Draw a flowchart for a program that reads the value of height,


width and length of a box from the user and prints its volume.

Programming (1) 2021-2022 33


Summary

 This Lecture introduced the concept of programming and


problem solving: a process of transforming the description of a
problem into a solution.

 A commonly used method – SDM which consists of 7 steps

 3 basic control structures : sequence, selection and repetition


structures

 Pseudocode and Flowchart.

Programming (1) 2021-2022 34


See You Next Lecture

Programming (1)
35 2021-2022

You might also like