0% found this document useful (0 votes)
78 views

Lab 5 - Flow Chart and PseudoCode

The document describes a flowchart for calculating an employee's gross pay. The flowchart begins with a starting point and ends with an ending point. It contains input/output operations like displaying messages and reading values entered by the user. It also includes a process - multiplying the number of hours worked by the hourly pay rate to calculate the gross pay, and stores the result in a variable. By stepping through the flowchart, we can see the program flow and how the variable values change at each step.

Uploaded by

ahmed barakat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Lab 5 - Flow Chart and PseudoCode

The document describes a flowchart for calculating an employee's gross pay. The flowchart begins with a starting point and ends with an ending point. It contains input/output operations like displaying messages and reading values entered by the user. It also includes a process - multiplying the number of hours worked by the hourly pay rate to calculate the gross pay, and stores the result in a variable. By stepping through the flowchart, we can see the program flow and how the variable values change at each step.

Uploaded by

ahmed barakat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 74

Introduction to Computers Lab First Year 2019– 2020

1
Agenda
• Flow Chart

• Pseudo-code

• Exercises

2
What is a Flowchart?
START

Display message
“How many
hours did you
work?”

• A flowchart is a Read Hours

diagram that Display message


“How much do
depicts the “flow” you get paid per
hour?”

of a program.
Read PayRate
• The figure shown
here is a flowchart Multiply Hours
by PayRate. Store

for the pay-


result in
GrossPay.

calculating. Display
GrossPay

END

3
Basic Flowchart START Terminal

Symbols Display message


“How many
hours did you
work?”

• Terminals Read Hours

o represented by Display message

rounded rectangles
“How much do
you get paid per
hour?”
o indicate a starting
or ending point Read PayRate

Multiply Hours
by PayRate. Store
START result in
GrossPay.

Display
GrossPay
END Terminal
END

4
Basic Flowchart START

Symbols Display message


“How many
hours did you
work?”

• Input / Output Read Hours

Operations Display message


“How much do
o represented by
Input/Output
you get paid per
hour?” Operation
parallelograms
o indicate an input or Read PayRate

output operation Multiply Hours


by PayRate. Store
Display result in
GrossPay.
message “How
many hours did Read Hours Display
GrossPay
you work?”
END

5
Basic Flowchart START

Symbols Display message


“How many
hours did you
work?”

• Processes Read Hours

o represented by
rectangles Display message
“How much do
you get paid per
o indicates a process hour?”

such as a Read PayRate


mathematical
computation or Multiply Hours

variable assignment
by Pay Rate.
Process Store result in
GrossPay.
Multiply Hours
by PayRate. Display
GrossPay
Store result in
GrossPay. END

6
Stepping Through START
Output

the Flowchart Display message


“How many
hours did you
work?”
Operation

Read Hours
How many
hours did
you work?
Display message
“How much do
you get paid per
hour?”

Read PayRate

Multiply Hours
by PayRate. Store
result in
Variable Contents: GrossPay.

Hours: ?
PayRate: ? Display
GrossPay
GrossPay: ?
END

7
Stepping Through START

the Flowchart Display message


“How many
hours did you
work?”

How many
Input Read Hours
hours did Operation
you work?
(User types 40) Display message
40
“How much do
you get paid per
hour?”

Read PayRate

Multiply Hours
by PayRate. Store
result in
GrossPay.
Variable Contents:
Hours: 40
Display
PayRate: ? GrossPay
GrossPay: ?
END

8
Stepping Through START

the Flowchart Display message


“How many
hours did you
work?”

Read Hours
How much
do you get
paid per
Display message
hour?
“How much do
Output you get paid per
Operation hour?”

Read PayRate

Multiply Hours
by PayRate. Store
result in
GrossPay.
Variable Contents:
Hours: 40 Display
PayRate: ? GrossPay

GrossPay: ?
END

9
Stepping Through START

the Flowchart Display message


“How many
hours did you
work?”

Read Hours
How much
do you get
paid per
Display message
hour? 20
“How much do
you get paid per
hour?”

Input Read PayRate


Operation
(User types 20) Multiply Hours
by PayRate. Store
result in
Variable Contents: GrossPay.

Hours: 40
PayRate: 20 Display
GrossPay
GrossPay: ?
END

10
Stepping Through START

the Flowchart Display message


“How many
hours did you
work?”

Read Hours
How much
do you get
paid per
Display message
hour?
“How much do
you get paid per
hour?”

Read PayRate

Multiply Hours
Process: The by PayRate. Store
result in
product of
Variable Contents: 40 times 20 is
GrossPay.

Hours: 40 stored in
PayRate: 20 Gross Pay Display
GrossPay
GrossPay: 800
END

