The Accumulator Pattern Summing: Add up (“accumulate”), e.g. 1 2 plus 2 2 plus 3 2 plus … plus 1000 2 Variation: Form a product (instead of sum), e.g.

Slides:



Advertisements
Similar presentations
Counting and Factorial. Factorial Written n!, the product of all positive integers less than and equal to n. Ex: Evaluate.
Advertisements

String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
Mathematics Vocabulary – Grade K add ©Partners for Learning, Inc. To bring two or more numbers or things together to make a new total.
1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.
Lists in Python.
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Functions Copyright © J. Mercer, A function is a number-machine that transforms numbers from one set called the domain into a set of new numbers.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
The Real Number System.  Natural Numbers (AKA Counting Numbers): {1, 2, 3, 4, …}  Whole Numbers (Natural Numbers plus zero): {0, 1, 2, 3, …} NOTE: Both.
Operations with Integers
Programming Paradigms Backus Naur Form and Syntax Diagrams.
Median Median is the middle number in a data set when the data are arranged in numerical order. If you have an even number of data items, add the two middle.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Objectives The student will be able to:  use Sigma Notation  find the mean absolute deviation of a data set SOL: A
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
Computing Science 1P Large Group Tutorial 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Nonvisual Arrays by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Counting Loops.
Complete the Following Table. From the Previous Table… What patterns do you notice in the table? How can you tell how many zeros are in a number based.
Adding Integers on a Number Line
It says "ADD" but sometimes I really subtract.
Thinking Mathematically Statistics: 12.3 Measures of Dispersion.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Squaring of a number ending in 5 An approach to determine answers Quickly! Squaring of a number ending in 5.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 9 For Loops.
PYTHON FOR HIGH PERFORMANCE COMPUTING. OUTLINE  Compiling for performance  Native ways for performance  Generator  Examples.
CS 115 Lecture 17 2-D Lists Taken from notes by Dr. Neil Moore.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 10: Files.
The Accumulator Pattern Motivating Example: Suppose that you want to add up (“accumulate”) 1 2 plus 2 2 plus 3 2 plus … plus You could use: total.
Adapted from slides by Marty Stepp and Stuart Reges
String and Lists Dr. José M. Reyes Álamo.
Catapult Python Programming Session 4
Section 2-4: Adding Integers using Rules
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Good practices that help minimize the need for debugging
CS 115 Lecture 8 Structured Programming; for loops
List comprehensions (and other shortcuts) UW CSE 160 Winter 2017.
2-D Lists Taken from notes by Dr. Neil Moore
While Loops in Python.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
List comprehensions (and other shortcuts) UW CSE 160 Spring 2018.
CSc 110, Spring 2017 Lecture 4: Nested Loops and Loop Figures
Computer Science 2 Getting an unknown # of …. Into an array.
String and Lists Dr. José M. Reyes Álamo.
Lists in Python Creating lists.
Suppose I want to add all the even integers from 1 to 100 (inclusive)
CS150 Introduction to Computer Science 1
Building Java Programs
Mean Absolute Deviation
More Repetition While and For loop variations
The for loop suggested reading:
INC 161 , CPE 100 Computer Programming
Building Java Programs
For loops Taken from notes by Dr. Neil Moore
class 5 retrieving a previous statement in Thonny or IDLE // %
Mean Absolute Deviation
Introduction to Computer Science
X values y values. x values y values Domain Range.
Mean Absolute Deviation
2-D Lists Taken from notes by Dr. Neil Moore
Class code for pythonroom.com cchsp2cs
Introduction to Computer Science
Presentation transcript:

The Accumulator Pattern Summing: Add up (“accumulate”), e.g. 1 2 plus 2 2 plus 3 2 plus … plus Variation: Form a product (instead of sum), e.g. 1 × 2 × 3 ×... × 1000 Counting: Count, e.g. how many integers from 1 to 1000 have a positive cosine Graphical accumulation, e.g. pictures like these:

