writing algorithms using the for-statement

38
Writing algorithms using the for- statement

Upload: keena

Post on 21-Jan-2016

48 views

Category:

Documents


0 download

DESCRIPTION

Writing algorithms using the for-statement. Programming example 1: find all divisors of a number. We have seen a program using a while-statement to solve this problem. Here, we will see that this problem can be solved more naturally using a for -statement. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Writing  algorithms  using the for-statement

Writing algorithms using the for-statement

Page 2: Writing  algorithms  using the for-statement

Programming example 1: find all divisors of a number

• We have seen a program using a while-statement to solve this problem.

Here, we will see that this problem can be solved more naturally using a for-statement.

Page 3: Writing  algorithms  using the for-statement

Programming example 1: find all divisors of a number (cont.)

• Problem description:

• Write a Java program that reads in an integer n...

• and prints all its divisors

Page 4: Writing  algorithms  using the for-statement

Programming example 1: find all divisors of a number (cont.)

• A concrete example:

• Input: n = 12

• Output: 1, 2, 3, 4, 6, 12

Page 5: Writing  algorithms  using the for-statement

Programming example 1: find all divisors of a number (cont.)

• What would you do to solve this problem ?

• Problem:

• Find all divisor of the number 12

Page 6: Writing  algorithms  using the for-statement

Programming example 1: find all divisors of a number (cont.)

• I think you would have done this:

• Check if 12 is divisible by 1

• Check if 12 is divisible by 2

• ...

• Check if 12 is divisible by 12

Page 7: Writing  algorithms  using the for-statement

Programming example 1: find all divisors of a number (cont.)

• Note:

• When the remainder of the division is equal to 0, we print out the divisor

• We do not need to check numbers > 12 because only number ≤ 12 can be divisors !

Page 8: Writing  algorithms  using the for-statement

Programming example 1: find all divisors of a number (cont.)

• Reminder: Notation

• The notation:

for (x = 1, 2, 3, ...., 10) do { print x; }

Page 9: Writing  algorithms  using the for-statement

Programming example 1: find all divisors of a number (cont.)

means:

• The variable x takes on (assumes, gets) the value 1, 2, 3, ..., 10, one at a time • For each individual value taken on by x, you perform the operation "print x" once

Page 10: Writing  algorithms  using the for-statement

Programming example 1: find all divisors of a number (cont.)

• This notation can be converted to a for-statement very naturally:

for ( x = 1; x <= 10; x = x + 1 ) { print x; }

Page 11: Writing  algorithms  using the for-statement

Programming example 1: find all divisors of a number (cont.)

• Rough algorithm (pseudo code) to find all divisors:

input: n = some integer number

for (x = 1, 2, 3, ..., n) do { if ( n is divisible by x ) then print x; (because x is a divisor !) }

Page 12: Writing  algorithms  using the for-statement

Programming example 1: find all divisors of a number (cont.)

• Structure diagram of the algorithm:

Notice it is much easier to write the algorithm using a for-statement (than a while-statement - in this example)

Page 13: Writing  algorithms  using the for-statement

Programming example 1: find all divisors of a number (cont.)

• Java program:

import java.util.Scanner; public class Divisors01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; int x;

Page 14: Writing  algorithms  using the for-statement

Programming example 1: find all divisors of a number (cont.)

System.out.print("Enter a number n: "); n = in.nextInt(); // Read in number for ( x = 1; x <= n; x++ ) // Run x = 1, 2, ..., n { if ( n % x == 0 ) { // x is a divisor of n System.out.println(x); // Print x (because it's a divisor) } } } }

Page 15: Writing  algorithms  using the for-statement

Programming example 1: find all divisors of a number (cont.)

• Example Program: (Demo above code)– Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/Divisors03.java

• How to run the program:            

• Right click on link and save in a scratch directory

• To compile:   javac Divisors03.java

• To run:          java Divisors03

Page 16: Writing  algorithms  using the for-statement

Programming example 2: compute the sum 1+2+3+...+n

• Problem description:

• Write a Java program that reads in an integer n...

• and prints the sum 1+2+3+...+n

Page 17: Writing  algorithms  using the for-statement

Programming example 2: compute the sum 1+2+3+...+n (cont.)

• A concrete example:

• Input: n = 5

• Output: 15   (because 1 + 2 + 3 + 4 + 5 = 15)

Page 18: Writing  algorithms  using the for-statement

Programming example 2: compute the sum 1+2+3+...+n (cont.)

• What would you do to solve this problem ?

• Imagine again that you are using a calculator....

• I think you would have done this:

• Punch clear to set the total to 0.

• Press 1, then press add to add to the running total

• Press 2, then press add to add to the running total

• ...

• Press 5, then press add to add to the running total

• Show the total

Page 19: Writing  algorithms  using the for-statement

Programming example 2: compute the sum 1+2+3+...+n (cont.)

• Rough algorithm (pseudo code) to compute 1 + 2 + 3 + ... + n:

input: n = some integer number

sum = 0; // Clear the running total !

for ( x = 1, 2, 3, ..., n ) do { Add x to sum }

Print sum;

Page 20: Writing  algorithms  using the for-statement

Programming example 2: compute the sum 1+2+3+...+n (cont.)

