arrays - ev3 lessons · ì what is an array? ì an array is a variable that holds multiple values...

17
ADVANCED EV3 PROGRAMMING LESSON ADVANCED EV3 PROGRAMMING LESSON Arrays By Sanjay and Arvind Seshan

Upload: others

Post on 24-Jul-2020

22 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Arrays - EV3 Lessons · ì What is an array? ì An array is a variable that holds multiple values ì There are two types of arrays: ì Numeric Array (Holds a set of numbers … 1,2,3,10,55)

ADVANCEDEV3PROGRAMMINGLESSONADVANCEDEV3PROGRAMMINGLESSON

ArraysBySanjayandArvindSeshan

Page 2: Arrays - EV3 Lessons · ì What is an array? ì An array is a variable that holds multiple values ì There are two types of arrays: ì Numeric Array (Holds a set of numbers … 1,2,3,10,55)

ì BuilduponskillsfromtheVariableslessoninIntermediate

ì Learnhowtoread/writetoarrays

ì LearnabouttheArrayOperationsblock

ì Learntousetheloopcountinaloop

ì Prerequisites:DataWires,Loops,Variables

©2016EV3Lessons.com,Lastedit7/19/2016

LessonObjectives

Page 3: Arrays - EV3 Lessons · ì What is an array? ì An array is a variable that holds multiple values ì There are two types of arrays: ì Numeric Array (Holds a set of numbers … 1,2,3,10,55)

1. Simplifyprogramsbystoringmultiplerelatedvaluesinasinglevariable

2. Canbeusedwithloopstomakecompactandusefulprograms

3. Areusefulformakingacustomcalibrationprogram(seeNXTLightSensorinEV3onourcontributedlessonstab)

©2016EV3Lessons.com,Lastedit7/19/2016

WhyUseArrays?

Page 4: Arrays - EV3 Lessons · ì What is an array? ì An array is a variable that holds multiple values ì There are two types of arrays: ì Numeric Array (Holds a set of numbers … 1,2,3,10,55)

ì 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

Page 5: Arrays - EV3 Lessons · ì What is an array? ì An array is a variable that holds multiple values ì There are two types of arrays: ì Numeric Array (Holds a set of numbers … 1,2,3,10,55)

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

Page 6: Arrays - EV3 Lessons · ì What is an array? ì An array is a variable that holds multiple values ì There are two types of arrays: ì Numeric Array (Holds a set of numbers … 1,2,3,10,55)

ì Eachvalueinanarrayisassignedanindex

ì Thefirstvaluewouldbeatindex0

ì LogicarrayswouldstoreTrue/Falseinsteadofnumbers

ì Toaddavaluetoanarrayclicktheplus+ì Thisaddsanentryatthe

nextindexvalue(i.e.index3)

©2016EV3Lessons.com,Lastedit7/19/2016

ArrayIndexes

Thesearevaluesforindex0,1,2

Page 7: Arrays - EV3 Lessons · ì What is an array? ì An array is a variable that holds multiple values ì There are two types of arrays: ì Numeric Array (Holds a set of numbers … 1,2,3,10,55)

ì ThisblockisusedtoreadorwritetoLogicorNumericarrays

ì Differentmodes:ì Append:Addanewentryafterthelast

arrayindexì Readatindex:Readsthevalueatacertain

indexì WriteatIndex:Writeanewvaluetoa

certainarrayindexì Length:Howmanyentriesareinthearray

ì Bothwriteandappendoutputanarrayàyouwillneedtowritethisarraybacktothevariableifyouwishtoupdatethestoredarray(seewrite/appendslides)

©2016EV3Lessons.com,Lastedit7/19/2016

Block:ArrayOperations

Page 8: Arrays - EV3 Lessons · ì What is an array? ì An array is a variable that holds multiple values ì There are two types of arrays: ì Numeric Array (Holds a set of numbers … 1,2,3,10,55)

©2016EV3Lessons.com,Lastedit7/19/2016

HowdoyouuseArrays(Reading)?

Readindex1inthearrays

Use“readatindex”mode

Arrayoperationblock

Displaythevalueonthescreen

Abovecodewilldisplay10Belowcodewilldisplay0forfalse

Page 9: Arrays - EV3 Lessons · ì What is an array? ì An array is a variable that holds multiple values ì There are two types of arrays: ì Numeric Array (Holds a set of numbers … 1,2,3,10,55)

©2016EV3Lessons.com,Lastedit7/19/2016

