# Algorithms CSC1310 Fall 2009. What Is Programming? Programming Programming means writing down a series of instructions that tell a computer what to do.

## Presentation on theme: "Algorithms CSC1310 Fall 2009. What Is Programming? Programming Programming means writing down a series of instructions that tell a computer what to do."— Presentation transcript:

Algorithms CSC1310 Fall 2009

What Is Programming? 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.

Algorithms algorithm 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.

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.

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.

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.

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

Ways to Express Algorithms Natural languages Natural languages. Allows anyone who understands that language to read the algorithm, but lacks precision. Programming languages Programming languages. Precise, yet simple enough for computers to understand. Pseudocode. 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.

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

Chapter 9 IF Statement CSC1310 Fall 2009

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 Logic process ( “==”, “!=”, “>”, “>=”, “<”, “<=”)

Simple If statement if It takes the form of an if test if :

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”

If... else statement if else It takes the form of an if test, and ends with an optional else block if : else:

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”

If… else if … else statement if elif else It takes the form of an if test, followed by one or more optional elif tests, and ends with an optional else block if : elif : else:

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

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'

Syntax Rules Statements execute one after another, unless you say otherwise. automatically Block and statement boundaries are detected automatically (no “end/begin” delimiters). Header “:” indented statements Header “:” indented statements (pattern of all compound statements). Statement delimiters 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. non-compound 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.

Truth Test X and Y bothtrue 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 eithertrue 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 false not X is true if X is false. >>> not 2, not {}, not [1,2,3,4,5]

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

Download ppt "Algorithms CSC1310 Fall 2009. What Is Programming? Programming Programming means writing down a series of instructions that tell a computer what to do."

Similar presentations