11
Stepping Through START

the Flowchart Display message


“How many
hours did you
work?”

Read Hours
Your gross
pay is 800
Display message
“How much do
you get paid per
hour?”

Read PayRate

Multiply Hours
by PayRate. Store
result in
Variable Contents: GrossPay.

Hours: 40
PayRate: 20 Output Display
Operation GrossPay
GrossPay: 800
END

12
Four Flowchart Structures
• Sequence
• Decision
• Case
• Repetition

13
Sequence Structure
• A series of actions are performed in
sequence
• The pay-calculating example was a
sequence flowchart.

14
Decision Structure
• One of two possible actions is taken,
depending on a condition.

NO YES

15
Decision Structure
• A new symbol, the diamond, indicates a yes/no
question. If the answer to the question is yes, the
flow follows one path. If the answer is no, the flow
follows another path

NO YES

16
Decision Structure
• In the flowchart segment below, the question “is x <
y?” is asked. If the answer is no, then process A is
performed. If the answer is yes, then process B is
performed.

NO YES
x < y?

Process A Process B

17
Decision Structure
• The flowchart segment below shows how a decision
structure that multiplies x by 2 if x is less than y.
otherwise, it adds x to y

NO YES
x < y?

Calculate a Calculate a
as x plus y. as x times 2.

18
Case Structure
• One of several possible actions is taken,
depending on the contents of a variable.

19
Case Structure
• The structure below indicates actions to
perform depending on the value in
years_employed.
CASE
years_employed

=1 =2 =3 Otherwise

bonus = 100 bonus = 200 bonus = 400 bonus = 800

20
Case Structure

If years_employed = If years_employed =
2, bonus is set to 200 3, bonus is set to 400
If years_employed = If years_employed is
CASE
1, bonus is set to 100 years_employed any other value,
bonus is set to 800

=1 =2 =3 Otherwise

bonus = 100 bonus = 200 bonus = 400 bonus = 800

21
Repetition Structure
• A repetition structure represents part of the program
that repeats. This type of structure is commonly
known as a loop.

22
Repetition Structure
• Notice the use of the diamond symbol. A loop tests
a condition, and if the condition exists, it performs
an action. Then it tests the condition again. If the
condition still exists, the action is repeated. This
continues until the condition no longer exists.

23
Repetition Structure
• In the flowchart segment, the question “is x < y?” is
asked. If the answer is yes, then Process A is
performed. The question “is x < y?” is asked again.
Process A is repeated as long as x is less than y.
When x is no longer less than y, the repetition stops
and the structure is exited.

YES
x < y? Process A

24
Repetition Structure
• The flowchart segment below shows a repetition
structure that increments x by 1 as long as x is less
than y.

YES
x < y? Add 1 to x

25
Controlling a Repetition
Structure
• The action performed by
a repetition structure must
eventually cause the loop
to terminate. Otherwise,
an infinite loop is created.
• In this flowchart segment,
x is never changed. Once
the loop starts, it will never
YES
end. Display x
x < y?
• QUESTION: How can this
flowchart be modified so
it is no longer an infinite
loop?
26
Controlling a Repetition
Structure
• ANSWER: By adding an action within the repetition
that changes the value of x.

YES
x < y? Display x Add 1 to x

27
A Pre-Test Repetition
Structure
• This type of structure is known as a pre-test
repetition structure. The condition is tested BEFORE
any actions are performed.

YES
x < y? Display x Add 1 to x

28
A Pre-Test Repetition
Structure
• In a pre-test repetition structure, if the condition
does not exist, the loop will never begin.

YES
x < y? Display x Add 1 to x

29
A Post-Test Repetition
Structure
• This flowchart segment
shows a post-test
repetition structure.
• The condition is tested
Display x
AFTER the actions
are performed.
• A post-test repetition Add 1 to x
structure always
performs its actions at
YES
least once. x < y?

30
WHILE Loop (pre-test) Expression is
evaluated at the
start of each
iteration of the loop

Expression
If expression is false
true ,Action is
executed true

If expression is
Action false, program
execution
continues with
next statement
outside the loop

31
DO WHILE Loop(post-test)

Execute Action first

Action
Action

Expressio true
Expression
n

Expression is evaluated at false


the End , If Expression is
true then execute Action
again , Repeat this
process until Expression
evaluates to false
32
FOR Loop
Executed once at the Condition is evaluated at the
beginning of for loop Initialization start of each iteration of the
loop

False
If condition is true the Condition
Action is executed

True

After the action has Action If condition is false ,


completed , the increment program execution
is evaluated , then the continues with next
next iteration of the loop statements.
starts. Increment

33
WHILE Loop
Start

Program that increments an integer by


