Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// PAGE 135 7. (5 pts) On p. 135, we see the intermediate values for mysum, namely 3, 5, 12, 11, and 10. Try to

image text in transcribed

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

PAGE 135

image text in transcribed

image text in transcribed

7. (5 pts) On p. 135, we see the intermediate values for mysum, namely 3, 5, 12, 11, and 10. Try to understand where those values come from as the loop progresses. Now, imagine the same while loop was executed, but with the first line of code changed to numList=[8, 3, 1, 2, 7] (instead of numList=[3, 2, 7, -1, 9].) What would the successive intermediate values of mySum be in that case? List them below. 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

3.2 African

Answered: 1 week ago