From csp4hs - summer 2014
Lists
- Watch the video below entitled "Lists." The link to the slides is just above the video.
- As noted in the last lesson, variables are used to store the values of computations that are made in a program. Our past look at variables considered only a single variable that contained a single value. There are many occasions when a programmer may want to group a set of values into a single variable name (e.g., a list of class grades, a sequence of notes for a song, the list of high scores for a game), rather than storing each value separately (e.g., grade1, grade2, ..., grade20). If we had to store each value in a separate variable, our programs would become complex (imagine over 1000 different variables to store the notes of a song). A data structure allows us to group information together into a single variable that can be created and manipulated. Lists are a specific type of data structure in Snap! that allow a programmer to store a sequence of values that can be individually indexed through a common variable name. In Snap!, each element in a list is indexed, starting at index location 1. For example, on slide 2 of the "Lists" presentation below, index location 1 is an "Apple" and index location 5 is "Grape." The index locations of -1 and 6 are undefined in the example on slide 2.
- Go to Snap! and look at the Variables palette, making note of the various blocks that can process lists (see slide 3 of the presentation below). These blocks can be used to create, delete, add, insert at specific location, determine the length of a lists, and access values from a list. Using a loop, it is possible to iterate over all of the elements of a list and perform some special computation.
- Watch the video "Printing the contents of a list" located below. Notice that there are three key parts to this program, which you will often find in most code that processes a list: 1) Creation and initialization of a loop counter (often called "index" or "counter"), 2) a repeat loop that iterates over the length of a list, 3) some access to the list through blocks like "item of", and 4) within the loop, an update to the loop counter to move to the next element in the list (often done with the "change" block in Snap!). Study the program that says "C S P 4 H S" and make sure that you understand the list blocks that are used and how they work in conjunction with the repeat loop.
- Watch the video "Summing a list of numbers" located below. This program creates a new Reporter block (called sum) that receives a list of integers as input and returns the sum of all the numbers in the list. The benefit of using a list is that sum can be called with an arbitrary number of integers - it can compute the sum of 10 numbers as easily as it can computer the sum of 100 numbers. There is nothing fixed in the block definition that ties it to a specific size of the list (the use of the length block enables this benefit). In the code of the block, in addition to the loop index counter, there is also a variable called tempSum that is used to keep track of the running sum of the list numbers. Make sure you understand how this block works by tracing out a sample list input and how the values of index and tempSum change during the iteration over the list. After the sum of the list is computed in tempSum, notice that the value is reported back to the caller. A sample program that uses the sum block is also shown in this video, which simply sums the values of a small set of integers and says the value to the screen.
No comments:
Post a Comment