1 / 21

Algorithms

Algorithms. CSC1310 Fall 2009. What Is Programming?. Programming means writing down a series of instructions that tell a computer what to do. Properties of these instructions: Computation proceeds in discrete steps. Each step is precisely defined.

cherie
Download Presentation

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. Algorithms CSC1310 Fall 2009

  2. What Is Programming? • Programming means writing down a series of instructions that tell a computer what to do. • Properties of these instructions: • Computation proceeds in discrete steps. • Each step is precisely defined. • The order in which steps are performed may be important.

  3. Algorithms • A set of instructions with these properties is said to be an algorithm. • The steps in an algorithm are not always short and simple. • Some steps may involve a series of smaller steps. • Some steps may involve making decisions. • Some steps may repeat. • Algorithms are common in the real world.

  4. The Recipe 1. Melt slowly and keep warm: 1/2 cup butter. 2. Barely heat: 1 1/2 tablespoons lemon juice, dry sherry, or tarragon vinegar. 3. Place in the top of a double boiler over—not in—hot water: 3 egg yolks. 4. Beat the yolks with a wire whisk until they begin to thicken. Add: 1 tablespoon boiling water. 5. Beat again until the eggs begin to thicken. Repeat this process until you have added: 3 more tablespoons water. 6. Then beat in the warm lemon juice. 7. Remove double boiler from heat. 8. Beat the sauce well with a wire whisk. Continue to beat while slowly adding the melted butter and: 1/4 teaspoon salt; a few grains cayenne. 9. Beat until the sauce is thick. 10.Serve at once.

  5. The Recipe as an Algorithm • This recipe satisfies most of the requirements for an algorithm: • It involves discrete steps. • Each step is more or less precisely defined. • The order of the steps matters, at least to some extent. • Some steps require making decisions and repeating actions. • This recipe is detailed enough for an experienced cook, but it might pose problems for a novice. • Computer algorithms need to be much more precise.

  6. Computer Algorithms • Computer algorithms often involve obtaining input, performing a calculation, and producing output. • An algorithm for converting from Fahrenheit to Celsius: 1. Ask the user to enter a Fahrenheit temperature. 2. Obtain the input entered by the user. 3. Convert the user’s input into numerical form. 4. Calculate the equivalent Celsius temperature, using the formula C = (F – 32)  (5 / 9) 5. Convert the Celsius temperature into character form and display the result.

  7. Boil Water for a Tea Algorithm • Input: empty kettle, stove, faucet • Output: boiled water in the kettle. • Algorithm? • Reuse of algorithm.

  8. Ways to Express Algorithms • Natural languages. Allows anyone who understands that language to read the algorithm, but lacks precision. • Programming languages. Precise, yet simple enough for computers to understand. • Pseudocode. A mixture of natural language and a programming language. More precise than natural language but less precise than a programming language. Often easier to read (and to write) than a programming language.

  9. Fahrenheit to Celsius Conversion in Python 1. Ask the user to enter a Fahrenheit temperature. 2. Obtain the input entered by the user. 3. Convert the user’s input into numerical form. >>>x=float(raw_input(“Please, enter Fahrenheit temperature:\n”)) 4. Calculate the equivalent Celsius temperature, using the formula C = (F – 32)  (5 / 9) 5. Convert the Celsius temperature into character form and display the result. >>>print “The Celsius temperature is ” +str((x-32)*(5.0/9))

  10. Chapter 9 IF Statement CSC1310 Fall 2009

  11. If Statement • The main statement used for selecting from alternative actions based on test results • It’s the primary selection tool in Python and represents the Logicprocess ( “==”, “!=”, “>”, “>=”, “<”, “<=”)

  12. Simple If statement • It takes the form of an if test • if <test>: <statement>

  13. Simple If statement >>>if 4>3: print “it is true that 4>3” >>>if 1: print “true” >>>a=10 >>>if a==10: print “a=10”

  14. If... else statement • It takes the form of an if test, and ends with an optional else block • if <test>: <statement1> else: <statement2>

  15. If... else statement >>>if 3>4: print “3 is larger than 4” else: print “3 is NOT larger than 4” >>>if {}: print “true” else: print “false”

  16. If… else if … else statement • It takes the form of an if test, followed by one or more optional elif tests, and ends with an optional else block if <test1>: <statement1> elif <test2>: <statement2> else: <statement3>

  17. If… else if … else statement >>>a=10 >>>if a>10: print “a > 10” elif a==10: print “a = 10” else: print “a < 10”

  18. If… else if … else statement example >>>number = 23 >>>guess = int(raw_input('Enter an integer : ')) >>>if guess == number: print 'Congratulations, you guessed it.' # New block starts here print "(but you do not win any prizes!)" # New block ends here elif guess < number: print 'No, it is a little higher than that' # Another block else: print 'No, it is a little lower than that'

  19. Syntax Rules • Statements execute one after another, unless you say otherwise. • Block and statement boundaries are detected automatically (no “end/begin” delimiters). • Header “:” indented statements (pattern of all compound statements). • Statement delimiters: • Statement may span lines if you’re continuing an open syntactic pair((),{},[]); • Statement may span lines if they end with backslash. • Block literals. • Several non-compound statements could be written at the same line being separated by semicolons. • You could move body of compound statement up to header line.

  20. Truth Test • X and Y is true if both X and Y are true. >>>a=5;b=3;c=2 >>> a>b and a>c >>> b>c and b>a >>>2 and 3, 3 and 2, {} and [], 3 and [] • X or Y is true if either X or Y are true. >>> b>c or b>a >>> b>a or b>c >>>2 or 3, 3 or 2, {} or [], 3 or [] • not X is true if X is false. >>> not 2, not {}, not [1,2,3,4,5]

  21. Non empty set • Assume user may input some data to A, B and C. • Program does not know if data was provided. >>>X = A or B or C or None

More Related