1 / 38

Writing algorithms using the for-statement

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.

keena
Download Presentation

Writing algorithms using the for-statement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Writing algorithms using the for-statement

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

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

  4. Programming example 1: find all divisors of a number (cont.) • A concrete example: • Input: n = 12 • Output: 1, 2, 3, 4, 6, 12

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

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

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

  8. Programming example 1: find all divisors of a number (cont.) • Reminder: Notation • The notation: for (x = 1, 2, 3, ...., 10) do { print x; }

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

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

  11. 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 !) }

  12. 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)

  13. 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;

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

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

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

  17. 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)

  18. 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 clearto 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

  19. 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;

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

  21. Programming example 2: compute the sum 1+2+3+...+n (cont.) • Statement to perform the task: "add x to sum" sum = sum + x;

  22. 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 !

  23. Programming example 2: compute the sum 1+2+3+...+n (cont.) • Structure diagram of the algorithm:

  24. 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;

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

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

  27. Programming example 3: compute factorial n! • Problem description: • Write a Java program that reads in an integer n... • and prints the factorialn! = 1×2×3×...×n

  28. Programming example 3: compute factorial n! (cont.) • A concrete example: • Input: n = 5 • Output: 120   (because 1 × 2 × 3 × 4 × 5 = 120)

  29. 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 1to 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)

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

  31. 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;

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

  33. Programming example 3: compute factorial n! (cont.) • Statement to perform the task: "multiply x into product" product = product * x;

  34. 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 !

  35. Programming example 3: compute factorial n! (cont.) • Structure diagram of the algorithm:

  36. 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;

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

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

More Related