Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 11 DO NOW QUESTIONS. An ask turtles block is a set of instructions that is issued to every turtle. Even though computers can do things very quickly,

Similar presentations


Presentation on theme: "Week 11 DO NOW QUESTIONS. An ask turtles block is a set of instructions that is issued to every turtle. Even though computers can do things very quickly,"— Presentation transcript:

1 Week 11 DO NOW QUESTIONS

2 An ask turtles block is a set of instructions that is issued to every turtle. Even though computers can do things very quickly, some problems are still too big to handle. It is important to be able to have a rough idea of how many operations your code is going to execute when a block runs. Look at the two ask turtle blocks below and determine which one does more “work”. Both block call the procedure “wiggle”. Both procedures are only called once (no forever buttons). to wiggle right random 15 left random 16 forward 1 end A. to test-A Create-turtles 100 Ask turtles [ setxy random 16 random 16 Pen-down wiggle set pcolor green pen-up ] end B. to test-B Create-turtles 50 [ ask turtles [ setxy random 16 random 16 set shape “ant” set color red ask n-of 10 other turtles [wiggle] ] end a)Block A runs more operations because there are more turtles. b)Block A does more “work” because it moves the pen up and down. c)Block B executes many more wiggle operations than Block A. It would probably take longer to run. d)Block B does more work because it sets a shape. Graphics use lots of memory.

3 Assignment to a variable You are trying to understand a NetLogo program written by another programmer. You come across the following line of code Set x = 3 What does it do? a)Sets x equal to the value 3 b)It causes an error. The programmer probably meant: set x 3. c) Creates a variable x and tests if it is equal to three d) Creates an agentset with 3 members. e) Both a and c

4 Search Efficiency A magician shows you a deck of 52 cards arranged in numerical order by suit. He picks a card and writes it down on a paper. You get 6 yes and no questions to find the card. Do your think that you can identify the card that the magician picked? a)Sure, just use a binary search. Start with “Is it in the top 26 cards?” b)Yes, first narrow it down to which suite (Hearts, Clubs, Diamonds, Spades). c)No, you may get lucky but it is possible that it would take more guesses. d)Magicians do not play fair, you will not identify the card. e)A and D

5 Evaluation of procedures You have code that creates a list, L and then runs several commands at once on that list. Make sure that you start with the innermost command and work your way out. The code shows the results in the command center window with the “show” command. let L [ “a” “b” “c” “d”] show fput “a” reverse reverse butfirst L a)The butfirst command removes the “a” from [ “a” “b” “c” “d”] b)The 2 nd Reverse requires one list as input, in this case it gets [“b” “c” “d”]. c)fput adds an “a” on the result of Reverse Reverse butfirst L d)All of the above are true.

6 Order of Execution The show or type commands are very useful debugging tools. They allow the programmer to peek at the values the of variables or attributes. If we use a couple of type commands as below what will be the output? to test create-turtles 50 ;a random agentset of 3 turtles ask n-of 3 turtles set color red repeat 2 [type " Ha "] type xcor die ] end a)Ha Ha 0 Ha Ha 0 Ha Ha 0 b)Fifty 0’s and 50 Ha Ha’s c)Ha Ha xcor d)Nothing will be printed because the turtles all die.

7 Getting ready for Recursion-- Here is a simple reporter that adds two numbers. It has two input parameters so you need to pass it two arguments when you call it. It returns a single number. Remember, when you write a reporter, you put the parameter(s) in square brackets. When you call or use it, you just pass an argument of the right type (string, number value, or list.) to-report adds-two-numbers [p q] report p + q end Now, lets call this reporter like this: show adds-two-numbers adds-two-numbers 1 2 adds-two-numbers 4 5 What does this code do? a)It won’t work because adds-two-numbers needs two arguments. b)It gives an error because you forgot the square brackets. c)The 1 and the 4 get added and the 2 and the 5 get added. d)The first adds-two-numbers gets its input only after the other adds-two- numbers reporters run. This code sums the four numbers.


Download ppt "Week 11 DO NOW QUESTIONS. An ask turtles block is a set of instructions that is issued to every turtle. Even though computers can do things very quickly,"

Similar presentations


Ads by Google