100 likes | 112 Views
ITM 352 Algorithms. We'll go into greater depth on this later. Today we will just see and play with some simple examples. A Brief Tour of Programming Logic and Algorithms. What is Programming?. Direct (specify) computer's actions in detail
E N D
We'll go into greater depth on this later. Today we will just see and play with some simple examples A Brief Tour of Programming Logic and Algorithms
What is Programming? • Direct (specify) computer's actions in detail • Uses a language that humans can deal with and translates it into something the computer can process. • Builds a software representation of something (a model) that people value in some way (e.g. solves a problem).
Qualities of Algorithms • An algorithm expects a defined set of inputs. • An algorithm produces a defined set of outputs. • An algorithm is guaranteed to terminate and produce a result. • Most algorithms are guaranteed to produce the correct result. • Algorithms may impose requirements on inputs (called preconditions). For example, a precondition might be that an algorithm will only accept positive numbers as an input. Programs are just a series of algorithms!
Example Algorithm in Pseudo-Code get_dressed( int temperature ) { if( temperature > 75 ) /* it's hot out */ put_on( tshirt ); put_on( shorts ); put_on( sandals ); else if( temperature > 60 ) /* it's warm out */ putOn( blue_shirt ); putOn( blue_jeans ); putOn( white_socks ); putOn( sneakers ); else /* it's kind of cold */ putOn( sweatshirt ); putOn( blue_jeans ); putOn( wool_socks ); putOn( shoes ); putOn( jacket ); }
Example (Bad) Algorithm • Preheat oven to 350 degrees. • Grease and flour a 9x9 inch pan. • In a medium bowl, cream together 1 cup sugar and ½ cup butter. • Beat in 2 eggs, one at a time, then stir in 2 tsp vanilla. • Bake until done.
Example (Bad) Algorithm • Add shampoo to hair. • Lather. • Rinse. • Repeat.
Example: Making Change • Everyone knows how to make change • When given an amount of change, we will all do exactly the same thing • Q: if you owe someone 73 cents and have a drawer full of pennies, nickels, dimes, and quarters, how many of each will you give?
Example: Making Change - 2 • A: 2 quarters, 2 dimes, 3 pennies • Did anyone get a different answer? • Why not? • Because we are all using the same algorithm!
Example: Making Change - 3 • Create the making change algorithm. • You know how to do it! The problem is writing it down clearly and unambiguously. • Assume you are giving this to someone who has never made change before (but who can do arithmetic). • So you must be very clear and explicit and assume nothing.