1 university of utah – school of computing computer science 1021 "thinking like a...

34
1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

Upload: quinten-twiss

Post on 31-Mar-2015

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

11University of Utah – School of Computing

Computer Science 1021 "Thinking Like a Computer"

Page 2: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

22

Define the Problem

• You are to “write a program” to shuffle a stack of numbered cards

Page 3: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

33

Programming Language

• Your “Program” always starts with:- Create a pile of cards called “Start” with all

52 cards in it.

• Your “Program” can then contain any combination of:- Create a new, empty, pile called _____

(name of your choice)

- Move Card from Pile ______ to Pile ______ Where ____ is one of your piles

Page 4: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

44

Language Rephrased

This is All your program can consist of:

1. Create start pile

2. Create empty pile called __________

3. Move card from ______ to _________

But Lines 2 and 3 can occur as often as you want!

Page 5: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

55

Program Execution Rules

• Program Always Starts from Top of Page

• Program Always Proceeds one line at a time- From Top Bottom

• Every Line is completed before next line is executed.

Page 6: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

66

Algorithm

• An Algorithm is a list of steps that tell us how to accomplish a goal, e.g.,- a cooking recipe- a set of directions to get to a party- a computer program

Page 7: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

77

How Verbose?

• An Algorithm can be very high level- Go get me a pizza

• Or Very Low Level- Stand up, Turn Left, Walk 10 paces, …

Hand the clerk $6.00, Receive change, Turn around, … Hand box to instructor, Turn around, … Sit down.

Page 8: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

88

High Level vs Low Level

• High Level Programming Language:- The closer the algorithm is to English and

how you normally converse, the “Higher” the level of the program

• Computer Programming Languages:- While considered high-level, are still very

“low level” compared to what you are used to

Page 9: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

99

Example Algorithm

• Create Piles Start and Finish

• Move all cards from Start to Finish

• This is PSEUDOCODE- A shorthand notation that is easily translated

into actual CODE

Page 10: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

1010

Example Program1. Create Start Pile2. Create New Pile “Finish”3. Move card from Start to Finish4. Move card from Start to Finish5. Move card from Start to Finish6. Move card from Start to Finish7. Move card from Start to Finish8. Move card from Start to Finish9. Move card from Start to Finish10. Move card from Start to Finish11. Move card from Start to Finish12. Move card from Start to Finish13. Move card from Start to Finish14. Move card from Start to Finish15. Move card from Start to Finish16. Move card from Start to Finish17. Move card from Start to Finish18. Move card from Start to Finish19. Move card from Start to Finish

…54. Move card from Start to Finish

Page 11: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

1111

What does this Program Accomplish?

• The sample program is an example of an algorithm which changes a list of items from forward to backward (changes the order)

• In this case the “list” is a pile of cards, but just as easily we could have reversed the list of students in the class.

Page 12: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

1212

Your Job: Shuffle Cards

• Before you begin:

Always ASK Questions.

Always CLARIFY the problem!

So?

Page 13: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

1313

Did you ask?

• What does “Shuffle” mean?

• Can we make multiple piles?

Page 14: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

1414

Your Turn

• On paper, write out an algorithm for shuffling the stack of cards

Page 15: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

1515

Language Rephrased

This is all your program can consist of:

1. Create start pile

2. Create empty pile called __________

3. Move card from ______ to _________

But Lines 2 and 3 can occur as often as you want!

Page 16: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

1616

Possible SolutionStart Up

• Create Start Pile with all cards

• Create A Pile

• Create B Pile

• Create Finish Pile

Page 17: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

1717

Solution – Separate Cards

• Move Card from Start to A• Move Card from Start to B• Move Card from Start to A• Move Card from Start to B• Move Card from Start to A• Move Card from Start to B• Move Card from Start to A• Move Card from Start to B• Move Card from Start to A• Move Card from Start to B

Page 18: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

1818

Solution …

