Top Banner
1 Copyright 2006 by Pearson Education reading: 4.1 Cumulative sum Cumulative sum
17

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

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.

1Copyright 2006 by Pearson Education

reading: 4.1

Cumulative sumCumulative sum

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

2Copyright 2006 by Pearson Education

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);

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

3Copyright 2006 by Pearson Education

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.

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

4Copyright 2006 by Pearson Education

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 hereSystem.out.println("The sum is " + sum);

The scope of sum is inside the for loop, so the last line of code fails to compile.

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

5Copyright 2006 by Pearson Education

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.

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

6Copyright 2006 by Pearson Education

Variation: cumulative product The same idea can be used with other operators, such

as multiplication which produces a cumulative product:

// raise 2 to a powerpublic 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 3rd power

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

7Copyright 2006 by Pearson Education

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 5The number is 10 and the total is 15The number is 15 and the total is 30The number is 20 and the total is 50The final total is 50

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

8Copyright 2006 by Pearson Education

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); } ...

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

9Copyright 2006 by Pearson Education

reading: 4.1

Fencepost loopsFencepost loops

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

10Copyright 2006 by Pearson Education

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

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

11Copyright 2006 by Pearson Education

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,

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

12Copyright 2006 by Pearson Education

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

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

13Copyright 2006 by Pearson Education

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.}

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

14Copyright 2006 by Pearson Education

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.}

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

15Copyright 2006 by Pearson Education

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

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

16Copyright 2006 by Pearson Education

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]

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

17Copyright 2006 by Pearson Education

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