CSE 1321 Module 1 Algorithms vs Programming 1022020

  • Slides: 17
Download presentation
CSE 1321 Module 1 Algorithms vs. Programming 10/2/2020 CSE 1321 Module 1 1

CSE 1321 Module 1 Algorithms vs. Programming 10/2/2020 CSE 1321 Module 1 1

Motivation To learn algorithmic thinking with emphasis on pseudocode development; To understand what an

Motivation To learn algorithmic thinking with emphasis on pseudocode development; To understand what an algorithm is and how to write an algorithm; To describe the concept of abstraction To describe the relationship between algorithms and programming 10/2/2020 CSE 1321 Module 1 2

Topics 1. Algorithms 1. 2. 3. 4. 5. Definition Properties of Good Algorithms Describing

Topics 1. Algorithms 1. 2. 3. 4. 5. Definition Properties of Good Algorithms Describing Algorithms Examples Components of Algorithms 2. Fine, then what is Programming? 10/2/2020 CSE 1321 Module 1 3

What is an Algorithm? What it’s not: a logarithm** What it is: • A

What is an Algorithm? What it’s not: a logarithm** What it is: • A set of logical steps to accomplish a specific task • A set of direction/instructions • You use them everyday, even right now Algorithms contain: • Data • Instructions that work on that data **. . Which shares the same letters as “algorithm” 10/2/2020 CSE 1321 Module 1 4

Algorithms: the visual Too much screen time Input Data Algorithm Output Data 10/2/2020 CSE

Algorithms: the visual Too much screen time Input Data Algorithm Output Data 10/2/2020 CSE 1321 Module 1 5

Algorithms: a Recipe Chocolate Chip Cookies Ingredients: 2 1/4 cups flour 1 tsp baking

Algorithms: a Recipe Chocolate Chip Cookies Ingredients: 2 1/4 cups flour 1 tsp baking soda 3/4 cup brown sugar 3/4 cup granulatd sugar 12 oz. semi-sweet chocolate chips 1 tsp salt 2 eggs 1 tsp vanilla ext. 1 cup soft butter Instructions Where’s the: - Input data? - Algorithm? - Output data? Preheat oven to 375°. Combine flour, salt, baking soda, in bowl, set mixture aside. Combine sugars, butter, vanilla, beat until creamy. Add eggs and beat. Add dry mixture and mix well. Stir in chocolate chips Drop mixture by teaspoons onto ungreased cookie sheet Bake 8 to 10 minutes 10/2/2020 CSE 1321 Module 1 6

Abstraction – the level of detail 1. a general idea or term 2. an

Abstraction – the level of detail 1. a general idea or term 2. an impractical idea; visionary and unrealistic 3. general characteristics apart from concrete realities, specific objects or actual instances (Random House Dictionary of the English Language) In computing: • Abstraction refers to the LOGICAL GROUPING of concepts or objects • We want to hide the details (in functions – later) • We like to think at a high level of abstraction (no details) • Too many details overwhelm us! 10/2/2020 CSE 1321 Module 1 7

Abstraction Example Someone tell me to turn off the lights 10/2/2020 CSE 1321 Module

Abstraction Example Someone tell me to turn off the lights 10/2/2020 CSE 1321 Module 1 8

What is Wrong with this Algorithm? (From back of shampoo bottle) Directions: Wet Hair

What is Wrong with this Algorithm? (From back of shampoo bottle) Directions: Wet Hair Apply a small amount of shampoo Lather Rinse Repeat Lesson learned: Just because we have algorithm doesn’t mean it’s a good one 10/2/2020 CSE 1321 Module 1 9

Properties of Good Algorithms Good algorithms are: • • • 10/2/2020 Precise – why?

Properties of Good Algorithms Good algorithms are: • • • 10/2/2020 Precise – why? Unambiguous – why? Complete – why? Correct – why? Simple – why? Contain levels of abstraction – why? CSE 1321 Module 1 10

Describing Algorithm Can be created using: • Natural language (e. g. English) • Pictures

Describing Algorithm Can be created using: • Natural language (e. g. English) • Pictures or flowcharts • Pseudocode or a specific programming language We’ll use pseudocode in this course • It’s a mixture of languages • Not concerned with syntax, but concepts You’ll use it through the rest of your career 10/2/2020 CSE 1321 Module 1 11

Example: Natural Language Algorithm 1. START by make a list of courses you want

Example: Natural Language Algorithm 1. START by make a list of courses you want to register for, in order of priority. 2. Number of hours = 0. 3. Choose highest priority class on list. 4. IF the chosen class is not full and its class time does not conflict with classes already scheduled, THEN register for the class: 4. a. Add the class to the schedule. 4. b. Add the class hours to the number of hours scheduled. 5. Cross that class off of your list. 6. Repeat steps 3 through 5 until the number of hours scheduled is >= 15, or until all classes have been crossed. 7. END ALGORITHM. 10/2/2020 CSE 1321 Module 1 12

START Make list of classes you want to take Example: Flowchart Algorithm Num Hours

START Make list of classes you want to take Example: Flowchart Algorithm Num Hours = 0 Choose highest priority class on list yes Is this class full? no Is there a time conflict? yes no Add the class to your schedule. Add class hours to Num Hours. Cross the class off your list. Num Hours >= 15? no More classes on list? yes END no 10/2/2020 yes CSE 1321 Module 1 13

Components of Algorithm Any computing algorithm will have AT MOST five kinds of components:

Components of Algorithm Any computing algorithm will have AT MOST five kinds of components: • • • Data structures to hold data Instructions to change data value Conditional expressions to make decisions Control structures to act on decisions Modules to make the algorithm manageable by abstraction (i. e. , grouping related components) 10/2/2020 CSE 1321 Module 1 14

Fine! Then what is Programming? • Programming requires two skills: • Algorithmic thinking (hard!)

Fine! Then what is Programming? • Programming requires two skills: • Algorithmic thinking (hard!) • Knowledge of programming language syntax • The syntax is the easy part • Programming: 1. Start by developing a good algorithm 2. Then, convert into a language-specific syntax 10/2/2020 CSE 1321 Module 1 15

How to Learn Programming • We use pseudo-code to focus on learning algorithmic thinking

How to Learn Programming • We use pseudo-code to focus on learning algorithmic thinking • Not using a computer to compile and run your program forces you to mentally execute your program and debug it! • Too many programmers try to program by brute force and twiddling in an IDE. Wrong approach! 10/2/2020 CSE 1321 Module 1 16

Summary Programming starts with algorithm development An algorithm is just a set of instructions

Summary Programming starts with algorithm development An algorithm is just a set of instructions Abstraction is just the level of detail A good algorithm exhibits precision, clarity, completeness, correctness, and simplicity. • An algorithm can be described using a natural language, pictures and diagrams, and pseudocode or a specific programming language. • • 10/2/2020 CSE 1321 Module 1 17