1 / 108

Code Compression

all others welcome!. Today in CS 5. Lab: N-Z. Code Compression. the benefits of looping. concerns?. Grading and the submission website. questions?. Let’s Make a Deal. due Sunday, 9/26 at midnight. M/T sections. HW 4 - (3 problems). due Monday, 9/27 at midnight. W/Th sections.

cardm
Télécharger la présentation

Code Compression

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. all others welcome! Today in CS 5 Lab: N-Z • Code Compression the benefits of looping... concerns? • Grading and the submission website... questions? • Let’s Make a Deal due Sunday, 9/26 at midnight M/T sections • HW 4 - (3 problems) due Monday, 9/27 at midnight W/Th sections Coding style & commenting will count for HW4 ! Reading: Class notes for week 4 • Friday, 8:00 am – recitation section optional, but all are welcome!

  2. Today in CS 5 • Code Compression the benefits of looping... int i = 0; while (i < 4) { ++i; } for (int i=0 ; i<4 ; ++i) { ; } for loop while loop more braces!

  3. use reasonable variable names Style introductory comment /* * Author: me! * Date: 9/26/04 * Time: d+(int)d/x minutes * Comment: The virtual lyricist */ class CS5App { public static void main(String[] args) { H.pl(“Enter your favorite mascot: ”); String mascot = H.nw(); … more code here … } } leave space before blocks of code align curly braces that correspond leave space between parts of code that do different things indent code evenly within the surrounding braces

  4. Comments ? /* * Author: me! * Date: 9/26/04 * Time: 3 kiloseconds * Comment: Finding the max and min of 4 values */ if ( a < b && a < c && a < d ) // see if a is minimal { H.pl(“ Minimum is ” + a); } else if ( b < a && b < c && b < d ) // seeif b is minimal { H.pl(“ Minimum is ” + b); } introductory comment comment at least each block of code very small blocks need only one comment for the entire group

  5. Back inside the machine... Shortcuts for changing variables: int i = 35; i = i + 1; i += 1; ++i; i++;

  6. Back inside the machine... Shortcuts for changing variables: int i = 35; i = i + 1; i += 1; ++i; i++; all of these increment i by 1

  7. Back inside the machine... Shortcuts for changing variables: int i = 35; i = i + 1; i += 1; ++i; i++; all of these increment i by 1 int amoebas = 100000; amoebas = amoebas * 2; double hwToGo = 11.0; hwToGo = hwToGo - 1; long u235 = 10000000000000L; u235 = u235 / 2;

  8. Back inside the machine... Shortcuts for changing variables: int i = 35; i = i + 1; i += 1; ++i; i++; all of these increment i by 1 int amoebas = 100000; amoebas = amoebas * 2; double hwToGo = 11.0; hwToGo = hwToGo - 1; long u235 = 10000000000000L; u235 = u235 / 2; amoebas *= 2; hwToGo -= 1; --hwToGo; u235 /= 2;

  9. An aside ++i and i++ ? What’s the difference between Nothing -- when they are on their own!

  10. An aside ++i and i++ ? What’s the difference between Nothing -- when they are on their own! When they are being used and set at the same time things change... int i = 32; int x = i++ + 10; int i = 32; int x = ++i + 10; increments before use increments after use

  11. An aside ++i and i++ ? What’s the difference between Nothing -- when they are on their own! When they are being used and set at the same time things change... int i = 32; int x = i++ + 10; int i = 32; int x = ++i + 10; x is 43 x is 42 i is 33 i is 33 increments before use increments after use

  12. Hw 4, Problem 1 Hw4Pr1) The improved math menu ... Pair Programming Problem Option # 4: unlimited max and min Option # 3: function graphing x = p/2 Option # 2: this integral dx Option # 1: raw power x = 0 the period of a pendulum with length L and initial angle a Option # 0: sequences Hw4Pr2) Let’s Make a Deal ! Hw4Pr3) Let’s Make A LOT of Deals.

  13. code != work Sequencing items with a single piece of code 42 42 42 42 42 7 14 21 28 35 3 6 9 18 21 2 22 222

  14. code != work Sequencing items with a single piece of code 42 42 42 42 42 42 7 14 21 28 35 42 3 6 9 18 21 42 2 22 222 2222

  15. Anatomy of a for loop for ( int i = 0 ; i < 4 ; ++i ) { H.pl(i); }

  16. Anatomy of a for loop PART 1 PART 2 for ( int i = 0 ; i < 4 ; ++i ) { H.pl(i); } PART 4 PART 3

  17. Anatomy of a for loop 0 PART 1 PART 2 int i for ( int i = 0 ; i < 4 ; ++i ) { H.pl(i); } PART 4 PART 3 create and initialize loop variable PART 1 test to determine if we run the loop PART 2 i<4 true false loop body is run if test is true always PART 3 end loop update the loop variable PART 4

  18. code != work Sequencing items with a single piece of code 42 42 42 42 42 42 7 14 21 28 35 42 3 6 9 18 21 42 2 22 222 2222

  19. an egocentric for loop for ( int i = 1 ; i <= 6 ; ++i ) { H.p( 7*i ); }

  20. a selfless for loop int x = 42; for ( int i = 0 ; i < 6 ; ++i ) { H.p( x + “ ” ); }

  21. sharing with if int x = 3; for ( int i = 0 ; i < 6 ; ++i ) { H.p( x + “ ” ); if (x%2 == 0) else }

  22. “Quiz” Print the output of these loops for (int i = 0 ; i != 4 ; i+=1) { H.p( i + “ ” ); } A “Extra Credit” change one character in the code above so that this loop runs 4294967292 times. int c = 0; for (int i=10 ; i>0 ; i/=2 ) { H.p( i + “ ” ); c++; } H.pl(“c is ” + c); B int s = 0; for (int i = 1 ; i < 5 ; ++i) { H.p( i + “ ” ); s = s + i; } H.pl(“\ns is ” + s); C

  23. Write a loop to print this sequence: Write a loop to print this sequence: 9 terms total 9 terms total 0 1 2 3 6 7 14 15 30 2 22 222 2222 ... “Quiz”, part 2 Hint: Use a for loop with an if inside it! Hint: Use a for loop with a for loop inside it!

  24. Variables: a life story... int sum = 0; for ( int i = 1 ; i < 5 ; ++i ) { sum = sum + i; } H.pl(“sum is ” + sum); int sum = 0; for ( int i = 0 … Why will java complain about this ?!?

  25. Variables: a life story... (1) Birth (of sum and i) int sum = 0; for ( int i = 1 ; i < 5 ; ++i ) { sum = sum + i; } H.pl(“sum is ” + sum); int sum = 0; for ( int i = 0 … Why will java complain about this ?!?

  26. Variables: a life story... (1) Birth (of sum and i) int sum = 0; for ( int i = 1 ; i < 5 ; ++i ) { sum = sum + i; } H.pl(“sum is ” + sum); int sum = 0; for ( int i = 0 … (2) Growth Why will java complain about this ?!?

  27. Variables: a life story... (1) Birth (of sum and i) int sum = 0; for ( int i = 1 ; i < 5 ; ++i ) { sum = sum + i; } H.pl(“sum is ” + sum); int sum = 0; for ( int i = 0 … (2) Growth (3) Death (of i) but sum lives on… Why will java complain about this ?!?

  28. Variables: a life story... (1) Birth (of sum and i) int sum = 0; for ( int i = 1 ; i < 5 ; ++i ) { sum = sum + i; } H.pl(“sum is ” + sum); sum = 0; for ( int i = 0 … (2) Growth (3) Death (of i) but sum lives on… reuse is OK recreation is not!

  29. What’s this all for ? Program for finding the factorial of a number… H.pl(“Type an integer n and I will print n!”); int n = H.ni(); // we have n – we need n factorial int result = ; // be sure to give an initial value H.pl(“The result is ” + result); Use the same idea for creating powers in Hw4 Pr1 !

  30. Perspective on for loops At the top of a CS 5 placement project file … // Name: CS5App // Author: Matt Beaumont // Purpose: To get me out of CS5... // ...no, really... // Purpose: To create and maintain a list // of films and directors /* Notes: * I haven't liked for-loops since the day I met them. * They bother me for some reason. Hence, no for-loops… */ class CS5App { ...

  31. Extreme Looping Java’s last and least built-in variable type: boolean H.pl(“It keeps on”); while ( true ) { H.pl(“going and”); } “while” block

  32. Extreme Looping Java’s last and least built-in variable type: boolean H.pl(“It keeps on”); while ( true ) { H.pl(“going and”); } note what’s NOT here! “while” block

  33. Extreme Looping with while No variable or expression is needed! H.pl(“It keeps on”); while ( true ) { H.pl(“going and”); int escape = H.randInt(1,100); if (escape == 100) { break; } } worry later about how to escape ! here is how to quit – use break !

  34. “User – friendly” code long myNumber = H.randLong(0,9000000000000000000L); while ( true ) { H.pl(“I’m thinking of a positive integer”); H.pl(“If you guess it, you may leave.”); H.pl(“Otherwise, you must continue…\n”); H.p(“What is your guess? ”); int yourGuess = H.ni(); if ( yourGuess == myNumber ) { break; // let me out ! } } A break breaks out of the enclosing switch, for, or while.

  35. “User – friendly” code long myNumber = H.randLong(0,9000000000000000000L); while ( true ) { H.pl(“I’m thinking of a positive integer”); H.pl(“If you guess it, you may leave.”); H.pl(“Otherwise, you must continue…\n”); H.p(“What is your guess? ”); int yourGuess = H.ni(); if ( yourGuess == myNumber ) break; // let me out ! H.p(“Would you like to continue playing? ”); String answer = H.nl(); if (answer == “no”) break; } same as before but friendlier!

  36. Monty Hall Let’s make a deal ’63-’86 inspiring the “Monty Hall paradox” Hw4Pr2) The Virtual Monty Hall Sept. 1990

  37. The two Monte Carlos Making random numbers work for you! • math hw • cs hw • physics hw • Hum hw Monte Carlo casino, Monaco Monte Carlo methods, Math/CS

  38. Monte Carlo Monty Hall Suppose you always switch to the other door... What are the chances that you will win the car ? Run it (randomly) 1000 times and see! Hw4Pr3) Monte Carlo Monty Hall

  39. Monty Hall program design top-down software engineering HW4PR3) Virtual Monty Hall while (true) // program skeleton via short comments { } Detailed design this Friday in recitation…

  40. Monte Carlo in action Suppose you roll two dice. What are the chances that you roll doubles? set up a variable to count the number of doubles rolled int doublesCount = 0; for (int i=0 ; i<1000 ; ++i) { int d1 = H.randInt(1,6); int d2 = H.randInt(1,6); if ( d1 == d2 ) { ++doublesCount; } } H.pl(“We rolled ” + doublesCount + “ doubles.”); 1000 times one roll of the dice count the number of doubles rolled

  41. Summary statements and shortcuts for changing variables num += 10; x *= 10; ++i; i++; --i; i--; multiply x by 10 increase num by 10 increase i by 1 decrease i by 1 for loop examples int x = 3; for (int i=0 ; i<9 ; ++i) { H.p( x + “ ” ); x = x + 12; } for (int i=12 ; i>=0 ; i-=2) { H.p( i + “ ” ); } prints 12 10 8 6 4 2 0 prints 3 15 27 39 51 63 75 87 99 while loop examples while ( true ) { String s = H.nw(); if (s.equals(“no”)) break; } continues until the user types “no”

  42. Lab Today N-Z all others welcome! • I’d suggest starting by writing the for loops in Hw4Pr1 10 15 20 25 30 35 40 45 50 55 60 65 11 22 33 44 55 66 77 88 99 110 121 1 2 4 8 16 32 64 128 256 512 1024 2048 0 3 9 12 36 39 117 120 360 363 1089 1 22 333 4444 55555 666666 7777777 88888888 use loops to print these sequences print the sum of the first four sequences, too … Hw4Pr1) Improved Math Menu Pair Programming Problem • then move on to the more involved examples... Hw4Pr2 Virtual Monty Hall (while) Hw4Pr3 Monte Carlo Monty Hall (for)

  43. Summary statements and shortcuts for changing variables num += 10; x *= 10; ++i; i++; --i; i--; multiply x by 10 increase num by 10 increase i by 1 decrease i by 1 for loop examples int x = 3; for (int i=0 ; i<9 ; ++i) { H.p( x + “ ” ); x = x + 12; } for (int i=12 ; i>=0 ; i-=2) { H.p( i + “ ” ); } prints 12 10 8 6 4 2 0 prints 3 15 27 39 51 63 75 87 99 while loop examples while ( true ) { String s = H.nw(); if (s.equals(“no”)) break; } continues until the user types “no”

  44. Names: “Quiz” Print the output of these loops: for (int i = 10 ; i > 0 ; i+=1 ) { H.p( i + “ ” ); } A int i = 20; while (i > 0) { i /= 2; H.p( i + “ ” ); } B int s = 0; for (int x = 1 ; x < 5 ; ++x) { H.p( x + “ ” ); s = s + x; } H.pl(“\ns is ” + s); C

  45. Write a loop to print this sequence: Write a loop to print this sequence: 9 terms total 9 terms total 0 1 2 3 6 7 14 15 30 0 00 000 0000 ... “Quiz”, part 2 Hint: Use a for loop with an if inside it! Hint: Use a for loop with a for loop inside it!

  46. A for (int i = 10 ; i > 0 ; i = i-2) { H.p( i + “ ” ); } prints B int i = 20; while (i > 0) { i /= 2; H.p( i + “ ” ); } prints int s = 0; for (int x = 1 ; x < 5 ; ++x) { H.p( x + “ ” ); s = s + x; } H.pl(“\ns is ” + s); C prints

  47. A for (int i = 10 ; i > 0 ; i = i-2) { H.p( i + “ ” ); } prints 10 8 6 4 2 B int i = 20; while (i > 0) { i /= 2; H.p( i + “ ” ); } prints int s = 0; for (int x = 1 ; x < 5 ; ++x) { H.p( x + “ ” ); s = s + x; } H.pl(“\ns is ” + s); C prints

  48. for (int i = 10 ; i > 0 ; i = i-2) { H.p( i + “ ” ); } prints 10 8 6 4 2 int i = 20; while (i > 0) { i /= 2; H.p( i + “ ” ); } prints 10 5 2 1 0 int s = 0; for (int x = 1 ; x < 5 ; ++x) { H.p( x + “ ” ); s = s + x; } H.pl(“\ns is ” + s); prints

  49. A for (int i = 10 ; i > 0 ; i = i-2) { H.p( i + “ ” ); } prints 10 8 6 4 2 B int i = 20; while (i > 0) { i /= 2; H.p( i + “ ” ); } prints 10 5 2 1 0 int s = 0; for (int x = 1 ; x < 5 ; ++x) { H.p( x + “ ” ); s = s + x; } H.pl(“\ns is ” + s); C prints 1 2 3 4 s is 10

  50. 9 terms total Write a loop to print this sequence: 0 1 2 3 6 7 14 15 30 i = 8 i = 6 i = 2 i = 4 i = 0 i = 7 i = 3 i = 5 i = 1 int x = 0; for (int i=0 ; i<9 ; ++i) { H.p( x + “ ” ); if ( i%2 == 0 ) { x = x + 1; } else { x = x * 2; } } shorter?

More Related