count = 0 one and displays the new value as long
as it is less than 10.

count no
<10

yes
Write
add 1 to “The End”
count

Stop
write count

34
Start
DO WHILE Loop
count = 0
Program that increments an
integer by one and displays the
new value as long as it is less
than 10.
add 1 to
count

write count

yes count
<10

no
Write
“The End”

Stop 35
FOR Loop
Start

Program that increments an integer by


count = 0 one and displays the new value as long
as it is less than 10.

count no
<10
yes
Write
“The End”
write count

add 1 to Stop
count

36
Connectors
•The “A” connector
indicates that the START A

second flowchart
segment begins
where the first
segment ends.
END
A

37
Subroutines (Modules)
•A program module (such as a
START

function in C++) is represented Read Input.


by a special symbol.
•The position of the module Call calc_pay
symbol indicates the point the function.

module is executed.
Display
•A separate flowchart can be results.

constructed for the module. END

38
Combining Structures
• Structures are commonly combined to create more
complex algorithms.
• The flowchart segment below combines a decision
structure with a sequence structure.

YES
x < y? Display x Add 1 to x

39
Combining Structures
• This flowchart
segment
shows two NO YES
decision x > min?

structures
combined. Display “x is
outside the
NO YES
limits.” x < max?

Display “x is outside Display “x is


the limits.” within limits.”

40
Pseudo-code

41
Rules for Pseudo-code
• Write only one statement per line

• Capitalize initial keyword

• Indent to show hierarchy

• End multiline structures

• Keep statements language independent

42
Rules for Variable Names
• Begin with lowercase letter
• Contain no spaces
• Additional words begin with capital
• Unique names within code
• Consistent use of names

43
Advantages & Disadvantages
Flowchart Advantages: Pseudocode
 Standardized Advantages
 Visual  Easily modified
 Done easily on Word
Processor

Flowchart Pseudocode
Disadvantages: Disadvantages:
 Hard to modify  Not visual
 Special software required  No accepted standard, varies
from company to company

44
Flow chart review
• What do each of the following symbols
represent?

Decision
Terminal

Input/ Output
Operation Connector

Process Module

45
Review
• What type of structure is this?

Repetition

46
Review
• What type of structure is this?

Sequence

(Answer on next slide)


47
Review
• What type of structure is this?

Case

(Answer on next slide)


48
Review
• What type of structure is this?

Decision

(Answer on next slide)


49
Working with Fields
Calculations Selection
+ add > greater than
- subtract < less than
* multiply = equal to
/ divide >= greater than or equal to
** or ^ exponentiation <= less than or equal to
() grouping <> not equal to
% modulo

50
Logical Operators
NOT (!)
AND (&&)
OR (||)

• Logical operators are used to combine several


conditions into one compound condition
• The outcome of condition is True or False
• Operators above are listed in order of precedence
Not is evaluated first, then AND, then OR
• The relational operators have higher precedence that
the logical operators
• It is usually best to include the extra parentheses
• These operators are language dependent, they do not
exist in some languages such as Python and PhP

51
Logical Operators: Examples
IF (x = 32) AND (y = 7) THEN
sumXY = x + y

IF (letter = 'A') OR (letter = 'E') THEN


DISPLAY “Vowel”

IF NOT (letter = 'A') THEN


DISPLAY “Not letter A”

52
Exercises

53
Exercise 1
• Algorithm for program that reads a number and
determines whether it is positive, negative or zero.

54
Exercise 1: Solution
START

Input
number BEGIN
READ number
Number True IF number > 0 THEN
>0 PRINT “Positive Number”
Print
False “Positive Number” ELSE IF Number < 0 THEN
PRINT “Negative Number”
number True
<0
Print ELSE
False “Negative Number” PRINT ”Zero”

Print ENDIF
“Zero” END

END 55
Exercise 2
• Algorithm for program that checks whether an
input number lies within a specified range.
For example: If user enters range (20, 50) and
queried number 34, the program should display “In
range”.
On the other hand, if he enters 76, the program
should display “Not in Range”.

56
Exercise 2: Solution
• Using nested IF structure
START

Input
startRange, endRange,
number

number True
False
>=
startRange

False number
<=
endRange
Print
”Not in Range”
True

Print
”In Range”

END 57
Exercise 2: Solution

BEGIN
READ startRange, endRange, number

IF ( number >= startRange) THEN


IF (number <= endRange)
PRINT “In Range”
ELSE
PRINT “Not in Range”
ENDIF
ELSE
PRINT “Not in Range”
ENDIF
END

58
Exercise 2: Another Solution
• Using logical operators in IF condition
START

Input
startRange, endRange,
number

number >= startRange


