Data Types & Structures Using Arrays GCSE Computing
Array Definition A data structure that contains several data items… of the same data type… grouped under one identifier… Individual items are accessed using an index
Array Scores = identifier 5 elements Integers 1 12 2 4 scores 3 16 Variable Name 4 2 5 8 Element Number Data
Array Declaration Items which should be specified when declaring an array Identifier/name Size of the array (number of elements) Data type
Why use an Array? Why use an array rather than separate variables? Code is easier to manage as there are fewer variables. Can use iteration instead of dealing with each variable separately. More efficient programming
Fill an Array Manually - Code in VB Dim scores(5) as integer scores(1) = 12 scores(2) = 4 scores(3) = 16 scores(4) = 2 scores(5) = 8 console. writeline(scores(3)) 16
Fill an Array - Use a Loop - VB Dim scores(5) as integer for counter = 1 to 5 scores(counter) = a random number console. writeline(scores(counter)) next
Initialise an Array FOR counter=1 TO 5 scores(counter)= 0 NEXT Set values of each element within the array to 0
Class exercise VB Console Program Declare array called scores 10 elements Integers Fill with random number between 1 and 20 Use a FOR NEXT loop Display Array contents Element 1 is 12 Element 2 is 4 Element 3 is 17