Mini-Project: 

Lists & Loops

21

Getting Started

A. Download the Starter Code

B. In AppInventor, click on the Projects drop down menu 


For each of the following problems, code the solution in the Blocks Editor. Then run and test your solution. Keep running and testing until you are certain that it is correct. Each problem should have its solution displayed in the specific label for it. 

List Basics

Lists

Initialization: Before looking at the exercises, it’s important to take a look at how this app has been initialized in the Screen1.Initialize method, calling createRandomList. in order to get several random numbers. The global theList variable has been initialized to contain a list of between 3 and 10 random integers, each of which is between 0 and 10

Create a List Algorithm

Look at the algorithm in the createRandomList function above. It begins by initializing theList to the empty list. Then it uses the for-each block to repeatedly add a random integer to theList. The for-each loop is a counting loop: it will repeat according to the values of its arguments: from start, to end, and by step. Here it starts at 1, ends at some random number, and counts by steps of size 1. So if to=6 it will repeat 6 times. Each time through the loop the for-each variable, index, takes on a new value, from 1 to 6. So index will be 1 then 2 then 3 and so forth until it gets to 6, after which the loop stops. 

In pseudocode, we might represent this algorithm as follows:

Set theList to [ ] 

   For each index (from=1, to=randomInt(3,10), by=1) do:

       Add a randomInt(0,10) to theList


The number of iterations depends on the value of the start, end, and step slots. If we say:

for-each index (from=1, to=10, by=2) 

then index would take the values 1, 3, 5, 7, 9, and the loop would repeat 5 times. 

The createRandomList function creates a random list of numbers that is printed in the output label when the app starts. The following exercises perform various operations on that list. For each given exercise, add blocks to the given code at the spot indicated in the following picture, do not modify the collapsed blocks and do not move the current blocks around:

Goals & Requirements

0. Rename the Screen1.Title to your name. 

1. Print the number of items. In the firstStats procedure, add the number of items in theList to the setLabelFullList.Text to join block. There is one slot at the bottom to add the number of items. Look for the correct block in the Lists block section.

2. First & Last items. In the firstAndLast procedure, make it so that the correct values are set to the corresponding labels for the first and last elements. (select list item)

3. Print the list. Print each element of the list, one element per line.

4. Sum a list. Modify the sumTheList procedure to correctly calculate the sum of the elements of the list. For example, the sum of the list [1 4 7 1 7 1 8] is 29. 

Running Total Algorithm: To sum a list we use what is called a running total algorithm which uses a sum variable. Initially the sum is set to 0 (additive identity). Then, using a loop, for each item in the list, add it to the sum. In pseudocode, this looks like this:

        Set sum to 0

        For each number in list: theList

            Add number to sum

When this algorithm finished, the sum variable will store the sum of the list.

Define a procedure called sumTheList that implements this running total algorithm. The procedure should use the for-each block from the Controls drawer. 

Then call it and display the sum of the list:

5. Average a list. Modify the average procedure to correctly calculate the average of the elements of the list. For example, the average of the list [1 4 7 1 7 1 8] is 4.143. You do not need a running-total algorithm, instead call the sumTheList procedure and calculate from there. 

6. Item at Index. Write an app that lets the user input an index value and then reports the value in the list at that index: 

7. Index out of bounds Error. In the previous exercise, the app could crash if the user input an index that was out-of-bounds for the list. For example, if the user inputs 11 for the above list of 10 items, the app will crash:

8. Create a new sublist. Create a newList that consists of all the elements of the first list up to the index. Let the user input an index between 1 and the length of the list and copies each element from theList into the newList in the same order up until the index given by the user. If the index is out of bounds it should just be an empty list. 

9. Extra Credit: Style the app nicely. Make the app have a better style. One hint could be to have each test section a different color. 

Be creative!

Show TClark your completed app, then turn in all the files in the form below.