PreludeProgramming6ed pp06 PDF
PreludeProgramming6ed pp06 PDF
Flowchart using a
selec8on structure with
a loop to keep track of
the number of posi8ve
and nega8ve numbers
entered
If
TheSpace =
then:
MyLength = Length_Of(TheSpace)
assigns
the
value
of
1
to
MyLength because
a
space
counts
as
one
character.
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
Code
Code
Write Hi
Write Ho
Write Done
Print Hi
Print Ho <NL>
Print Done
Display
Display
Hi
Ho
Done
HiHo
Done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
NewNumber = Floor(Random()
number
between
1
and
10
(inclusive)
NewNumber = Floor(Random()
number
between
1
and
100
(inclusive)
NewNumber = Floor(Random()
number
between
4
and
13
(inclusive)
NewNumber = Floor(Random()
NewNumber = Floor(Random()
A[er
examining
these
examples,
we
can
conclude
that,
to
generate
a
sequence
of
N
random
integers
beginning
with
the
integer
M,
use:
Floor(Random() * N) + M
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
2 + 1 = 3
3 + 1 = 4
4 + 1 = 5
5 + 1 = 6
6 + 1 = 7
1 + 2 = 3
2 + 2 = 4
3 + 2 = 5
4 + 2 = 6
5 + 2 = 7
6 + 2 = 8
1 + 3 = 4
2 + 3 = 5
3 + 3 = 6
4 + 3 = 7
5 + 3 = 8
6 + 3 = 9
1 + 4 = 5
2 + 4 = 6
3 + 4 = 7
4 + 4 = 8
5 + 4 = 9
6 + 4 = 10
1 + 5 = 6
2 + 5 = 7
3 + 5 = 8
4 + 5 = 9
5 + 5 = 10
6 + 5 = 11
1 + 6 = 7
2 + 6 = 8
3 + 6 = 9
4 + 6 = 10
5 + 6 = 11
6 + 6 = 12
Possible
ways
to
roll
a
5:
(1,4), (4,1), (2,3), (3,2)
If
the
starJng
value
of
the
algorithm
never
changes,
the
same
sequence
of
numbers
will
be
produced
each
Jme
the
program
is
executed.
This
may
be
useful
for
debugging
purposes.
The
programmer
must
force
the
computer
to
use
a
dierent
seed
on
each
run
so
that
the
random
numbers
produced
will
be
unpredictable.
use
a
seed
that
is
not
predetermined,
like
the
number
of
milliseconds
since
the
beginning
of
the
current
year
it
will
only
occur
once
a
year
forces
a
random
number
generator
to
start
with
a
dierent
seed
each
Jme
Example
1
2
3
4
5
6
7
8
OutCount
InCount
output
Any style of loop may be nested. Each nested loop must be indented to indicate
which statements are controlled by which looping construct.
Do
Code Statements
While <condition>
More code may be here
For (initial condition; test; increment)
more code here
End For
More code may be here
End While
More code may be here
While <condition>
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
Example:
Drawing
Squares
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Declare X, Y, Z As Integer
For (X = 1; X < 4; X++)
Write Pass Number + X
For (Y = 1; Y < 10; Y+3)
Set Z = X + Y
Write X + + + Y + = + Z
End For(Y)
End For(X)
output
Pass Number 1
1 + 1 = 2
1 + 4 = 5
1 + 7 = 8
10
10
Pass Number 2
2 + 1 = 3
2 + 4 = 6
2 + 7 = 9
10
10
Pass Number 3
3 + 1 = 4
3 + 4 = 7
10
3 + 7 = 10
10
10
10
10
Count1
Count2
Pass Number 1
X = 2, Y = 3, Z = 6
3
3
6
Declare X, Y, Z As Integer
Declare Count1 As Integer
3
3
9
Declare Count2 As Integer
4
3
9
Set Y = 3
4
3
12
Set Count1 = 1
5
3
12
Do
Set X = Count1 + 1
Set Count2 = 1
Write Pass Number + Count1
While Count2 <= Y
Set Z = Y * X
Write X = + X + , Y = + Y + , Z =
Set X = X + 1
Set Count2 = Count2 + 1
End While
Set Count1 = Count1 + 1
While Count1 < Y
output
X = 3, Y = 3, Z = 9
X = 4, Y = 3, Z = 12
And so on...
+ Z
Mind
Games:
Workout
#3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Declare A, B, C As
Set A = 1
Write Cheers!
While A < 3
Set C = A
Write A
Set B = 1
While B < 4
Set C
Write
If (A
Integer
= C + A
C
== 1 AND C >= 4) Then
Write Lets do this some more!
Else
If (A == 2 AND C >= 8) Then
Write Who do we appreciate?
End If
End If
Set B = B + 1
End While
Set A = A + 1
End While
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
Mind
Games:
Workout
#3
Follow
Along
output
Cheers!
Who do we appreciate?
2
3