Answered step by step
Verified Expert Solution
Question
1 Approved Answer
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 135-136 8.1 (3 pts) On pages 135-136, the author discusses accumulating a product instead of a sum. The accumulator variable is called myProd this
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
135-136
8.1 (3 pts) On pages 135-136, the author discusses accumulating a product instead of a sum. The accumulator variable is called myProd this time. In the version of the code that works properly, what is myProd initialized to, and why? numList mySum = 0 Figure 5.3 Accumulator pattern. The for loop iterates over the numbers in list numList. In every iteration, the current number is added to the accumulator mySum using the assignment mySum = mySum + num. num = mySum = mySum + num = 3 num= mySum = mySum + num = 5 num = mySum = mySum + num = 12 num= mySum = mySum + num = 11 num= mySum = mySum + num = 20 The execution of the previous for loop example is illustrated in Figure 5.3. The variable mySum serves as the accumulator. In this case, it is an integer accumulator initialized to 0 because we are summing integers and 0 is the identity for addition (i.e., 0 doesn't affect addition). Every value of num is added to the accumulator with the assignment mySum = mySum + num In the expression to the right of the assignment operator =, the value of num and the current value of the accumulator mySum are added together. The assignment then puts the result of this addition back into the accumulator my Sum. We say that mySum is incremented by the value of num. This operation is so common that there is a shortcut for it: mySum += num Let's recompute the sum using this shortcut: >>> mySum = 0 >>> for num in numList: mySum += num We refer to the pattern of this for loop as the accumulator loop pattern. Accumulating Different Types We illustrate the accumulator pattern with several more examples. Recall that in Chapter 2 we introduced the built-in function sum) that can be used to add up the values in a list: >>> sum(numList) 20 So, writing a for loop to sum up the numbers in a list was not really necessary. Usually, however, a built-in function is not available. What if, for example, we wanted to multiply all the numbers in the list? An approach similar to the one we used for the sum might work: >>> myProd = 0 # initializing the product >>> for num in numList: # num gets values from numList myProd = myProd * num # myProd is multiplied by num >>> myProd # what went wrong? What went wrong? We initialized the accumulator product myProd to 0; the problem is that O times anything is 0. When we multiply myProd by every value in numList, we will always get 0 back. The value 0 was a good choice for initializing a sum because 0 is the identity for the addition operator. The identity value for the product operator is 1: >>> myProd = 1 >>> for num in numList: myProd = myProd * num >>> myProd -378Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started