HowdoyouuseArrays(Writing)?

Readthearrayyouwanttowriteto

Usearrayoperationstowriteavaluetoacertainindex

Writetheoutputbacktothearray

Thiswillwrite700toarrayatindex4

ThiswillwriteFalsetoarrayatindex4

Page 10: Arrays - EV3 Lessons · ì What is an array? ì An array is a variable that holds multiple values ì There are two types of arrays: ì Numeric Array (Holds a set of numbers … 1,2,3,10,55)

ì Theloopcountoutputstheamountoftimestheblocksinsidetheloophaveplayed.

ì Thisisusefultocreateaprogramthatrunsdifferentcodeeverytimeitgoesintheloop

ì Itisalsousefulforcomputingoneachitemofanarray

©2016EV3Lessons.com,Lastedit7/19/2016

BlockSetting:LoopCount

Loopcount

Displaystheloopcounttothescreen

Page 11: Arrays - EV3 Lessons · ì What is an array? ì An array is a variable that holds multiple values ì There are two types of arrays: ì Numeric Array (Holds a set of numbers … 1,2,3,10,55)

Note:Appendvs.Write

ì Appendaddsentriestotheendofanarray(i.e.createsanewindexvalue)

ì Thiscodeproducesanarraywith8entries(three0’sfollowedby5lightreadings)

ì Writeoverwritestheentryatthechosenindex

ì

ì Thiscodeproducesanarraywith5entries(just5lightreadings)

Page 12: Arrays - EV3 Lessons · ì What is an array? ì An array is a variable that holds multiple values ì There are two types of arrays: ì Numeric Array (Holds a set of numbers … 1,2,3,10,55)

ì Makeaprogramthatdisplaysalltheentriesofanarray.Displayeachindexonadifferentline.Youcanuseonlyonedisplayblock.

ì Tips:Youwillneedtouseloops,loopcount,arrayblock,arrayoperations

©2016EV3Lessons.com,Lastedit7/19/2016

Challenge1

Page 13: Arrays - EV3 Lessons · ì What is an array? ì An array is a variable that holds multiple values ì There are two types of arrays: ì Numeric Array (Holds a set of numbers … 1,2,3,10,55)

©2016EV3Lessons.com,Lastedit7/19/2016

Challenge1Solution

Create/Writethedisplayarray

Readhowmanyvaluesareinthearray

Usearrayoperationstoreadeachindexfortheloopcount

Displaythevalueonadifferentlineforeachloopcount

Waituntilyoubumpthebutton toexit

Endafteralltheindexeshavebeendisplayed

Page 14: Arrays - EV3 Lessons · ì What is an array? ì An array is a variable that holds multiple values ì There are two types of arrays: ì Numeric Array (Holds a set of numbers … 1,2,3,10,55)

ì Makeaprogramthataddsupalltheentriesofanarray.Displaythesum.

ì Tips:Youwillneedtouseloops,loopcount,arrayblock,arrayoperations

©2016EV3Lessons.com,Lastedit7/19/2016

Challenge2

Page 15: Arrays - EV3 Lessons · ì What is an array? ì An array is a variable that holds multiple values ì There are two types of arrays: ì Numeric Array (Holds a set of numbers … 1,2,3,10,55)

©2016EV3Lessons.com,Lastedit7/19/2016

Challenge2Solution

Readhowmanyvaluesareinthearray

Readtheindexbasedontheloopcount

Addthearrayvaluetothesumofthepastvalues Displaytothescreen

Createthedisplayarray

Page 16: Arrays - EV3 Lessons · ì What is an array? ì An array is a variable that holds multiple values ì There are two types of arrays: ì Numeric Array (Holds a set of numbers … 1,2,3,10,55)

ì Herearesomefunthingstotry:1. Makeaprogramtocomputetheaveragevalueinanarray2. Makeaprogramthatalwayssavesthelast4lightsensorreadings

inanarray3. Createanarraythatstorescalibrationvaluesforeachsensorport

©2016EV3Lessons.com,Lastedit7/19/2016

NextSteps

Page 17: Arrays - EV3 Lessons · ì What is an array? ì An array is a variable that holds multiple values ì There are two types of arrays: ì Numeric Array (Holds a set of numbers … 1,2,3,10,55)

ì ThistutorialwaswrittenbySanjaySeshan andArvindSeshanì Morelessonsatwww.ev3lessons.com

©2016EV3Lessons.com,Lastedit7/19/2016

Credits

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.