1 / 7

Week 11 DO NOW QUESTIONS

Week 11 DO NOW QUESTIONS.

afra
Download Presentation

Week 11 DO NOW QUESTIONS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 • Block A runs more operations because there are more turtles. • Block A does more “work” because it moves the pen up and down. • Block B executes many more wiggle operations than Block A. It would probably take longer to run. • 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? • Sets x equal to the value 3 • It causes an error. The programmer probably meant: set x 3. • Creates a variable x and tests if it is equal to three • Creates an agentset with 3 members. • 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? • Sure, just use a binary search. Start with “Is it in the top 26 cards?” • Yes, first narrow it down to which suite (Hearts, Clubs, Diamonds, Spades). • No, you may get lucky but it is possible that it would take more guesses. • Magicians do not play fair, you will not identify the card. • 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 • Thebutfirst command removes the “a” from [ “a” “b” “c” “d”] • The 2nd Reverse requires one list as input, in this case it gets [“b” “c” “d”]. • fputadds an “a” on the result of Reverse Reverse butfirst L • 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 • Ha Ha 0 Ha Ha 0 Ha Ha 0 • Fifty 0’s and 50 Ha Ha’s • Ha Ha xcor • 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? • It won’t work because adds-two-numbers needs two arguments. • It gives an error because you forgot the square brackets. • The 1 and the 4 get added and the 2 and the 5 get added. • The first adds-two-numbers gets its input only after the other adds-two-numbers reporters run. This code sums the four numbers.

More Related