Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribed

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

135-136

image text in transcribedimage text in transcribedimage text in transcribed

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 -378

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Database Design And Implementation

Authors: Edward Sciore

2nd Edition

3030338355, 978-3030338350

More Books

Students also viewed these Databases questions

Question

Differentiate between a judgment sample and a convenience sample.

Answered: 1 week ago

Question

1. Jacob is a natural leader.

Answered: 1 week ago

Question

what is the pupose of a computer audit

Answered: 1 week ago

Question

3. What are the current trends in computer hardware platforms?

Answered: 1 week ago