The Accumulator Pattern for summing, acted out Using a loop to compute 1 2 plus 2 2 plus 3 2 plus … plus total starts at 0 total becomes 1 total becomes 5 total becomes 14 total becomes 30 total becomes 55 total becomes total starts at zero Loop 1000 times: total becomes what it was + next item to add to total We add in 1 2 (which is 1), so... We add in 2 2 (which is 4), so... We add in 3 2 (which is 9), so... We add in 4 2 (which is 16), so... We add in 5 2 (which is 25), so... We add in 6 2 (which is 36), so... and so forth

The Accumulator Pattern for summing, in Python This summing version of the Accumulator Pattern, applied to this problem of summing squares, is written in Python like this: total = 0 for k in range(1000): total = total + (k + 1) ** 2 total starts at zero Loop 1000 times: total becomes what it was + next item to add to total Inside the loop, put: total = total +... Lousy mathematics, but great computer science! Read = as “becomes”. Use a variable, which we chose to call total, and initialize that variable to 0 before the loop Use a range expression in a for loop After the loop ends, the variable total has as its value the accumulated sum!

The Accumulator Pattern – for Counting Motivating Example: Suppose that you want to count how many of the integers from 1 to 1000 have a positive cosine cosine(1) is about 0.54, so we have one integer that has a positive cosine so far cosine(2) is about -0.42, so Nope, its cosine is not positive cosine(3) is about -0.99, so Nope, its cosine is not positive cosine(4) is about -0.65, so Nope, its cosine is not positive cosine(5) is about 0.28, so we have another integer that has a positive cosine, that makes 2 cosine(6) is about 0.96, so we have another integer that has a positive cosine, that makes 3 cosine(7) is about 0.75, so we have another integer that has a positive cosine, that makes 4 cosine(8) is about -0.15, so Nope, its cosine is not positive cosine(9) is about -0.91, so Nope, its cosine is not positive cosine(10) is about -0.84, so Nope, its cosine is not positive cosine(11) is about 0.004, so we have another integer that has a positive cosine, that makes 5 etc

The Accumulator Pattern – for Counting Motivating Example: Suppose that you want to count how many of the integers from 1 to 1000 have a positive cosine How would you modify this summing code to accomplish the above? Answer: total = 0 for k in range(1000): total = total + (k + 1) ** 2 total = 0 for k in range(1000): total = total + (k + 1) ** 2 count = 0 if math.cos(k + 1) > 0: count = + 1

The Accumulator Pattern for summing/counting, in Python The summing version of the Accumulator Pattern, applied to this problem of summing squares, is written in Python like this: The counting version of the Accumulator Pattern, applied to this problem of counting how many integers have positive cosines, is written in Python like this: total = 0 for k in range(1000): total = total + (k + 1) ** 2 Inside the loop, put: total = total +... count = count + 1 Lousy mathematics, but great computer science! Read = as “becomes”. Use a variable, which we chose to call total/count, and initialize that variable to 0 before the loop Use a range expression in a for loop After the loop ends, the variable total has as its value the accumulated value! count = 0 for k in range(1000): if math.cos(k + 1) > 0: count = count + 1

The Accumulator Pattern – for Graphical Accumulation window = zg.GraphWin('Circles', 300, 200) x = 250 y = 30 for k in range(7): center = zg.Point(x, y) circle = zg.Circle(center, 20) circle.setFill('green') circle.draw(window) x = x – 30 y = y + 20

The Accumulator Pattern total = 0 for k in range(1000): total = total + (k + 1) ** 2 Inside the loop, put: variable = variable +... Use a variable and initialize that variable to something before the loop Use a range expression in a for loop After the loop ends, the variable has as its value the accumulated value! count = 0 for k in range(1000): if math.cos(k + 1) > 0: count = count + 1 window = zg.GraphWin('Circles', 300, 200) x = 250 y = 30 for k in range(7): center = zg.Point(x, y) circle = zg.Circle(center, 20) circle.setFill('green') circle.draw(window) x = x – 30 y = y + 20