Anda di halaman 1dari 14

4/4/2011

1
DataStructures:
ArraysandRecords
CIS1005 ComputerProgramming
F d t l Fundamentals
UniversityofMalta Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering.
1
Summary
Introduction
Arrays Arrays
Subscripts
Dimensionandtypeofarray
Multidimensionalarrays
Passingarraystoprocedure
Redimensionadynamicarray
S d d Saveandaccessrecords
Dimensionanddefinerecorddatatypes
Passrecordstoprocedures
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
2
4/4/2011
2
Introduction
Datastructuringisaboutdataorganisationto
k it ffi i t makeitsusemoreefficient
Themostfundamentalwaytostructuredata
isbyusingDim,ConstandPublicstatements
Thissectiondealswithtwootherfeatures
Arrays and Records ArraysandRecords
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
3
Arrays&Records
Array
A collection of values that are referenced by a single Acollectionofvaluesthatarereferencedbyasingle
variablename
Individualvaluesarethenaccessedwithsubscripts
Allarrayvaluesmustbeofthesametype
Record
Allowsvaluesofdifferent typestobestoredusinga
single variable name singlevariablename
Thisprovidesausefulmethodforstoringand
manipulatingdatabasesconsistingofdifferenttypes
ofinformation
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
4
4/4/2011
3
Arraydeclaration
Dimaname(sbs)[Astype]
Wh Where:
aname isthearraysname
sbs denotesthesubscripts,and
typeisthedatatype
Anarrayxhaving5elementsisdefinedas:
Dim x(4) as Double Dimx(4)asDouble
Thisdeclarationsetsasidefivememorylocationsfor
thesubscriptedvariablex(0to4),and
SpecifiesthedatatypeasDouble
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
5
Memorylocationsforarrays
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
6
4/4/2011
4
Array
Ifdimensionisnotlargeenoughtohold
program data an error message is generated programdataanerrormessageisgenerated
Mostoftenthesizeofanarrayismadelarger
thanrequired
Maximumsizedependsonoperatingsystem
andmemoryavailable
ArraysthatexceedamountofsystemRAMare
muchslowerbecausedatawillhavetobe
swappedfromandtodisk
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
7
Engineeringapplication
x 0 2.5 5 7.5 10
T 40 175 245 255 200
Uninsulated
metalbar
fixed
between
twowalls.
Becauseof
theheat
transfer
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
8
transfer
process,the
resulting
temperature
isasshown
inthegraph.
4/4/2011
5
Datastorageusingsimplevariables
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
9
Datastorageusingarrays
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
10
4/4/2011
6
Example...1
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
11
Example...2
Ratherthanhardcodingtheprocesstoaspecific
numberofvalues,programmaycountnumber
ofcellsofdata
Positionthecursoratthetopofthe
columnofdata.DetermineRowvalue
offirstrow.
FindRowvalueoflastdatarow.
Subtractfirstrowvaluefromlastrow
value to find number of rows
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
12
valuetofindnumberofrows.
Resultisthenusedasthefinishvalueofafor
nextlooptoreadthedatafromthesheet.
ActiveCell.Offset isusedtomoveacrossthe
columnsanddowntherows.
4/4/2011
7
MultidimensionalArrays
Exampleofatwodimensionalarrayisaheatedplate
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
13
TraversingmultidimensionalArrays
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
14
4/4/2011
8
Arraybounds
Togloballychangethelowerlimitofallarraysto1,
OptionBasestatement isusedbeforefirstSubprocedurein
module
OptionBase1
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
15
PassingArraystoprocedures
Arrayscanbepassedtoandfromprocedures
E l S b d t d t i Example:Subproceduretodetermine
minimumandmaximumvaluesofaone
dimensionalarray
CallMinMax(Temp,nt,Min,Max)
Where
T i h l Tempisthearray toanalyse
nt isthenumberofcellsofdata
Min&Maxareminimumandmaximumvaluesofdata
containedinthearray
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
16
4/4/2011
9
Arrayparameters
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
17
Bubblesort
Efficientforsmallquantitiesofdata
C t Concept
passdownthroughanarray,
compareadjacentcellsand
swapvaluesiftheyareoutoforder
Repeatuntilarrayissortedcompletely
Smallerelementsrisetowardsthetop
Duringthefinalpassnoswappingwillbe
necessary.Atthispointarrayissorted.
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
18
4/4/2011
10
Diagrammaticrepresentationof
Bubblesort
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
19
Bubblesortprocedure
DimnAsLong
n=inputBox(Numberofvalues)
Dim a(n) As Double, b(n) as Double Dima(n)AsDouble,b(n)asDouble
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
20
4/4/2011
11
Dynamicarrays
Fixedsize arraysaredefinesas:
Di (10) D bl Dima(10)asDouble
Dynamicarraysaredeclaredwithempty
parenthesis
Dima()asDouble
ReDim statementisusedtoredefinethearrayy
ReDim [preserve]aname(sbs)[Astype]
Preserve isanoptionalkeywordusedtopreservethe
datainanexistingarraywhenitssizeischanged
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
21
Records
Structuresofdatathatcontaindifferenttypes
f i f ti ofinformation.
Onewaytohandlesuchdataistodefinean
arrayforeachcolumnofinformation
DimElname(118)AsString
Dim Symbol(118) As String * 3 DimSymbol(118)AsString 3
DimAtomNumber(118)AsInteger
DimAtomicWeight(118)AsDouble
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
22
4/4/2011
12
Record:Typestatement
VBAprovidesanalternativecalledrecord thatallows
storingallinformationinasinglevariable
Typeusertype
itemname Astype
itemname Astype
itemname Astype
...
EndType
Aft t bli hi th t d b di i d Afterestablishingthetype,recordcanbedimensionedas
Dimvarname(n)Asusertype
Individualelementsareidentifiedas
varname(i).itemtype
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
23
Example
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
24
4/4/2011
13
Passingarecordtoaprocedure
TopasstherecordChem toaprocedure,the
ll ld b d lt ith th f callwouldbedealtwiththesamewayasfor
anarray:
CallDisplay(Chem)
ArgumentfortheSubstatementmustinclude
the type thetype
SubDisplay(Chem asChemData)
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
25
Summary
Introduction
Arrays Arrays
Subscripts
Dimensionandtypeofarray
Multidimensionalarrays
Passingarraystoprocedure
Redimensionadynamicarray
S d d Saveandaccessrecords
Dimensionanddefinerecorddatatypes
Passrecordstoprocedures
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
26
4/4/2011
14
DataStructures:
ArraysandRecords
Discussion
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino
ICTSystemsEngineering.
27

Anda mungkin juga menyukai