Page 232 - Learn To Program With Scratch
P. 232

Let’s trace the operation of FindGCD when num1 = –10 and num2 = 6.
                         We need to find the largest positive integer that divides num1 and num2 with-
                         out a remainder. The procedure starts by setting gcd, the result, to the lesser
                         absolute value of the two numbers, −6 in our example. A loop then tests the
                         numbers 6, 5, 4, and so on, until both num1 and num2 divide evenly by the
                         checked number. This is the result we are after. In this  example, gcd will be
                         set to 2 since both numbers (–10 and 6) divide by 2 without a remainder.
                             The last procedure to examine is the GiveFeedback procedure, which
                         compares the user’s answer with the correct answer and displays an appropri-
                         ate message, as shown in Figure 8-37. The figure also shows some examples
                         that demonstrate the different cases of the if/else structure.


                                                                                 Examples
                                                                             Correct  User
                                                                              Answer   Answer
                                                                             ansNum   num3
                                                                            ansDen   den3
                                                                              3       3
                                                                               4       4
                                                                             3       6
                                                                               4       8
                                                                             3       2
                                                                               4       3

                         Figure 8-37: The GiveFeedback procedure of the Teacher sprite



                                                   try it out 8-8
                            Modify the fraction tutor program to keep track of the number of correct and incor-
                            rect answers . Devise a scheme for calculating a score and showing it to the user .



              Summary

                         String processing is an important programming skill. In this chapter, you
                         learned how to access individual characters of a string to combine them,
                         compare them, remove them, and shuffle them around.
                             We started with a detailed look at the string data type and how strings
                         are stored as sequences of characters. We then wrote several procedures that
                         demonstrated basic string manipulation techniques. After that, we used these
                         techniques to write several interesting and practical applications. The con-
                         cepts developed in these projects can be applied in many other areas, and
                         I certainly hope they’ll help you think of your own projects.
                             In the next chapter, you’ll learn about lists and how to use them to store
                         and manipulate a bunch of values. Equipped with this new data structure,
                         you’ll have all the tools you need to write professional programs in Scratch.


           210   Chapter 8
                                           www.it-ebooks.info
   227   228   229   230   231   232   233   234   235   236   237