Presentation on using pseudocode to play computer

Download Report

Transcript Presentation on using pseudocode to play computer

Playing computer with logic
problems
Please use speaker notes for
additional information!
start
firstNum = 10
secondNum = 5
thirdNum = 1
theAns = 0
do while thirdNum < 10
if firstNum > 10
firstNum = firstNum + 5
secondNum = secondNum + 5
thirdNum = thirdNum + 5
else
if secondNum > 10
firstNum = firstNum + 3
secondNum = secondNum + 3
thirdNum = thirdNum + 3
else
secondNum = secondNum + 4
end if
end if
end while loop
theAns = firstNum +secondNum + thirdNum
display theAns
end
To start the program:
firstNum
10
secondNum thirdNum theAns
5
1
0
thirdNum = 1 so thirdNum is less then
10 so I move on to the first if statement
firstNum = 10 so firstNum is NOT
greater than 10 so I go to the else.
secondNum = 5 so secondNum is NOT
greater than 10 so I go to the else
I add 4 to secondNum. Since
secondNum started at 5, it is now 9.
5+4=9
NOW:
firstNum
10
secondNum thirdNum theAns
5
9
1
0
Now I drop through the end if, end if
and end while loop and return to the
do while statement to check and see if
the loop should be done again.
start
NOW:
firstNum = 10
secondNum = 5
firstNum secondNum thirdNum theAns
thirdNum = 1
10
5
1
0
theAns = 0
do while thirdNum < 10
9
if firstNum > 10
firstNum = firstNum + 5
thirdNumis 1 so it is still < 10 so I enter the
secondNum = secondNum + 5
loop
thirdNum = thirdNum + 5
else
firstNum is 10 so is NOT greater than 10 so
if secondNum > 10
firstNum = firstNum + 3
I go to the else
secondNum = secondNum + 3
thirdNum = thirdNum + 3
secondNum is 9 so is NOT greater than 10
else
so I go to the else
secondNum = secondNum + 4
end if
I add 4 to second number which is currently
end if
9. 9 + 4 equals 13
end while loop
theAns = firstNum +secondNum + thirdNum
NOW:
display theAns
end
firstNum secondNum thirdNum theAns
Now I drop through the end if, end if
and end while loop and return to the
do while statement to check and see if
the loop should be done again.
10
5
9
13
1
0
start
firstNum = 10
secondNum = 5
thirdNum = 1
theAns = 0
do while thirdNum < 10
if firstNum > 10
firstNum = firstNum + 5
secondNum = secondNum + 5
thirdNum = thirdNum + 5
else
if secondNum > 10
firstNum = firstNum + 3 + 3
secondNum = secondNum + 3
thirdNum = thirdNum + 3
else
secondNum = secondNum + 4
end if
end if
end while loop
theAns = firstNum +secondNum + thirdNum
display theAns
end
Now I drop through the end if, end if
and end while loop and return to the
do while statement to check and see if
the loop should be done again.
NOW:
firstNum
10
secondNum thirdNum theAns
5
1
0
9
13
thirdNum is still 1 and that means it is less
than 10, so I enter the loop.
firstNum is 10 so it is still not greater than 10
so I go to the else.
secondNum is 13 so secondNum is greater
than 10 so I execute the three statements under
that it.
NOW:
firstNum
10
secondNum thirdNum theAns
5
1
9
13
13
16
4
0
start
firstNum = 10
secondNum = 5
thirdNum = 1
theAns = 0
do while thirdNum < 10
if firstNum > 10
firstNum = firstNum + 5
secondNum = secondNum + 5
thirdNum = thirdNum + 5
else
if secondNum > 10
firstNum = firstNum + 3 + 3
secondNum = secondNum + 3
thirdNum = thirdNum + 3
else
secondNum = secondNum + 4
end if
end if
end while loop
theAns = firstNum +secondNum + thirdNum
display theAns
NOW:
firstNum
10
5
1
0
9
13
13
16
4
thirdNum is 4 and that means it is still less
than 10, so I enter the loop.
firstNum is 13 so it is greater than 10 so I
execute the statements in the if by adding 5
to the numbers
NOW:
firstNum
10
Now I drop through the end if and
end while loop and return to the do
while statement to check and see if the
loop should be done again. Note that I
do not enter the else on the first if so I
do not ask the second question.
secondNum thirdNum theAns
secondNum thirdNum theAns
5
1
9
13
13
16
4
18
21
9
0
start
NOW:
firstNum = 10
firstNum secondNum thirdNum theAns
secondNum = 5
thirdNum = 1
10
5
1
0
theAns = 0
9
do while thirdNum < 10
if firstNum > 10
13
firstNum = firstNum + 5
13
16
4
secondNum = secondNum + 5
thirdNum = thirdNum + 5
18
21
9
else
if secondNum > 10
thirdNum is 9. The do while says to enter the loop
firstNum = firstNum + 3 + 3 if thirdNum is less then 10, so I enter the loop.
secondNum = secondNum + 3
thirdNum = thirdNum + 3
firstNum is 18 so the answer to the firstNum > 10
else
question is yes and I execute the three commands.
secondNum = secondNum + 4
end if
end if
NOW:
end while loop
firstNum secondNum thirdNum theAns
theAns = firstNum +secondNum + thirdNum
display theAns
10
5
1
0
9
Now I drop through the end if and
end while loop and return to the do
while statement to check and see if the
loop should be done again. Note that I
do not enter the else on the first if so I
do not ask the second question.
13
13
16
4
18
21
9
23
26
14
start
firstNum = 10
secondNum = 5
thirdNum = 1
theAns = 0
do while thirdNum < 10
if firstNum > 10
firstNum = firstNum + 5
secondNum = secondNum + 5
thirdNum = thirdNum + 5
else
if secondNum > 10
firstNum = firstNum + 3 + 3
secondNum = secondNum + 3
thirdNum = thirdNum + 3
else
secondNum = secondNum + 4
end if
end if
end while loop
theAns = firstNum +secondNum + thirdNum
display theAns
NOW:
firstNum
secondNum thirdNum theAns
10
5
1
0
9
13
13
16
4
18
21
9
23
26
14
thirdNum is 14 so it is no longer less than 10.
This means that I do not enter the loop.
When I drop out of the loop, I execute the
sentence immediately after the end while
loop.
theAns = 23 + 26 + 14
theAns = 63
23 will be displayed on the screen