• Sample execution (n = 5):

sum = 0; ---> sum = 0

for-statement: add 1 to sum; ---> sum = 1 add 2 to sum; ---> sum = 3 add 3 to sum; ---> sum = 6 add 4 to sum; ---> sum = 10 add 5 to sum; ---> sum = 15

Print sum; ---> Prints 15

Page 21: Writing  algorithms  using the for-statement

Programming example 2: compute the sum 1+2+3+...+n (cont.)

• Statement to perform the task: "add x to sum"

sum = sum + x;

Page 22: Writing  algorithms  using the for-statement

Programming example 2: compute the sum 1+2+3+...+n (cont.)

• Example:

Suppose the variable sum contains 6 and x = 4

Then:

sum = sum + x; = 6 + 4; = 10; ---> Result, sum = 10 We have added 4 to sum !

Page 23: Writing  algorithms  using the for-statement

Programming example 2: compute the sum 1+2+3+...+n (cont.)

• Structure diagram of the algorithm:

Page 24: Writing  algorithms  using the for-statement

Programming example 2: compute the sum 1+2+3+...+n (cont.)

• Java program:

import java.util.Scanner; public class Divisors01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; int x, sum;

Page 25: Writing  algorithms  using the for-statement

Programming example 2: compute the sum 1+2+3+...+n (cont.)

System.out.print("Enter a number n: "); n = in.nextInt(); // Read in number sum = 0; // Clear running total

for ( x = 1; x <= n; x++ ) // Run x = 1, 2, ..., n { sum = sum + x; // Add x to sum }

System.out.println(sum); // Print final sum } }

Page 26: Writing  algorithms  using the for-statement

Programming example 2: compute the sum 1+2+3+...+n (cont.)

• Example Program: (Demo above code)– Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/RunningSum01.java

• How to run the program:            

• Right click on link and save in a scratch directory

• To compile:   javac RunningSum01.java

• To run:          java RunningSum01

Page 27: Writing  algorithms  using the for-statement

Programming example 3: compute factorial n!

• Problem description:

• Write a Java program that reads in an integer n...

• and prints the factorial n! = 1×2×3×...×n

Page 28: Writing  algorithms  using the for-statement

Programming example 3: compute factorial n! (cont.)

• A concrete example:

• Input: n = 5

• Output: 120   (because 1 × 2 × 3 × 4 × 5 = 120)

Page 29: Writing  algorithms  using the for-statement

Programming example 3: compute factorial n! (cont.)

• We can re-use the previous programming paradigm to construct the algorithm:

• Imagine you are using a calculator....

• This is how one may compute the factorial:

Punch 1 to set the running product to 1.

(You can't use 0 as starting value for multiplication , because the multiplying with 0 will always produce 0)

Page 30: Writing  algorithms  using the for-statement

Programming example 3: compute factorial n! (cont.)

• Press 1, then press multiply to multiply 1 into the running product

• Press 2, then press multiply to multiply 2 into the running product

• ...

• Press 5, then press multiply to multiply 5 into the running product

• Show the product

Page 31: Writing  algorithms  using the for-statement

Programming example 3: compute factorial n! (cont.)

• Rough algorithm (pseudo code) to compute n! = 1 × 2 × 3 × ... × n:

input: n = some integer number

product = 1; // Set running product to 1

for ( x = 1, 2, 3, ..., n ) do { Multiply x into product }

Print sum;

Page 32: Writing  algorithms  using the for-statement

Programming example 3: compute factorial n! (cont.)

• Sample execution (n = 5):

product = 1; ---> product = 1

for-statement: multiply 1 into product; ---> product = 1 multiply 2 into product; ---> product = 2 multiply 3 into product; ---> product = 6 multiply 4 into product; ---> product = 24 multiply 5 into product; ---> product = 120

Print product; ---> Prints 120

Page 33: Writing  algorithms  using the for-statement

Programming example 3: compute factorial n! (cont.)

• Statement to perform the task: "multiply x into product"

product = product * x;

Page 34: Writing  algorithms  using the for-statement

Programming example 3: compute factorial n! (cont.)

• Example:

Suppose the variable product contains 6 and x = 4

Then:

product = product * x; = 6 * 4; = 24; ---> Result, product = 24 We have multiplied 4 into the product !

Page 35: Writing  algorithms  using the for-statement

Programming example 3: compute factorial n! (cont.)

• Structure diagram of the algorithm:

Page 36: Writing  algorithms  using the for-statement

Programming example 3: compute factorial n! (cont.)

• Java program:

import java.util.Scanner; public class Factorial01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; int x, product;

Page 37: Writing  algorithms  using the for-statement

Programming example 3: compute factorial n! (cont.)

System.out.print("Enter a number n: "); n = in.nextInt(); // Read in number product = 1; // Set init. product to 1

for ( x = 1; x <= n; x++ ) // Run x = 1, 2, ..., n { product = product + x; // Multiply x into product }

System.out.println(product); // Print final product } }

Page 38: Writing  algorithms  using the for-statement

Programming example 3: compute factorial n! (cont.)

• Example Program: (Demo above code)– Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/Factorial01.java

• How to run the program:            

• Right click on link and save in a scratch directory

• To compile:   javac Factorial01.java

• To run:          java Factorial01