Level 13 – Lesson 03 [List Loop Algorithms]

Now that you’ve seen how to fill and show a values in a list using loops you are ready to challenge yourself by figuring out some standard loop algorithms. The following algorithms will start on the easy side and then get harder. When you get stuck, just watch the solution video at the appropriate time for a walk through. Expect to get stuck on at least one or two of them – if they were all too easy you wouldn’t have any fun trying to figure them out.

Continue with your roller project from 13-02-A, or open 13-02-A-Done.gmk for my version and try to solve the next 5 algorithms.

Solution/Hints (Don’t peek until you’ve tried)
Solution Video: 13-03-A
Done File: 13-03-A-Done.gmk

Algorithm 01 – How Many Sixes?
Solved at 0:06

In the KEYPRESS “H” KEY EVENT
a) find out how many values in the list
b) create a counter and set it to zero
c) use a loop to cycle through each value in the list
d) in the loop, if the value is a 6, increase the counter
e) when the loop is done, display how many sixes were rolled!

Algorithm 02 – Total Of All Rolls
Solved at 2:36

In the KEYPRESS “T” KEY EVENT
a) find out how many items in the list
b) create a variable called total and set it equal to zero
c) use a loop to cycle through each value in the list
d) add each value onto the total variable
e) when the loop is done, display the total of the list

Algorithm 03 – Where’s The First One?
Solved at 4:56

In the KEYPRESS “F” KEY EVENT
a) find out how many items in the list
b) use a loop to go through all the values in the list
c) if the current value is equal to 1 then show the current index position in the list and then use
‘break’ statement to exit out of the loop.

Algorithm 04 – Largest Value in List
Solved at 8:26

Before starting this algorithm, change your existing rolling code so that the values placed into the list are 1-100 and not 1-6.

In the KEYPRESS “L” KEY EVENT
a) find the size of the list, if it is zero, exit this event
b) create a variable called largest and set it equal to the value of the first item in the list
c) use a loop and go through each value in the list
d) if the current value is larger than the variable largest, set largest equal to the value
e) when the loop is done, largest should be equal to the largest value in the list, show it!

Algorithm 05 – Where is the Smallest Value in List?
Solved at 13:06

Leave your rolling code set to fill the list with values 1-100.

In the KEYPRESS “M” KEY EVENT
a) find the size of the list, if it is zero, exit this event
b) create a variable called smallest and set it equal to the value of the first item in the list
create a variable called position and set it equal to 0
c) use a loop and go through each value in the list
d) if the current value is smaller than smallest, set smallest to the value and set the position
variable to the current index position you are on
e) when the loop is done, position should equal the index position of the smallest value – show it!

Smile!  Next lesson…