• Move Card from Start to A• Move Card from Start to B• Move Card from Start to A• Move Card from Start to B• Move Card from Start to A• Move Card from Start to B• Move Card from Start to A• Move Card from Start to B• Move Card from Start to A• Move Card from Start to B• Move Card from Start to A• Move Card from Start to B• Move Card from Start to A• Move Card from Start to B• Move Card from Start to A• Move Card from Start to B• Move Card from Start to A• Move Card from Start to B

Page 19: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

1919

Solution Getting to the Finish

• Move Card from A to Finish• Move Card from A to Finish• Move Card from A to Finish• Move Card from A to Finish• Move Card from A to Finish• Move Card from A to Finish• Move Card from A to Finish• Move Card from A to Finish• Move Card from A to Finish• Move Card from A to Finish• …

Page 20: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

2020

Solution – B to Finish

• Move Card from B to Finish• Move Card from B to Finish• Move Card from B to Finish• Move Card from B to Finish• Move Card from B to Finish• Move Card from B to Finish• Move Card from B to Finish• Move Card from B to Finish• Move Card from B to Finish• …

Page 21: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

2121

? Only One Solution ?

• Did your solution match mine?

• Does that mean it is incorrect?

• How many solutions are there?

Page 22: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

2222

LONG Programs

• Is it just me or did that program contain a huge number of lines of code to accomplish a very simple task?

• Any suggestions to make the program easier to read (more concise)?

Page 23: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

2323

Repetition

• If you are doing something over and over again, why not just say so:

• New Line of CODE- Repeat the previous _N_ lines of code _M_

times. (Note N and M can be any whole numbers)

Page 24: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

2424

Program Rewritten

• Create Start Pile with all cards• Create A Pile• Create B Pile• Create Finish Pile• Move Card from Start to A• Move Card from Start to B• Repeat the previous 2 lines 26 times• Move Card from A to Finish• Repeat the previous line 26 times• Move Card from B to Finish• Repeat the previous line 26 times

Page 25: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

2525

Vocabulary Recap

• Algorithm- A list of steps to accomplish a Goal

• Computer Program- Repeats a bunch of simple steps to

accomplish a larger goal

Page 26: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

2626

What’s wrong?

• Create Start Pile with all cards• Create A Pile• Create B Pile• Create Finish Pile• Move Card from Start to A• Move Card from Start to B• Repeat the previous 2 lines 52 times

• …

Page 27: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

2727

What if?

• Create Start Pile with all cards

• Create Finish Pile• Move Card from Start to Finsh• Move Card from Start to Finsh• Move Card from Start to Finsh• Move Card from Start to Finsh• …

Page 28: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

2828

Bugs

• The previous examples are called bugs:- Finsh was a syntax error- 52 was a logical error

• Syntax Errors are often easy to find (but not always)… Usually the computer won’t even run the program

• Logical Errors can take many hours to track down!

Page 29: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

2929

Questions?

• Algorithms• Repetition (loops)• Bugs

Page 30: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

3030

What did I really accomplish?

• Did I really shuffle the cards?

• What do else do we need to do?

• What other “Programming Constructs” do we need?

Page 31: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

3131

New Algorithm(Pseudo Code!)

• Set up Start, A, B, Finish

• For every card in the deck, move some cards from start to A and other cards to B “Randomly”

• Move all cards from A to Finish

• Move all cards from B to Finish

Page 32: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

3232

Randomizer

• Also, we get only simple commands in a program, so….

• New Command:- Randomizer()

Produces a number between 0 and 1 (e.g., 0.01, 0.95, 0.3432, 0.123, 0.0001, 0.99999)

- Note the () is just syntactic sugar telling us that “Randomizer” does something

Page 33: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

3333

If Statement

• If ( randomizer() produces a number > .5 )- Move Card from Start to A

• Otherwise- Move Card from Start to B

• End If Statement

(Note: this is considered a single statement!)

Page 34: 1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"

University of Utah – School of Computing

University

of Utah

3434

Questions

• Discussion!