; Place Value Method Simulation ; written by Teresa Carrigan, 2004 globals [ start-x eq-unsign praise digits myDigits save-base step] breeds [ digit place product decimal power] digit-own [ place-value current-value digit-value] place-own [ value ] product-own [ value place-value ] ; runs setup when program is first loaded to startup setup end ; makes the correct number of digits, and the red arrow ; initializes variables to setup locals [ here-x current n] ca set praise [ "You got it!" "Right!" "Correct" "Awesome!" "Perfect!" ] set digits [ "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F" ] set save-base base set myDigits [] set n 0 repeat base [ set myDigits lput (item n digits ) myDigits set n (n + 1) ] set start-x 7 set here-x 4 set current 1 set n 0 set eq-unsign 0 ; for each digit make four turtles: power, place, digit, and product repeat number-of-digits [ cct-digit 1 [ setxy here-x 0 set shape "circle" set color white set size 2 set label-color black set place-value current set digit-value random base set label (word (item digit-value myDigits) " ") set current-value (digit-value * place-value) ] cct-place 1 [ hideturtle setxy here-x 2 ; set size 2 set color black set label-color yellow set label current set value current ] cct-power 1 [ hideturtle setxy here-x 4 ; set size 2 set color black set label-color red set label n set n (n + 1) ] cct-product 1 [ hideturtle setxy here-x -2 ; set size 2 set color black set label-color cyan set value current-value-of random-one-of digit-at 0 2 set label value set place-value current-value-of random-one-of digit-at 0 2 ] set here-x (here-x - 2) set current (current * base) ] ; create explanation bar at the bottom ask patches with [ pycor < -3 ] [ set pcolor blue ] ask patch-at 6 -4 [ set plabel "Converting from base " + base + " to decimal" set plabel-color white ] ask patch-at 6 -5 [ set plabel "using the Place Value method." ] set step 1 end ; randomize settings and start over to setup-random set number-of-digits 2 + (random 4) set base 2 + (random 15) setup end ; decimal equivalent of digit pattern to-report calc set eq-unsign (sum values-from digit [ current-value ]) wait .5 report eq-unsign end ; show the powers of each digit to step1 locals [ n ] ask patch-at 6 -4 [ set plabel "Mark each digit with its power of " + base + "." ] ask patch-at 6 -5 [ set plabel "Just start counting from zero, at the left." ] wait slow-motion set n 0 ask patch-at 6 5 [ set plabel "Power of " + base set plabel-color red ] repeat number-of-digits [ ask power with [ label = n ] [ showturtle] wait slow-motion set n (n + 1) ] end ; show the place value of each digit to step2 locals [ n ] ask patch-at 6 -4 [ set plabel "Calculate " + base + " to that power." ] ask patch-at 6 -5 [ set plabel "This is the PLACE VALUE." ] wait slow-motion set n 1 ask patch-at 6 3 [ set plabel "Place value" set plabel-color yellow ] repeat number-of-digits [ ask place with [ label = n ] [ showturtle] wait slow-motion set n (n * base) ] end ; show the current value (product) of each digit to step3 locals [ n ] ask patch-at 6 -4 [ set plabel "Multiply each digit" ] ask patch-at 6 -5 [ set plabel "by its place value." ] wait slow-motion set n 4 ask patch-at 6 -1 [ set plabel "Product" set plabel-color cyan ] repeat number-of-digits [ ask product-at n -2 [ showturtle] wait slow-motion set n (n - 2) ] end ; add all the products to step4 locals [ n collect] ask patch-at 6 -4 [ set plabel "Add all the products." ] get-expression wait 1 + slow-motion ask patch-at 6 -5 [ set eq-unsign sum values-from digit [ current-value ] set eq-unsign (word "" eq-unsign) set eq-unsign add-commas eq-unsign set plabel "Sum = " + eq-unsign ] end ; do whatever step comes next, then wait until user wants to continue to one-step locals [ which ] if step < 5 [ set which (word "step" step) run which ] set step (step + 1) end ; show all remaining steps to go ifelse step < 5 [ one-step wait slow-motion ] [ stop ] end to show-again ask power [hideturtle] ask place [hideturtle] ask product [hideturtle] ask patches [ set plabel "" ] set step 1 end ; asks the user to convert a number from a base to decimal to ask-other locals [ guess target question ] without-interruption [ set question get-number ] set question add-commas question set guess user-input (word "Convert the base " base " number " question " to decimal.") set guess clean-input guess set guess read-from-string guess set target calc ifelse guess = target [ user-message random-one-of praise ] [ user-message "I'm sorry, but the correct answer is " + target] end to-report clean-input [guess] locals [ pos k upper lowerList] set guess remove " " guess set guess remove "," guess set lowerList ["a" "b" "c" "d" "e" "f"] foreach lowerList [ while [member? ? guess] [ set pos position ? guess set k position ? lowerList set upper (item (k + 10) digits) set upper word upper "" set guess replace-item pos guess upper ] ] report guess end ; read the digits, storing it as a string to-report get-number locals [ target n num] set target "" set n 4 repeat number-of-digits [ set num label-of random-one-of digit-at n 0 set target (word num target) set n (n - 2) ] set target remove " " target report target end ; read the products, adjusting the label of the patch at the bottom to get-expression locals [ target n num] set target "" set n 4 repeat number-of-digits [ set num label-of random-one-of product-at n -2 set target (word " + " num target) set n (n - 2) ask patch-at 6 -5 [ set plabel " ... " + target ] wait slow-motion ] set n position "+" target set num length target set target substring target (n + 1) num ask patch-at 6 -5 [ set plabel "Sum = " + target ] end ; add commas every three digits, so the user won't make copy errors to-report add-commas [ number ] locals [ save k ] set save "" set k 0 while [ (length number) > 0 ] [ set save (word last number save ) set number butlast number set k (k + 1) if (k = 3) and (length number > 0) [ set save (word "," save ) set k 0 ] ] set number save report number end ; ask a quiz question to quiz without-interruption [setup ] wait .5 ask-other end ; *** NetLogo Model Copyright Notice *** ; ; Copyright 2004 by Teresa W. Carrigan. All rights reserved. ; ; Permission to use, modify or redistribute this model is hereby granted, ; provided that both of the following requirements are followed: ; a) this copyright notice is included. ; b) this model will not be redistributed for profit without permission ; from Teresa W. Carrigan. ; Contact Teresa W. Carrigan for appropriate licenses for redistribution ; for profit. ; ; To refer to this model in academic publications, please use: ; Carrigan, T. (2004). Place Value Method Simulation model. ; Blackburn College, Carlinville IL. ; ; In other publications, please use: ; Copyright 2004 by Teresa W. Carrigan. All rights reserved. ; ; *** End of NetLogo Model Copyright Notice *** @#$#@#$#@ GRAPHICS-WINDOW 3 10 388 316 7 5 25.0 1 18 1 1 1 CC-WINDOW 413 267 600 328 Command Center BUTTON 417 41 500 74 NIL setup NIL 1 T OBSERVER T SLIDER 418 201 590 234 number-of-digits number-of-digits 2 5 3 1 1 digits SLIDER 418 166 591 199 slow-motion slow-motion 0 1 0.5 0.1 1 seconds BUTTON 502 108 590 141 quiz quiz NIL 1 T OBSERVER T SLIDER 419 234 591 267 base base 2 16 8 1 1 NIL BUTTON 417 75 499 108 step one-step NIL 1 T OBSERVER T BUTTON 501 40 590 73 random setup-random NIL 1 T OBSERVER T BUTTON 501 74 590 107 go go T 1 T OBSERVER T BUTTON 417 108 502 141 NIL show-again NIL 1 T OBSERVER T @#$#@#$#@ WHAT IS IT? ----------- This model demonstrates the Place Value method of converting from another base to decimal. You may choose any base in the range of two to sixteen. HOW IT WORKS ------------ The Place Value method first determines the powers by counting the digits starting with zero on the right. Next, the place value of each digit is determined. The place value is the base to the power determined in the first step. The third step is to multiply each digit by its place-value. The last step is to add all the products calculated in the third step. This gives us the decimal equivalent of an unsigned representation in another base. HOW TO USE IT ------------- If you want examples from a specific base: Use the number-of-digits slider to set the number of digits that the counter can hold. Use the base slider to set the base. Now press the setup button. This will generate a random number with that many digits in that base (although it is possible to have leading zeroes). If you do not care which base is used, click the random button to generate an arbitrary base, number-of-digits, and number. The slow-motion slider is an easy way to adjust the speed of the display. Set it to zero if you want to show the final result as quickly as possible. 0.5 is a good setting for most purposes. The step button demonstrates the next step of the place value method, and then stops so you can take notes. This is useful when you are first learning the method. The go button does all remaining steps, at a speed determined by the slow-motion slider. This is useful when you do not need to take notes between each step, or do not wish to press the step button several times to get an answer. If you want to pause the demonstration, simply click the go button a second time and it will stop after it finishes the current step. You may then click go a third time to resume. The quiz button will generate a random number using the base and number-of-digits sliders, and then ask you to convert it to decimal. If you want to drill conversion from binary, set the base to 2 and the number-of-digits to the maximum. If you want to drill conversion from hexadecimal, set the base to 16, and the number-of-digits to a small number unless you have a calculator and a lot of patience. The show-again button starts the exact problem from the beginning. You may then click either the step button or the go button to see the same demonstration. THINGS TO NOTICE ---------------- When the base is 2 (binary) the digits are only 0 and 1. In this case, you don't really need the third step (multiplying); just add the place values for the digits that are 1, dropping the digits that are 0. When the base is smaller than ten, the decimal equivalent will have at most the same number of digits as the original number, and usually it will have fewer digits. When the base is larger than ten, the decimal equivalent will have at least the same number of digits as the original number, and usually it will have more digits. THINGS TO TRY ------------- Set slow-motion to 0.5, click random, and then click go. Set the sliders to a problem type you want to drill, then click setup. Attempt one step at a time on paper, and then click the step button to check that you did that step correctly. EXTENDING THE MODEL ------------------- Modify the model to show fixed point representation; that is, specify a given number of digits to the right of the decimal place. Allow the user to input a starting digit pattern. Allow the user to input a decimal number, and then display the corresponding digit pattern. NETLOGO FEATURES ---------------- Extensive use is made of "patch-at" and "BREED-at". It was awkward when it should be impossible to have more than one turtle at a given location to be required to say "random-one-of" in order to return a single turtle instead of an agent set. RELATED MODELS -------------- Binary Counter, Counting in Other Number Bases, Horner's Method CREDITS AND REFERENCES ---------------------- This model was written by Teresa W. Carrigan, 2004. Permission to use, modify or redistribute this model is hereby granted, provided that both of the following requirements are followed: a) this copyright notice is included. b) this model will not be redistributed for profit without permission from Teresa W. Carrigan. Contact Teresa W. Carrigan for appropriate licenses for redistribution for profit. To refer to this model in academic publications, please use: Carrigan, T. (2004). Place Value Method Simulation model. Blackburn College, Carlinville, IL. In other publications, please use: Copyright 2004 by Teresa W. Carrigan. All rights reserved. FOR MORE INFORMATION -------------------- For more information converting from another number base to decimal, see one of these textbooks: [1] Null, L. and Lobur, J. "Essentials of Computer Organization and Architecture", First Edition, Jones & Bartlett, page 38. [2] Dale, N. and Lewis, J. "Computer Science Illuminated" Second Edition, Jones and Bartlett, pages 35-39. @#$#@#$#@ default true 0 Polygon -7566196 true true 150 5 40 250 150 205 260 250 arrow true 0 Polygon -7566196 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150 box true 0 Polygon -7566196 true true 45 255 255 255 255 45 45 45 circle false 0 Circle -7566196 true true 35 35 230 @#$#@#$#@ NetLogo 2.0.1 @#$#@#$#@ @#$#@#$#@ @#$#@#$#@