announcements the algorithmic model programming languages data assignment arithmetic

71
Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Upload: colt-wortham

Post on 01-Apr-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

AnnouncementsThe Algorithmic Model

Programming LanguagesData

AssignmentArithmetic

Page 2: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Announcements

• Web Page

http://www.cc.gatech.edu/classes/AY2001/cs1311/

Page 3: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic
Page 4: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic
Page 5: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic
Page 6: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic
Page 7: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

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

Page 8: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

The Algorithmic Model

Page 9: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

The Algorithmic Model

• What is Computer Science• What is Programming• Algorithms

– Defined– Properties of Good Algorithms– Describing Algorithms– Examples– Components of Algorithms

Page 10: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

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

Page 11: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

What is Programming

• Programming requires two skills:– Algorithmic thinking– Knowledge of programming language

syntax• Learning language syntax is the easy part

Page 12: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

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.

Page 13: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

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

Page 14: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Algorithms in Computing

Input Data

Algorithm

Output Data

Page 15: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

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 chips

INSTRUCTIONS (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

Algorithm “Recipe”

Page 16: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Abstraction

1. a general idea or term2. an impractical idea; visionary and unrealistic3. general characteristics apart from concrete

realities, specific objects or actual instances 4. withdrawal 5. absent-mindedness; inattention6. a work of art stressing formal relationships

(Random House Dictionary of the English Language)

Page 17: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

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

Page 18: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Abstraction Examples

• Algorithms• Variable names• Procedures and functions• Data structures• The computer language itself!

Page 19: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

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 buildingend

Other implementations of the same abstraction can be just as correct, better, or worse

Page 20: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Why do we stress abstraction?

Page 21: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

What’s Wrong With This Algorithm?

(From back of shampoo bottle)

Directions: Wet Hair Apply a small amount of shampoo Lather Rinse Repeat

Page 22: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Properties of Good Algorithms

Good algorithms are• Precise• Unambiguous• Complete• Correct• Simple• Contain levels of abstraction

Page 23: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Describing Algorithms

• Natural language (English)

• Pictures

• Pseudo-code or a specific programming language

Page 24: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

1. Make a list of courses you want to register for, in order of priority

2. Start with empty schedule. 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 (2 steps): 4.a. Add the class to the schedule4.b. Add the class hours to the number of hours

scheduled5. 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 out.

7. Stop.

Page 25: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Flowcharts

End

Begin

Make list of classes you want to take

Num Hours = 0

Choose highest priority class on list

Add the class to your schedule. Add class hours to Num Hours.

Cross the class off your list.

yes

yes

yes

no

no

no

Is this class full?

Is there a time conflict?

Num Hours >= 15?

More classes on list?yes

no

Page 26: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

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

Page 27: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Questions?

Page 28: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

View of Programming Languages

Page 29: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

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

Page 30: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Program Language

Language

Built-in User-defined

Data OperatorsAssignmentArithmeticInput/OutputRelationalBoolean

Data Operators

AtomicNumberCharacterBooleanPointer

ComplexString

Page 31: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Relations between Problems, Algorithms, and Programs

Problem

Algorithm Algorithm

Program ProgramProgram Program

. . . .

. . . . . . . .

Page 32: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Questions?

Page 33: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Data

Page 34: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

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.

Page 35: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Built-in Data Types

Four built-in atomic types: - Character (char) - Number (num) - Boolean (boolean) - Pointer (ptr)

One built-in complex type: - String (string)

Page 36: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Memory Lane

Page 37: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

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

Page 38: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Three Steps in Using Memory

• Declare

• Initialize (no default value)

• Manipulate

Page 39: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Declaring Variables

Variables are data structures whose value may be changed by 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

Page 40: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

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

Page 41: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

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

Page 42: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

CharactersDeclared as: your_grade isoftype 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

Page 43: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Booleans

Declared as: this_test isoftype Boolean

this_test can now hold one boolean (TRUE or FALSE) value.

Examples:this_test <- TRUE // this is OKthis_test <- FALSE // so is thisthis_test <- “FALSE” // an ERRORthis_test <- “TRUE” // an ERROR

Page 44: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

StringStrings 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” // OKthis_string <- hello world // ERRORthis_string <- “45” // OKthis_string <- 45 // ERROR

Page 45: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

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

Page 46: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

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_ptr42

Page 47: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Questions?

Page 48: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Assignment

Page 49: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

The Assignment Operator

The assignment operator stores a particular value in a variable.

Assignment: <-

Format:variable <- value/variable

Page 50: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

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

Page 51: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Assignment Examples

Examples:my_age <- 43your_age <- my_age // duplicates?your_grade <- ‘A’this_test <- TRUEhi_there <- “Hello World”

TRUE and FALSE are each single boolean values.

Page 52: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

More Examples

a <- b + c

Get the value of b from memory

Get the value of c from memory

Add the two values together

Store the result in memory location a

a <- a + 1

Get the value of a from memory

Add the number 1 to it.

Store the result in memory location a

Page 53: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Pointer Assignment

Given: this_ptr isoftype ptr toa Num that_ptr isoftype ptr toa NumThen: this_ptr <- new(Num) that_ptr <- new(Num)

Gives each pointer variable a new number variable to which it refers:

??

this_ptr that_ptr

Page 54: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Given above, then:

this_ptr^ <- 4

that_ptr^ <- 8

Assigns values to the variable to which the pointer refers.

Pointer Assignment

4this_ptr

8

that_ptr

Page 55: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Pointer Assignment

Given:

other_ptr <- this_ptrthis_ptr <- that_ptr

Assigns/reassigns pointers variables to reference other variables

4

this_ptr

8

that_ptr

other_ptr

4this_ptr

8

that_ptr

Page 56: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Questions?

Page 57: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Arithmetic

Page 58: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Arithmetic Operators

Arithmetic operators allow us to express mathematical operations:

• Addition +• Subtraction -• Multiplication *• Division /• Integer Division DIV• Modulo MOD

Page 59: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Order of Operations

Follow standard arithmetic order of operations:

• Multiplication & Division• Addition & Subtraction• Left to Right

Page 60: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Parentheses

• Use parentheses to control order of execution.• Suppose we want:

• Will x <- a + b / c - d work?

dc

bax

dc

bax

Page 61: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

So we want

x <- (a + b) / (c - d)

Page 62: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

DIV and MOD

DIV: keep whole number quotient, and throw away the remainder

MOD: keep the remainder, and throw away the whole number quotient

Examples:8 DIV 3 = 2 // 3 goes into 8 two times8 DIV 9 = 0 // 9 doesn’t go into 87 MOD 3 = 1 // remainder of 17 MOD 9 = 7 // remainder of 7

Page 63: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

MOD & DIV

7.3333 DIV 1 =

-6.3 DIV 2 =

0 MOD 3 =

1 MOD 3 =

2 MOD 3 =

3 MOD 3 =

4 MOD 3 =

5 MOD 3 =

6 MOD 3 =

7

-3

0

1

2

0

1

2

0

Page 64: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Questions?

Page 65: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Input and Output

Page 66: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Input and Output

I/O Operators allow us to communicate with the “outside world,” the world beyond the algorithm itself.

We’re not concerned with formatting.

Page 67: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Print

Displays output items to the user

Syntax:print(item_1, item_2, ..., item_n)

Examples:print(“Please enter your info.”)print(num_one, my_char, is_Student)

Page 68: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Read

Obtains input items from the user

Syntax:read(item_1, item_2, ..., item_n)

Examples:read(menu_choice)read(is_Student, name, age)

No automatic prompting!

Page 69: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Input and Output Examplesalgorithm IO_Example num_one, num_two, average isoftype Num

// obtain two numbers print(“Please enter two numbers”) read (num_one, num_two)

// output a literal text message and the// value of the sum print (“Sum = ”, num_one + num_two)

// output a string literal and average average <- (num_one + num_two) / 2 print (“Average = “, average)endalgorithm

Page 70: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic

Questions?

Page 71: Announcements The Algorithmic Model Programming Languages Data Assignment Arithmetic