Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.

Slides:



Advertisements
Similar presentations
BUILDING JAVA PROGRAMS CHAPTER 3 PARAMETERS AND OBJECTS.
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 4 Lecture 4-2: Advanced if/else ; Cumulative sum reading: 4.1, 4.3, 4.5; "Procedural.
Building Java Programs
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Copyright 2008 by Pearson Education 1 Class constants and scope reading: 2.4 self-check: 28 exercises: 11 videos: Ch. 2 #5.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops; Procedural Design reading: 5.1 – 5.2; 4.5.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Topic 14 while loops and loop patterns Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Building Java Programs Parameters and Objects. 2 Redundant recipes Recipe for baking 20 cookies: –Mix the following ingredients in a bowl: 4 cups flour.
Loop - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 1.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
1 Fencepost loops suggested reading: The fencepost problem Problem: Write a static method named printNumbers that prints each number from 1 to a.
Building java programs, chapter 5 Program logic and indefinite loops.
Cumulative algorithms. 2 Adding many numbers How would you find the sum of all integers from ? // This may require a lot of typing int sum = 1 +
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Building Java Programs Program Logic and Indefinite Loops.
CS 112 Introduction to Programming Loop Patterns: break; Fencepost Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
Copyright 2010 by Pearson Education Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
Copyright 2009 by Pearson Education Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1 self-check: #1-6 exercises: #1-3 videos: Ch.
1 Building Java Programs Chapter 4: Conditional Execution These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted,
Building Java Programs
Categories of loops definite loop: Executes a known number of times.
Building Java Programs
Lecture 7: Input and Miscellaneous
Topic 9 Using Objects, Interactive Programs and Loop Techniques
Building Java Programs
Building Java Programs
Building Java Programs Chapter 4
Introduction to Computer Programming Counting Loops 2
Fencepost loops reading: 4.1.
Topic 14 while loops and loop patterns
Redundant recipes Recipe for baking 20 cookies:
Building Java Programs
Building Java Programs
Lecture 5: For Loops Building Java Programs: A Back to Basics Approach
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Using Objects (continued)
Loop problem Write a method printLetters that prints each letter from a word separated by commas. For example, the call: printLetters("Atmosphere") should.
Building Java Programs
Building Java Programs
Building Java Programs
Suggested self-checks:
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
CSE 142, Spring 2013 Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum

Copyright 2006 by Pearson Education 2 Adding many numbers Consider this code to add three values: int num1 = 10; int num2 = 5; int num3 = 15; int sum = num1 + num2 + num3; System.out.println("The sum is " + sum);

Copyright 2006 by Pearson Education 3 A cumulative sum The variables num1, num2, and num3 are unnecessary: int sum = 10; int sum = 5+sum; int sum = 15+sum; System.out.println("The sum is " + sum); cumulative sum: A variable that keeps a sum-in- progress and is updated many times until the task of summing is finished. The variable sum in the above code is a cumulative sum.

Copyright 2006 by Pearson Education 4 Failed cumulative sum loop How could we modify the code to sum 100 numbers? Creating 100 copies of the same code would be redundant. An incorrect solution: for (int i = 1; i <= 100; i++) { int sum = 0; sum = sum + i; // or sum += i } // sum is undefined here System.out.println("The sum is " + sum); The scope of sum is inside the for loop, so the last line of code fails to compile.

Copyright 2006 by Pearson Education 5 Fixed cumulative sum loop A corrected version of the sum loop code: Scanner console = new Scanner(System.in); int sum = 0; for (int i = 1; i <= 100; i++) { sum = sum + i; // or sum += i } System.out.println("The sum is " + sum); The key idea: Cumulative sum variables must always be declared outside the loops that update them, so that they will continue to live after the loop is finished.

Copyright 2006 by Pearson Education 6 Variation: cumulative product The same idea can be used with other operators, such as multiplication which produces a cumulative product: // raise 2 to a power public static final int exponent = 4; int product = 1; for (int i = 1; i <= exponent; i++) { product = product * 2; } System.out.println("2 to the " + exponent + " = " + product); Exercises: Change the above code so that it also uses a constant for the base, instead of always using 2. Change the code above to give 3 to the 3 rd power

Copyright 2006 by Pearson Education 7 Cumulative sum question Sum the 4 numbers when counting by 5 starting at 5 Print the number and running sum Print the final number with a label Example log of execution: The number is 5 and the total is 5 The number is 10 and the total is 15 The number is 15 and the total is 30 The number is 20 and the total is 50 The final total is 50

Copyright 2006 by Pearson Education 8 Cumulative sum answer //Sum the 4 numbers when counting by 5 starting at 5 //Print the number and running sum //Print the final number with a label public class Fives { public static void main(String[] args) { int fivenumber; int sum = 0; for (count = 1; count <= 4; count++) { fivenumber = count * 5; sum = sum + fivenumber; System.out.println(“ The number is “ + fivenumber + “ and the total is “ + sum); } System.out.println("The final total is " + sum); }...

Copyright 2006 by Pearson Education 9 reading: 4.1 Fencepost loops

Copyright 2006 by Pearson Education 10 The fencepost problem Problem: Write a static method named printNumbers that prints each number from 1 to a given maximum, separated by commas. For example, the method call: printNumbers(5) should print: 1, 2, 3, 4, 5

Copyright 2006 by Pearson Education 11 Flawed solution 1 A flawed solution: public static void printNumbers(int max) { for (int i = 1; i <= max; i++) { System.out.print(i + ", "); } System.out.println(); // to end the line of output } Output from printNumbers(5) : 1, 2, 3, 4, 5,

Copyright 2006 by Pearson Education 12 Flawed solution 2 Another flawed solution: public static void printNumbers(int max) { for (int i = 1; i <= max; i++) { System.out.print(", " + i); } System.out.println(); // to end the line of output } Output from printNumbers(5) :, 1, 2, 3, 4, 5

Copyright 2006 by Pearson Education 13 Fence post analogy We print n numbers but need only n - 1 commas. This problem is similar to the task of building a fence with lengths of wire separated by posts. often called a fencepost problem If we repeatedly place a post and wire, the last post will have an extra dangling wire. A flawed algorithm: for (length of fence) { place some post. place some wire. }

Copyright 2006 by Pearson Education 14 Fencepost loop The solution is to add an extra statement outside the loop that places the inital "post." This is sometimes also called a fencepost loop or a "loop-and-a-half" solution. The revised algorithm: place a post. for (length of fence - 1) { place some wire. place some post. }

Copyright 2006 by Pearson Education 15 Fencepost method solution A version of printNumbers that works: public static void printNumbers(int max) { System.out.print(1); for (int i = 2; i <= max; i++) { System.out.print(", " + i); } System.out.println(); // to end the line of output } OUTPUT from printNumbers(5) : 1, 2, 3, 4, 5

Copyright 2006 by Pearson Education 16 Fencepost question Write a method named printFactors that, when given a number, prints its factors in the following format (using an example of 24 for the parameter value): [1, 2, 3, 4, 6, 8, 12, 24]

Copyright 2006 by Pearson Education 17 Fencepost question Write a Java program that sets a constant for a base and a maximum power and prints all of the powers of the given base up to that max, separated by commas. Base: 2 Max exponent: 9 The first 9 powers of 2 are: 2, 4, 8, 16, 32, 64, 128, 256, 512