Page 247 - Learn To Program With Scratch
P. 247

Finding the minimum value in a list follows a similar algorithm. We
                          start by assuming that the first element in the list is the smallest element
                          and then use a loop to check the remaining elements. Each time we find
                          a smaller value, we update the variable that holds the minimum value.



                                                   try it out 9-4
                            Use what you learned in this section to create a procedure called FindMin that
                            finds the minimum value of the score list .



                          Finding the Average

           FindAverage .sb2  In our next example, we’ll write a procedure that computes the average
                          score of the numbers stored in our score list. You can find the average of
                          a sequence of N numbers by first finding their sum and then dividing the
                          total by N. The procedure shown in Figure 9-18 does exactly that.




                                                                 This variable is used to hold the
                                                                 total sum.
                                                   Note: You can also use the change sum by block.

                                                                 Loop through the scores stored in
                                                                 the list and calculate their sum.

                                                                 Divide the sum by the number of
                                                                 scores in the list.



                          Figure 9-18: Finding the average value of a list of numbers

                             The FindAverage procedure uses a loop to step through the scores
                          stored in the list, add them together, and store the result in a variable
                          named sum. (This variable is initialized to 0 before the start of the loop.)
                          When the loop terminates, the script calculates the average by dividing
                          sum by the number of scores, and it saves the result in a variable named
                          average.
                 n o t e    Pay special attention to the way we accumulated the sum variable inside the
                          loop. This pattern, known as the accumulator pattern, comes up very often
                          in programming.

                             In the next section, we’ll explore how to search and sort lists, two com-
                          mon problems in programming. I’ll also walk you through some simple
                          algorithms for performing each operation.


                                                                                      Lists   225

                                           www.it-ebooks.info
   242   243   244   245   246   247   248   249   250   251   252