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

## Presentation on theme: "1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer""— Presentation transcript:

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

University of Utah – School of Computing University of Utah 2 Define the Problem You are to write a program to shuffle a stack of numbered cards

University of Utah – School of Computing University of Utah 3 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

University of Utah – School of Computing University of Utah 4 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!

University of Utah – School of Computing University of Utah 5 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.

University of Utah – School of Computing University of Utah 6 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

University of Utah – School of Computing University of Utah 7 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.

University of Utah – School of Computing University of Utah 8 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

University of Utah – School of Computing University of Utah 9 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

University of Utah – School of Computing University of Utah 10 10 Example Program 1.Create Start Pile 2.Create New Pile Finish 3.Move card from Start to Finish 4.Move card from Start to Finish 5.Move card from Start to Finish 6.Move card from Start to Finish 7.Move card from Start to Finish 8.Move card from Start to Finish 9.Move card from Start to Finish 10.Move card from Start to Finish 11.Move card from Start to Finish 12.Move card from Start to Finish 13.Move card from Start to Finish 14.Move card from Start to Finish 15.Move card from Start to Finish 16.Move card from Start to Finish 17.Move card from Start to Finish 18.Move card from Start to Finish 19.Move card from Start to Finish … 54.Move card from Start to Finish

University of Utah – School of Computing University of Utah 11 11 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.

University of Utah – School of Computing University of Utah 12 12 Your Job: Shuffle Cards Before you begin: Always ASK Questions. Always CLARIFY the problem! So?

University of Utah – School of Computing University of Utah 13 13 Did you ask? What does Shuffle mean? Can we make multiple piles?

University of Utah – School of Computing University of Utah 14 14 Your Turn On paper, write out an algorithm for shuffling the stack of cards

University of Utah – School of Computing University of Utah 15 15 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!

University of Utah – School of Computing University of Utah 16 16 Possible Solution Start Up Create Start Pile with all cards Create A Pile Create B Pile Create Finish Pile

University of Utah – School of Computing University of Utah 17 17 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

University of Utah – School of Computing University of Utah 18 18 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

University of Utah – School of Computing University of Utah 19 19 Solution Getting to the Finish Move Card from A to Finish …

University of Utah – School of Computing University of Utah 20 20 Solution – B to Finish Move Card from B to Finish …

University of Utah – School of Computing University of Utah 21 21 ? Only One Solution ? Did your solution match mine? Does that mean it is incorrect? How many solutions are there?

University of Utah – School of Computing University of Utah 22 22 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)?

University of Utah – School of Computing University of Utah 23 23 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)

University of Utah – School of Computing University of Utah 24 24 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

University of Utah – School of Computing University of Utah 25 25 Vocabulary Recap Algorithm -A list of steps to accomplish a Goal Computer Program -Repeats a bunch of simple steps to accomplish a larger goal

University of Utah – School of Computing University of Utah 26 26 Whats 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 …

University of Utah – School of Computing University of Utah 27 27 What if? Create Start Pile with all cards Create Finish Pile Move Card from Start to Finsh …

University of Utah – School of Computing University of Utah 28 28 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 wont even run the program Logical Errors can take many hours to track down!

University of Utah – School of Computing University of Utah 29 29 Questions? Algorithms Repetition (loops) Bugs

University of Utah – School of Computing University of Utah 30 30 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?

University of Utah – School of Computing University of Utah 31 31 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

University of Utah – School of Computing University of Utah 32 32 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

University of Utah – School of Computing University of Utah 33 33 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!)

University of Utah – School of Computing University of Utah 34 34 Questions Discussion!

Similar presentations