Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

############################################################### PLEASE ONLY ANSWER 8.2 ############################################################### ################################################################################# 135-136 ################################################################################# 8.1 (3 pts) On pages 135-136, the author discusses accumulating a product instead of a sum.

###############################################################

PLEASE ONLY ANSWER 8.2

###############################################################

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 my Prod initialized to, and why? 8.2 (8 pts) Re-write this code to iterate through the numbers in numList and multiply all the numbers in the list using a while loop. (Note that because the last statement in the given code is print (my Prod), you should store successive products in the variable called my Prod; if you call that variable something else, my Prod would not exist (see Question 1) and the print (my Prod) would result in an error.) Page 3 of 6 CS 8 (Winter 2020) numList - (3, 2, 7, -1, 9] print (my Prod) 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 Query Formulation And Administration Using Oracle And PostgreSQL

Authors: Michael Mannino

8th Edition

1948426951, 978-1948426954

More Books

Students also viewed these Databases questions