“Computer Science is no more about computers than astronomy is about telescopes.” — E. W. Dijkstra Algorithms CSE 130: Introduction to C Programming Spring 2005 “Don’t sweat it — it’s only ones and zeros.” — P. Skelly 1 Algorithms 2 Algorithm Attributes • Algorithms are the heart of computer • An algorithm must be: • complete • precise • finite • An algorithm should describe exactly how science • An algorithm is a description of the steps necessary to solve a problem • “problem” = “task to be performed” • An algorithm is a “blueprint” for a program • A computer program embodies an algorithm to perform a given procedure 3 Developing an Algorithm 4 Algorithm Example 1 • Start with the input and desired output • Divide and conquer the problem • Iterative refinement of solution • Present your solution as pseudocode • When each solution step looks like a • Compute the average of a list of numbers • Input: a list of numbers • Output: their average program statement, you’re done! 5 6 Divide and Conquer Pseudocode Solution 1. Initialize total and count to 0 1. Add up the numbers 2. For each number: 2. Count the total number of values 1. Add it to the total 3. Average = total / count 2. Add 1 to count 3. Divide total by count 7 Algorithm Example 2 8 Making Change 1. Get the amount of money to return • Problem: Given a monetary amount, print 2. Compute the number of quarters needed the number and types of coins required to make change 3. Compute the number of dimes needed • Input: a number between 0 and 99 inclusive • Output: a number of quarters, dimes, 4. Compute the number of nickels needed 5. Compute the number of pennies needed nickels, and pennies equal to the input 6. Print the results 9 Making Change, part 2 10 Algorithm Example 3 • Problem: Create a receipt for a list of purchased items. Print the total price, compute the total with 8.25% tax, and print the money tendered (plus any change given) 1. 2. Compute the number of quarters needed 1. number of quarters = total / 25 • Input: a list of items and prices • Output: a list of items and prices, plus the 2. remainder = remainder % 25 total, total with tax, and cash tendered 11 12 Printing a Receipt Printing a Receipt 2 1. Set the total to $0.00 5. Print total with tax 2. For each item: 6. Get cash tendered 1. Print the item name and price 7. Print cash tendered 2. Add item price to total 8. If cash tendered > total with tax: 3. Print total 1. Make change 4. Total with tax = 1.0825 * total 2. Print change 13 14 Problem Solving Process The Feynman Problem-Solving Algorithm: • What is it? • Analysis • Design • Implementation • Testing (1) write down the problem; (2) think very hard; (3) write down the answer. — Murray Gell-mann 15 Problem Solving Process • What is it? – Analysis 16 Problem Solving Process • What is it? • Analysis Determine the inputs, outputs, and other components of the problem Description should be sufficiently specific to allow you to solve the problem • Design • Implementation • Testing Describe the components and associated processes for solving the problem Straightforward and flexible – Design • Implementation • Testing 17 18 Problem Solving Process Problem Solving Process • What is it? • Analysis • Design • What is it? • Analysis • Design • Implementation Develop solutions for the components and use those components to produce an overall solution Straightforward and flexible – Implementation • Testing – Testing Test the components individually and collectively 19 Problem Solving Process 20 Problem Solving Methodologies • How to do it? • Depends upon your mode of thinking • Bricolage approach • Planned approach 21 Problem Solving Methodologies Problem Solving Methodologies • How to do it? • Depends upon your mode of thinking • Bricolage approach 22 • How to do it? • Depends upon your mode of thinking Problem features and aspects are repeatedly tried and manipulated according to your personal way of organizing information • Bricolage approach A mistake is not an error, but a correction waiting to be made in the natural course of solving the problem • Planned approach • Planned approach 23 Uses logic, formalism, and engineering coupled with a structured methodology Inherent structure of the planned approach offers makes it easier to show correctness of solution Dominant method in terms of teaching 24 Tips Tips • Find out as much as you can • Reuse what has been done before • Expect future reuse • Break complex problems into • Find out as much as you can Find out what is known about the problem subproblems Talk to the presenter Determine what attempts have succeeded and what 25 Tips 26 Tips • Find out as much as you can • Find out as much as you can Research can require significant time and generate questions Consider: The effort is worthwhile because the result is a better understanding Sketching a solution and then repeatedly refine its components 27 Tips 28 Tips • Reuse what has been done before • Reuse what has been done before Your time is valuable Correctness is probably even more valuable Be open to indirect use of existing materials 29 30 Tips Tips • Break complex problems into • Expect future reuse subproblems Make as few assumptions as necessary Divide-and-conquer Maximizes the likelihood that your effort can be used in future situations 31 32 Algorithm Building Blocks Programming Process 1. Specify the problem to be solved 1. Expressions 2. Develop an appropriate algorithm 2. Conditional (selection) statements 3. Code the algorithm using a programming language (i.e., C) 3. Iteration (repetition) statements 4. Subprogram invocations 4. Test the resulting code Closely related to expressions 33 Next Time • Programming Languages • Creating Simple Programs • Basic Input/Output 35 34