; Addition in Other Bases ; written by Teresa Carrigan, 2004 globals [ start-x praise digits myDigits save-base column doneyet?] breeds [ arrow digit plus] arrow-own [ carry state ] digit-own [ digit-value state] patches-own [ name ] ; runs setup when program is first loaded to startup setup end ; initializes number-of-digits and base, then calls setup to random-setup set number-of-digits (2 + random 9) set base (2 + random 15) setup end ; makes the correct number of digits, and the red arrow ; initializes variables to setup locals [ here-x here-y 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 here-y 5 set column here-x set doneyet? false set current 1 ; for each digit repeat number-of-digits [ cct-digit 1 [ set color black set shape "box" set size 2 setxy here-x here-y set digit-value random base if (number-of-digits - current) < 2 and random 100 < 50 [ set digit-value 0 ] set label (item digit-value myDigits) + " " set label-color white set state "ready" ] cct-digit 1 [ set color yellow set shape "bottom" set size 2 setxy here-x (here-y - 2) set digit-value random base if (number-of-digits - current) < 3 and random 100 < 50 [ set digit-value 0 ] set label (item digit-value myDigits) + " " set label-color white set state "ready" ] cct-digit 1 [ set size 2 setxy here-x (here-y - 4) set label "" set color black set label-color cyan set state "empty" ] set current (current + 1) set here-x (here-x - 1) ] ; create red arrow cct-arrow 1 [ setxy start-x here-y set heading -90 set color red set shape "arrow" set label-color white set carry 0 hideturtle ] set here-y (here-y - 2) set here-x xcor-of min-one-of digit [ xcor ] set here-x (here-x - 2) ; create the plus sign cct-plus 1 [ setxy here-x here-y set color yellow set shape "plus" set size .75 ] ; create explanation bar at the bottom ask patches with [ pycor < -4] [ set pcolor blue ] ask patch-at 6 -5 [ set plabel "Addition in Base " + base set plabel-color white set name 1 ] ask patch-at 6 -6 [ set plabel "" set name 2 ] ask patch-at 6 -7 [ set plabel "" set name 3 ] end to go ifelse not doneyet? [ step ] [ stop ] end to show-again locals [ here-y ] set doneyet? false ask patches [ set plabel "" ] explain 1 (word "Addition in Base " base) ask min-one-of digit [ ycor ] [ set here-y ycor] ask max-one-of digit [ xcor ] [ set column xcor ] ask digit [ set state "ready" ] ask digit with [ ycor = here-y ] [ set label "" set color black set label-color cyan set state "empty" ] ask arrow [ setxy start-x (here-y + 5) set heading -90 set carry 0 set state 0 hideturtle ] end to explain [ which msg ] ask patches with [ name = which ] [ set plabel msg ] end ; process the next column of digits to step locals [ new-digit problem readyList row] set readyList (digit with [ state = "ready" ]) ifelse count readyList > 0 [ set column xcor-of max-one-of readyList [ xcor ] ask arrow [ explain 2 (word "Carry-in = " carry) setxy column 6 set heading 180 showturtle wait slow-motion if carry > 0 [ wait slow-motion ] set base save-base fd 1 wait slow-motion ; top digit this column ask other-digit-here [ set new-digit label set state "done" ] set new-digit remove " " new-digit set new-digit position new-digit myDigits explain 2 (word carry " + " new-digit " + ?") set problem new-digit fd 1 wait slow-motion fd 1 wait slow-motion ; second digit this column ask other-digit-here [ set new-digit label set state "done" ] set new-digit remove " " new-digit set new-digit position new-digit myDigits explain 2 (word carry " + " problem " + " new-digit) set problem (problem + new-digit + carry) set carry 0 fd 1 wait slow-motion fd 1 ; calculate sum and carry set label "" if problem >= base [ explain 2 (word problem " >= " base ", so subtract " base) wait slow-motion set problem problem - base set carry 1 ] ask other-digit-here [ set label (item problem myDigits) + " " set state "done" ] if carry > 0 [ set label (word carry " ") ] fd 1 wait slow-motion ifelse problem < 10 [ explain 2 (word "Carry = " carry "; Sum = " problem) ] [ explain 2 (word "Carry = " carry "; Sum = " problem " = " (item problem digits)) ] fd 1 set new-digit carry ] ] [ ; finished all columns wait slow-motion if not doneyet? [ set doneyet? true ask arrow [ set new-digit carry setxy (xcor - 1) 6 set state "resting" ] ifelse new-digit > 0 [ explain 2 (word "Carry = " new-digit ". Overflow!") ask patch-at 4 0 [ set plabel "Overflow ERROR " set plabel-color red ] ask min-one-of digit [ ycor ] [ set row ycor] ask digit with [ ycor = row ] [ set label-color red ] ] [ explain 2 (word "Carry = " new-digit ". No overflow.") ask patch-at 4 0 [ set plabel "Answer " set plabel-color cyan ] ] ] ] end ; asks a quiz question to ask-add locals [ guess target ] set guess user-input (word "What is the answer to the base " base " problem shown?") set guess (word " " guess " ") set guess clean-input guess while [ not doneyet? ] [ go ] without-interruption [ set target get-number ] ifelse guess = target [ user-message random-one-of praise ] [ set target add-commas target user-message "I'm sorry, but the correct answer is " + target] end to-report pad-input [guess] while [ length guess < number-of-digits ] [ set guess (word "0" guess) ] report guess end ; cleans up the user input 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 pad-input guess end ; read the digits of the answer, storing it as a string to-report get-number locals [ target row answerList current] set target "" set answerList digit with [ label-color = cyan or label-color = red] while [ count answerList > 0 ] [ set current min-one-of answerList [ xcor ] set target (word target label-of current) set answerList answerList with [ xcor > (xcor-of current) ] ] set target remove " " target report 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 - calls ask-add to quiz locals [qt flag save-slow] set save-slow slow-motion set slow-motion 0 wait save-slow ask arrow [ ifelse (state = "resting") or (hidden? = true) [ set flag true ] [ set flag false ] ] ifelse flag [ ifelse random-quiz? [ without-interruption [random-setup ] ] [ without-interruption [setup ] ] wait .5 ask-add ] [ user-message "No quiz until arrow finishes." ] set slow-motion save-slow 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). Addition in Other Bases 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 416 7 7 25.0 1 22 1 1 1 CC-WINDOW 393 296 605 357 Command Center BUTTON 416 42 503 75 NIL setup NIL 1 T OBSERVER T BUTTON 417 75 503 108 NIL step NIL 1 T OBSERVER T SLIDER 417 188 589 221 number-of-digits number-of-digits 2 10 5 1 1 digits BUTTON 503 75 587 108 go go T 1 T OBSERVER T SLIDER 417 153 590 186 slow-motion slow-motion 0 1 0.05 0.05 1 seconds BUTTON 504 109 587 142 quiz quiz NIL 1 T OBSERVER T SLIDER 418 221 590 254 base base 2 16 2 1 1 NIL BUTTON 504 42 588 75 random random-setup NIL 1 T OBSERVER T SWITCH 418 253 591 286 random-quiz? random-quiz? 0 1 -1000 BUTTON 417 108 503 141 NIL show-again NIL 1 T OBSERVER T @#$#@#$#@ WHAT IS IT? ----------- This model demonstrates addition in any base from two to sixteen. It also illustrates the concept of overflow. HOW IT WORKS ------------ The arrow adds one column of digits at a time, starting with the right-most column. If the sum is too large to be a digit in that base, then the base is subtracted from the sum and the arrow carries a one to the next column. If the arrow holds a carry when all the columns are processed, then overflow occurs (assuming the number of digits is fixed). HOW TO USE IT ------------- The setup button generates a random addition problem. The base used is determined by the base slider, and then maximum number of digits for each number is determined by the number-of-digits slider. The number-of-digits slider sets the number of digits that the count will hold the next time the setup button is pressed. Possible values range from two to ten. The base slider sets the base to be used the next time the setup button is pressed. You may choose any base from two to sixteen. The random button changes the values of the base and number-of-digits sliders and then sets up a new addition problem. The slow-motion slider is an easy way to adjust the speed of the display so you can watch the digits change as the red arrow passes. Set it to zero if you want to just see the answer quickly. The step button adds the next column of digits, and then stops so you can take notes. The go button processes the remaining columns of digits, at a speed set 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. If the random-quiz? switch is on, then quiz questions can come from any base and any number of digits. If it is off, then quiz questions are generated from the base and number of digits specified by the sliders. The quiz button will generate a random addition problem and ask you for the answer. For bases larger than ten, you do not need to capitalize digits that are letters. You are not required to pad leading zeroes to the correct number of digits, but do not include any overflow digit. Spaces and commas in your answer will be ignored, so use them freely to avoid copy errors. 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 ---------------- If there is a carry out of the left-most column, an overflow error occurs. This is because computers reserve a fixed amount of space for each integer. In working problems by hand, we can always add another digit to the left. THINGS TO TRY ------------- Set the slow-motion to about .50 seconds (or slower) and press the step button a few times. Watch the red arrow as it processes each row. Press setup or random until you see a problem with large digits in the left-most column. Press go to see if an overflow error is detected. 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. Modify the model to allow addition of more than two numbers. Allow the user to input the two numbers to be added. Display the decimal equivalent of the two addends and the sum. NETLOGO FEATURES ---------------- "ask other-digit-here" is used by the arrow to process the digit it is passing over. RELATED MODELS -------------- Counting in Other Number Bases 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). Addition in Other Bases 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 about addition in other number bases and overflow detection, see one of the following textbooks: [1] Null, L. and Lobur, J. "Essentials of Computer Organization and Architecture", First Edition, Jones & Bartlett, pages 45-46; 53. [2] Dale, N. & Lewis, J. "Computer Science Illuminated" Second Edition, Jones and Bartlett, pages 39-40; 62-63. @#$#@#$#@ 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 bottom false 0 Rectangle -7566196 true true 0 270 298 297 box true 0 Polygon -7566196 true true 45 255 255 255 255 45 45 45 plus false 0 Rectangle -7566196 true true 138 17 161 284 Rectangle -7566196 true true 14 135 283 163 thin-arrow true 0 Polygon -7566196 true true 150 0 0 150 120 150 120 293 180 293 180 150 300 150 @#$#@#$#@ NetLogo 2.0.1 @#$#@#$#@ @#$#@#$#@ @#$#@#$#@