Page 246 - Learn To Program With Scratch
P. 246

try it out 9-3
                            Run this application several times to understand how it works . Change the script
                            so that each bar will be drawn in a different color . Hint: Create a new list, named
                            color, for the Painter sprite that stores the color number of the five bars and use
                            the following command before drawing each bar:







              numerical Lists

                         Lists of numbers appear in many practical applications. We can have lists of
                         test scores, temperature measurements, product prices, and more. In this
                         section, we’ll explore some common operations you might want to perform
                         on numerical lists. In particular, we’ll write procedures for finding the
                         maximum or minimum value and for finding the average of the numbers
                         stored in a list.

                         Finding Min and Max
             FindMax .sb2  Suppose you’re a teacher and you need to know the highest score from
                         the last exam your class took. You could write a program to compare all of
                         those test scores and find the maximum value. Our first example, shown in
                         Figure 9-17, finds the highest number in a list named score.
                             The FindMax procedure starts by setting the value of the maxScore vari-
                         able equal to the first number in the list. It then starts a loop to compare
                         the remaining numbers in the list with the current value of maxScore. Every
                         time it finds a value greater than maxScore, it sets maxScore equal to that
                         value. When the loop terminates, the value stored in maxScore will be the
                         largest value contained in the list.




                                                                  Assume that the first element in the list
                                                                  is the maximum, until we discover
                                                                  otherwise.
                                                                  Start a loop to compare the remaining
                                                                  numbers of the list (starting with the
                                                                  second number) with maxScore.
                                                                  Every time we find a value in the list
                                                                  that is greater than maxScore, we set
                                                                  maxScore equal to that value.



                                                                  When the loop terminates, the value
                                                                  stored in maxScore will be the largest
                                                                  value contained in the list.
                         Figure 9-17: Finding the maximum number in a list


           224   Chapter 9
                                           www.it-ebooks.info
   241   242   243   244   245   246   247   248   249   250   251