Вы находитесь на странице: 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