1 / 19

More Algorithms

This lecture covers control structures in pseudocode, including sequence, selection, and repetition. It also explains Euclid's Algorithm for finding the greatest common divisor of two integers.

sgerald
Télécharger la présentation

More Algorithms

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. More Algorithms 2008/09/24: Lecture 6bCMSC 104, Section 0101 John Y. Park

  2. More Algorithms Topics • Algorithms: Elements in Depth • In-Class Project: Euclid’s Algorithm • In-Class Project: Student Voluteerizer Reading • None

  3. Pseudocode:Control Structures Any problem can be solved using only three logical control structures: • Sequence • Selection • Repetition

  4. Sequence • A series of steps or statements that are executed in the order they are written. • Example: Display “Enter two numbers: “ Read <number1> Read <number2> <sum> = <number1> + <number2> Display “sum = “, <sum>

  5. Sequence • Some languages… • Have line numbers • Allow a set of sequenced steps to be grouped as a “statement block” • Explicitly bracketed (e.g., with “begin…end” or {…} • Allow “goto”s • Evil-evil-evil!!!!!

  6. Sequence • Goto e.g.: <a> = 0 goto Label3: Label1: DISPLAY “Hello” DISPLAY “bye” Label2: <a> = <a> + 1 DISPLAY <a> goto Label1: DISPLAY “Never get here…” Label3: DISPLAY “Starting up” goto Label2:

  7. Sequence • You should try to avoid the “goto” like the plague! • In this class, you will never need to use a “goto” • DON’T USE GOTOs!!! • Why did I even mention it? • It is a vehicle for explaining what other control structures are doing, in a more logical manner

  8. Selection • Defines one or more courses of action depending on the evaluation of a condition. • Synonyms: conditional, branching, decision • Examples:If (condition is true) If (condition is true) do this do this End_if Else do that End_if

  9. Selection • More complex examples: • DISPLAY “Enter number to invert”READ <my_num>If (<my_num> < 0) DISPLAY “Don’t like negative numbers” if (<my_num> < -999) DISPLAY “… but I guess you really do!” End_ifElse_if (<my_num> == 0) <result> = 0Else <result> = 1 / <my_num>End_if

  10. Repetition • Allows one or more statements to be repeated as long as a given condition is true. • Synonyms: looping, iteration • Example: While (condition is true) do this End_while

  11. Repetition • More complex example (with mistakes) • DISPLAY “Enter number to compute factorial for”READ <my_num>While (<my_num> > 0) <factorial> = <factorial> * <my_num> <my_num> = <my_num> - 1End_whileDISPLAY “The factorial of”, <my_num>, “ is “, <factorial>

  12. Repetition • More complex example (with mistakes) • DISPLAY “Enter number to compute factorial for”READ <my_num><factorial> = 0While (<my_num> > 0) <factorial> = <factorial> * <my_num> <my_num> = <my_num> - 1End_whileDISPLAY “The factorial of”, <my_num>, “ is “, <factorial>

  13. Repetition • More complex example (with mistakes) • DISPLAY “Enter number to compute factorial for”READ <my_num><factorial> = 0<saved_my_num> = <my_num>While (<my_num> > 0) <factorial> = <factorial> * <my_num> <my_num> = <my_num> - 1End_while<my_num> = <saved_my_num> DISPLAY “The factorial of”, <my_num>, “ is “, <factorial>

  14. Pseudocode Style • Any user prompts should appear exactly as you wish the programmer to code them. • The destination of any output data should be stated, such as in “Display”, which implies the screen. • Make the data items clear (e.g., surround them by < and > ) and give them descriptive names. • Use formulas wherever possible for clarity and brevity. • Use keywords (such as Read and While) and use them consistently. Accent them in some manner.

  15. Pseudocode (con’t)[Review] • Use indentation for clarity of logic. • Avoid using code. Pseudocode should not be programming language-specific. • Always keep in mind that you may not be the person translating your pseudocode into programming language code. It must, therefore, be unambiguous. • You may make up your own pseudocode guidelines, but you MUST be consistent.

  16. Euclid’s Algorithm Problem: Find the largest positive integer that divides evenly into two given positive integers (i.e., the greatest common divisor). Algorithm: • Assign M and N the values of the larger and smaller of the two positive integers, respectively. • Divide M by N and call the remainder R. • If R is not 0, then assign M the value of N, assign N the value of R, and return to Step 2. Otherwise, the greatest common divisor is the value currently assigned to N.

  17. Finding the GCD of 24 and 9 So, 3 is the GCD of 24 and 9.

  18. Euclid’s Algorithm Tips about problem # 1 : • The user should specify the two numbers to factor. She may enter them in any order (i.e., don't assume first is greater). • You will need a new arithmetic operation for computing remainders: the '%' operator. E.g.: • "24 % 7" equals "3" • “-5 % 2” equals “-1”, and "18 % -8" equals "+2" • "7 % 0" equals "the end of the world as we know it." • The user may input one or both values as negative numbers. You must either: • explicitly test for and reject negative numbers • make sure your algorithm computes the correct answer with them

  19. Student Voluteerizer Problem: Write a generic algorithm for helping call on student “volunteers” in a “fair” manner <obviously underspecified…>

More Related