710 likes | 723 Views
Understand the algorithmic model, programming, and abstraction in computing. Learn the key properties of good algorithms and their components through examples. Enhance your algorithmic thinking skills.
E N D
Lecture 2 AnnouncementsThe Algorithmic ModelProgramming LanguagesDataAssignmentArithmetic
Announcements • Web Page http://www.cc.gatech.edu/classes/AY2001/cs1311/
Announcements • Newsgroups • git.cc.class.cs1311.announce • git.cc.class.cs1311.rec • git.cc.class.cs1311.lab • git.cc.class.cs131x.macusers • Reading newsgroups • On Campus: Outlook Express/Netscape/tin • Off Campus: tin
The Algorithmic Model • What is Computer Science • What is Programming • Algorithms • Defined • Properties of Good Algorithms • Describing Algorithms • Examples • Components of Algorithms
Why? What is Computer Science • Not really a science in the traditional sense • Study of algorithms and how to use them • NOT study of computers • Software engineering is important! • Analysis and design of systems • Programming • Testing and maintenance
What is Programming • Programming requires two skills: • Algorithmic thinking • Knowledge of programming language syntax • Learning language syntax is the easy part
How We Learn Computer Science • We use pseudo-code to focus on learning algorithmic thinking. • From there, you can go anywhere! • 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 “trial and error” and twiddling.
Algorithm Defined • A set of logical steps to accomplish a task • One way to solve a problem • A “recipe of action” • A way of describing behavior Algorithms contain: • Data • Instructions
Algorithms in Computing Input Data Algorithm Output Data
Algorithm “Recipe” Chocolate Chip Cookies DATA (ingredients) 2 1/4 cups flour 1 tsp salt 1 tsp baking soda 2 eggs 3/4 cup brown sugar 1 tsp vanilla ext. 3/4 cup gran’d sugar 1 cup soft butter 12oz. semi-sweet chocolate chipsINSTRUCTIONS (steps) 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
Abstraction • a general idea or term • an impractical idea; visionary and unrealistic • general characteristics apart from concrete realities, specific objects or actual instances • withdrawal • absent-mindedness; inattention • a work of art stressing formal relationships (Random House Dictionary of the English Language)
Abstraction in Computing • Refers to the logical grouping of concepts or objects • Define/implement the general idea • Isolate the details • Helps readability and understandingof our algorithms
Abstraction Examples • Algorithms • Variable names • Procedures and functions • Data structures • The computer language itself!
Abstraction in Algorithms The Abstraction: Get to the College of Computing at Georgia Tech One Implementation: begin get on I-85 heading towards Midtown Atlanta exit I-85 at the 10th street exit proceed along exit ramp to 10th street turn west on 10th street turn left on Atlantic Drive stop in front of CoC building end Other implementations of the same abstraction can be just as correct, better, or worse
What’s Wrong With This Algorithm? (From back of shampoo bottle) Directions: Wet Hair Apply a small amount of shampoo Lather Rinse Repeat
Properties of Good Algorithms Good algorithms are • Precise • Unambiguous • Complete • Correct • Simple • Contain levels of abstraction
Describing Algorithms • Natural language (English) • Pictures • Pseudo-code or a specific programming language
Make a list of courses you want to register for, in order of priority • Start with empty schedule. Number of hours = 0. • Choose highest priority class on list. • If the chosen class is not full and its class time does not conflict with classes already scheduled, then register for the class (2 steps): • 4.a. Add the class to the schedule • 4.b. Add the class hours to the number of hours scheduled • Cross that class off of your list. • Repeat steps 3 through 5 until the number of hours scheduled is >= 15, or until all classes have been crossed out. • Stop.
Begin Make list of classes you want to take Num Hours = 0 Choose highest priority class on list yes Is this class full? no yes Is there a time conflict? no Add the class to your schedule. Add class hours to Num Hours. Cross the class off your list. yes Num Hours >= 15? no yes More classes on list? End no Flowcharts
Components of Algorithms Any computing algorithm will have AT MOST five kinds of components: • Data structures to hold data • Instructions change data values • Conditional expressions to make decisions • Control structures to act on decisions • Modules to make the algorithm manageable by abstraction, i.e., grouping related components
Overview of Programming Languages • Allows programmers to describe desired behaviors (algorithms) • The audiences are • Humans • Computers (only understand binary) • Consists of: • Built-in • User-defined
Program Language Language Built-in User-defined Data Operators Assignment Arithmetic Input/Output Relational Boolean Data Operators Atomic Number Character Boolean Pointer Complex String
Relations between Problems, Algorithms, and Programs Problem Algorithm . . . . Algorithm . . . . Program Program . . . . Program Program
Data Structures Data Structures are containers for data. The simplest of them are called “atomic” because they hold only a single value and cannot be subdivided into lower-level components Other “complex” data structures may hold multiple pieces of data, and are constructed from atomic types.
Built-in Data Types Four built-inatomic types: - Character (char) - Number (num) - Boolean (boolean) - Pointer (ptr) One built-incomplex type: - String (string)
Abstracting the Memory • Memory consists of 1’s and 0’s • We want to abstract out the details • Create a space in memory for a specific type of information • Access this location in memory using a meaningful name
Three Steps in Using Memory • Declare • Initialize (no default value) • Manipulate
Declaring Variables Variables are data structures whose value may be changedby the algorithm. Each variable must be declared to have an identifier (i.e., a name) and be of some data type. For example: to declare a variable named “counter” that can hold a numeric value: counter isoftype Num
Initialization Each variable must first be assigned a value of the appropriate type after being declared. For example: to assign the value 0 to the variable counter: counter <- 0 Variables do not contain information until they are initialized (I.e. no default value).
Numbers Declared as: my_num isoftype Num my_num can now store one (integer or real) number of any magnitude. Examples: my_num <- 1 // this is OK my_num <- -65 // so is this my_num <- 14.5 // and so is this my_num <- ‘a’ // an ERROR
Characters Declared as: your_gradeisoftype Char your_grade can now store any one alphanumeric character. Single-quotes used to distinguish Char values. Examples: your_grade <- ‘A’ // this is OK your_grade <- ‘A-’ // an ERROR your_grade <- ‘9’ // this is OK your_grade <- 9 // an ERROR
Booleans Declared as: this_test isoftype Boolean this_test can now hold one boolean (TRUE or FALSE) value. Examples: this_test <- TRUE // this is OK this_test <- FALSE // so is this this_test <- “FALSE” // an ERROR this_test <- “TRUE” // an ERROR
String Strings can holds a collection of characters. Declared as: this_string isoftype String this_string can now hold a “string” of any number of alphanumeric characters. Examples: this_string <- “hello world” // OK this_string <- hello world // ERROR this_string <- “45” // OK this_string <- 45 // ERROR
Distinguishing Data Types • “Woman, without her, man is nothing” • “Woman, without her man, is nothing” my_char <- ‘a’ // single quotes my_string <- “a” // double quotes
Pointers Declared as:this_num_ptr isoftype ptr toa Num this_num_ptr can now “know” where a Num variable “lives” in memory. this_num_ptr 42 Don't worry about pointers for now.
The Assignment Operator The assignment operator stores a particular value in a variable. Assignment: <- Format: variable <- value/variable
Type Matching The type of variable on the left side must match the type of value/variable on the right side: my_num <- 42 my_num <- your_num