Upload
buihanh
View
222
Download
2
Embed Size (px)
Citation preview
ì BuilduponskillsfromtheVariableslessoninIntermediate
ì Learnhowtoread/writetoarrays
ì LearnabouttheArrayOperationsblock
ì Learntousetheloopcountinaloop
ì Prerequisites:DataWires,Loops,Variables
©2016EV3Lessons.com,Lastedit7/19/2016
LessonObjectives
1. Simplifyprogramsbystoringmultiplerelatedvaluesinasinglevariable
2. Canbeusedwithloopstomakecompactandusefulprograms
3. Areusefulformakingacustomcalibrationprogram(seeNXTLightSensorinEV3onourcontributedlessonstab)
©2016EV3Lessons.com,Lastedit7/19/2016
WhyUseArrays?
ì Whatisanarray?ì Anarrayisavariablethatholdsmultiplevalues
ì Therearetwotypesofarrays:ì NumericArray(Holdsasetofnumbers…1,2,3,10,55)ì LogicArray(Holdsasetoflogic…True,True,False)
ì TheycanbeusedaseitherInputsorOutputssoyoucaneither….ì Write– putavalue(s)intothearrayì Read– getthevalue(s)fromthearrayout
©2016EV3Lessons.com,Lastedit7/19/2016
Arrays
ChangetoArraymode
ArrayBlocks:QuickGuide
©2016EV3Lessons.com,Lastedit7/19/2016
Write (Inputs) have 2 bumps up
Read (Outputs) have 2 bumps down
IdentifyifthevariablesareInputs/OutputsandiftheyareNumeric/ Logic
Write numeric array
Read numeric array
Write logic array
Read logic array
Logic Array
Numeric Array
Clickaddvariable
Modes
Naming
Key
Quiz
ì Eachvalueinanarrayisassignedanindex
ì Thefirstvaluewouldbeatindex0
ì LogicarrayswouldstoreTrue/Falseinsteadofnumbers
ì Toaddavaluetoanarrayclicktheplus+ì Thisaddsanentryatthe
nextindexvalue(i.e.index3)
©2016EV3Lessons.com,Lastedit7/19/2016
ArrayIndexes
Thesearevaluesforindex0,1,2
ì ThisblockisusedtoreadorwritetoLogicorNumericarrays
ì Differentmodes:ì Append:Addanewentryafterthelast
arrayindexì Readatindex:Readsthevalueatacertain
indexì WriteatIndex:Writeanewvaluetoa
certainarrayindexì Length:Howmanyentriesareinthearray
ì Bothwriteandappendoutputanarrayàyouwillneedtowritethisarraybacktothevariableifyouwishtoupdatethestoredarray(seewrite/appendslides)
©2016EV3Lessons.com,Lastedit7/19/2016
Block:ArrayOperations
©2016EV3Lessons.com,Lastedit7/19/2016
HowdoyouuseArrays(Reading)?
Readindex1inthearrays
Use“readatindex”mode
Arrayoperationblock
Displaythevalueonthescreen
Abovecodewilldisplay10Belowcodewilldisplay0forfalse
©2016EV3Lessons.com,Lastedit7/19/2016
HowdoyouuseArrays(Writing)?
Readthearrayyouwanttowriteto
Usearrayoperationstowriteavaluetoacertainindex
Writetheoutputbacktothearray
Thiswillwrite700toarrayatindex4
ThiswillwriteFalsetoarrayatindex4
ì Theloopcountoutputstheamountoftimestheblocksinsidetheloophaveplayed.
ì Thisisusefultocreateaprogramthatrunsdifferentcodeeverytimeitgoesintheloop
ì Itisalsousefulforcomputingoneachitemofanarray
©2016EV3Lessons.com,Lastedit7/19/2016
BlockSetting:LoopCount
Loopcount
Displaystheloopcounttothescreen
Note:Appendvs.Write
ì Appendaddsentriestotheendofanarray(i.e.createsanewindexvalue)
ì Thiscodeproducesanarraywith8entries(three0’sfollowedby5lightreadings)
ì Writeoverwritestheentryatthechosenindex
ì
ì Thiscodeproducesanarraywith5entries(just5lightreadings)
ì Makeaprogramthatdisplaysalltheentriesofanarray.Displayeachindexonadifferentline.Youcanuseonlyonedisplayblock.
ì Tips:Youwillneedtouseloops,loopcount,arrayblock,arrayoperations
©2016EV3Lessons.com,Lastedit7/19/2016
Challenge1
©2016EV3Lessons.com,Lastedit7/19/2016
Challenge1Solution
Create/Writethedisplayarray
Readhowmanyvaluesareinthearray
Usearrayoperationstoreadeachindexfortheloopcount
Displaythevalueonadifferentlineforeachloopcount
Waituntilyoubumpthebutton toexit
Endafteralltheindexeshavebeendisplayed
ì Makeaprogramthataddsupalltheentriesofanarray.Displaythesum.
ì Tips:Youwillneedtouseloops,loopcount,arrayblock,arrayoperations
©2016EV3Lessons.com,Lastedit7/19/2016
Challenge2
©2016EV3Lessons.com,Lastedit7/19/2016
Challenge2Solution
Readhowmanyvaluesareinthearray
Readtheindexbasedontheloopcount
Addthearrayvaluetothesumofthepastvalues Displaytothescreen
Createthedisplayarray
ì Herearesomefunthingstotry:1. Makeaprogramtocomputetheaveragevalueinanarray2. Makeaprogramthatalwayssavesthelast4lightsensorreadings
inanarray3. Createanarraythatstorescalibrationvaluesforeachsensorport
©2016EV3Lessons.com,Lastedit7/19/2016
NextSteps