67% found this document useful (3 votes)
3K views

Ch03 Flowchart and Pseudo Code Examples

The document provides examples of flowcharts and pseudocode to calculate sums, averages, and pay with or without overtime. It includes sequences, selections, and iterations. The key examples are: 1) Calculating the sum of two numbers as a sequence of steps 2) Calculating pay as hours times rate, with inputs for hours and rate 3) Calculating the average of three numbers by summing inputs then dividing by 3 4) Calculating pay with overtime by selecting normal pay if hours are below 40, overtime pay if above 5) Calculating the average of 10 numbers with a while loop iteration 6) Calculating the average of 10 numbers with a for loop iteration

Uploaded by

Lee Kah Hou
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
67% found this document useful (3 votes)
3K views

Ch03 Flowchart and Pseudo Code Examples

The document provides examples of flowcharts and pseudocode to calculate sums, averages, and pay with or without overtime. It includes sequences, selections, and iterations. The key examples are: 1) Calculating the sum of two numbers as a sequence of steps 2) Calculating pay as hours times rate, with inputs for hours and rate 3) Calculating the average of three numbers by summing inputs then dividing by 3 4) Calculating pay with overtime by selecting normal pay if hours are below 40, overtime pay if above 5) Calculating the average of 10 numbers with a while loop iteration 6) Calculating the average of 10 numbers with a for loop iteration

Uploaded by

Lee Kah Hou
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Sum of 2 Numbers - sequence

Introductory Examples of Flowcharts and Pseudocode


Chapter 3
Start Begin
input x, y
Calculate Pay - sequence
sum = x + y
print sum
input x End
Start input y
Begin
input hours
input rate
input hours pay = hours * rate
print pay
End sum = x + y

input rate

output sum
pay = hours * rate

End

print pay

End

1 2
Average of 3 Numbers - sequence Calculate Pay with Overtime - selection

Start
Begin
input x
input y
input x input z
input y sum = x + y + z input hours, rate
input z avg = sum / 3.0
print avg
End
sum = x + y + z
F
avg = sum / 3.0
hours ≤ 40

T
print avg
pay = 40 * rate +
pay = hours * rate (hours - 40) * 1.5 * rate

End

print pay

Begin
input hours, rate
if hours ≤ 40 then
pay = hours * rate
else
pay = 40 * rate + (hours – 40) * rate * 1.5
print pay
End

3 4
Average of 10 Numbers – iteration with a while loop Average of 10 Numbers – iteration with a for loop

Begin
sum = 0
i = 0 for i = 1 to 10
sum = 0 input x
sum = 0 sum = sum + x
avg = sum / 10.0
print avg
i End
while i < 10 1
1 10
F
1
T

input x avg = sum / 10.0


input x

sum = x + sum print avg


increment i
sum = x + sum

Begin Begin
i = 0 i = 0
sum = 0 sum = 0 avg = sum / 10.0
while i < 10 a: if i ≥ 10 goto b
input x input x
sum = sum + x sum = sum + x
++i ++i print avg
avg = sum / 10.0 goto a
print avg b: avg = sum / 10.0
End print avg
End

Comment Strictly speaking, the above flowchart corresponds more to the


pseudocode on the right hand side. However, as you can see, ‘gotos’ make
code less modular and more unreadable.

5 6
Flowchart for Function or Subroutine Module

Begin
Begin
print “Input 3 numbers: ”
input a, b, c
print “input 3 numbers” avg = average(a, b, c)
input a, b, c print “Average is ”, avg
End

Average

print “average is”, avg


input a, b, c

End

Average
Begin Average(a, b, c)
sum = a + b + c
avg = sum / 3.0
return avg
sum = a + b + c End
avg = sum / 3

Exit

You might also like