False AND True
number <= endRange
Print Print
”Not in Range” ”In Range”

END 59
Exercise 2: Another Solution

BEGIN
READ startRange, endRange, number

IF ( number >= startRange) AND (number <= endRange) THEN


PRINT “In Range”

ELSE
PRINT “Not in Range”
ENDIF
END

60
Exercise 3
• Algorithm for program that determines whether a
baby’s weight is normal or not.
For girls, normal babies weight are 2.5 to 4.5 KG.
On the other hand, for boys the normal weights are
4 to 5.5 KG.

61
Exercise 3: Solution
START

Input
gender, weight

False (gender = ‘F’) AND True


(weight >= 2.5) AND
(weight <= 4.5)

(gender = ‘M’) AND True Print


(weight >= 4) AND
(weight <= 5.5) “Normal Weight”

False

Print
“Not Normal Weight”

END 62
Exercise 3: Solution
BEGIN
READ gender, weight

IF (gender = ‘F’) AND (weight >= 2.5) AND (weight <= 4.5) THEN
PRINT “Normal Weight”

ELSE IF (gender = ‘M’) AND (weight >= 4) AND (weight <= 5.5) THEN
PRINT “Normal Weight”

ELSE
PRINT “Not Normal Weight”

ENDIF
END

63
Exercise 3: Another Solution
START

Input
gender, weight

((gender = ‘F’) AND


(weight >= 2.5) AND
False (weight <= 4.5)) OR True
((gender = ‘M’) AND
(weight >= 4) AND
(weight <= 5.5))

Print Print
“Not Normal Weight” “Normal Weight”

END

64
Exercise 3: Another Solution
BEGIN
READ gender, weight

IF ((gender = ‘F’) AND (weight >= 2.5) AND (weight <= 4.5)) OR
((gender = ‘M’) AND (weight >= 4) AND (weight <= 5.5)) THEN
PRINT “Normal Weight”
ELSE
PRINT “Not Normal Weight”
ENDIF

END

65
Exercise 4
• Algorithm for a calculator that works on integer
numbers. The user enters two numbers to perform
only one of four basic arithmetic operations (+, -, *
and /).
• The interaction with the user might look like this:
Enter your expression:
1 2 +
The result is: 3
• Hint: The user is allowed to do only one operation at
a time.

66
Exercise 4: Solution
• Using case structure
START

Input

num1, num2, op

Case of
op
‘+’ ‘-’ ‘*’ ‘/’ Otherwise
result = result = result = result = Print
num1+num2 num1-num2 num1*num2 num1/num2 “Invalid Operation”

Print
result
67

END
Exercise 4: Solution
BEGIN
PRINT “ Enter your expression: “
READ num1, num2 , op

CASE OF op:
= ‘+’:
result = num1 + num2
=‘-’:
result = num1 - num2
=‘*’:
result = num1 * num2
=‘/’:
result = num1 / num2
OTHERWISE:
PRINT “Invalid Operation”
END-OF-CASE
PRINT “The result is: ”, result
END
68
Exercise 5
• Algorithm for program that converts seconds to
equivalent hours, minutes and seconds.

69
Exercise 5: How to Solve?
totalSeconds = 4000
How to convert to hours, minutes and seconds?

• First, to calculate the number of seconds, get the remainder of


dividing totalSeconds by 60 and print it.
seconds = 4000 % 60 = 40 seconds.

• Second, to calculate the number of minutes, divide


totalSeconds by 60, then get the remainder of dividing it by 60
and print it.
totalSeconds= 4000/60 = 66.666 then 66.666 % 60=6 minutes

• Third, to calculate the number of hours, divide totalSeconds


by 60 and print it.
hours = 66.666 /60= 1hour

70
Exercise 5: Solution
START

Input A BEGIN
totalSeconds READ totalSeconds
Print

Seconds = minutes
seconds = totalSeconds % 60
totalSeconds %60
PRINT “Seconds: ”, seconds
totalSeconds =

Print
totalSeconds / 60 totalSeconds = totalSeconds / 60
seconds minutes = totalSeconds % 60
hours =
PRINT “ , Minutes: ”, minutes
totalSeconds = totalSeconds

totalSeconds / 60 totalSeconds = totalSeconds / 60


Print
hours = totalSeconds
hours
Minutes = PRINT “Hours: ”, hours
totalSecods % 60 END
END
A
71
Exercise 6
• Algorithm for a program to swap the values of
two integers using third variable.

72
Exercise 6: Solution
START

Input BEGIN
first, second READ first, second

third = first
third = first
first = second
first = second
second = third
second = third
PRINT “The first number: ”, first
Print PRINT “The second number: ”, second
first, second
END
END

73
Thank You

74

You might also like