Download - Arrays
1
Arrays
2
Objectives:• Learn about arrays and when to use them• Learn the syntax for declaring and initializing
arrays and how to access array’s size and elements
• Learn simple array algorithms
3
What is an Array• An array is a block of consecutive memory
locations that hold values of the same data type. (a collection of data)
• Individual locations are called the array’s elements.
• When we say “element” we often mean the value stored in that element.
1.39
1.69
1.74
0.0
An array of reals
4
What is an Array (cont’d)• Rather than treating each element as a
separate named variable, the whole array gets one name.
• Specific array elements are referred to by using array’s name and the element’s number, called index or subscript.
nums(1) nums(2) nums(3) nums(4)
1.39
1.69
1.74
0.0 nums is this
array’s name
5
Indices (Subscripts)• In Turing, an index is written within round
brackets following the array’s name, e.g. Assuming the array is named arr, arr(k)
• Indices start from 1; the first element of an array a is referred to as arr(1) and the n-th element as arr(n).
• An index must be any Integer value from 1 to array’s length (total number of elements)
6
Indices (cont’d)• An index can be an Integer variable or any
expression that evaluates to an Integer value. For example:
arr(3) arr(studentNumber) arr(studentNumber – 2)
7
Indices (cont’d)• In Turing, an array is declared with a
specified length that cannot be changed. Think of it as reserving parking spaces for your
data, but the spaces are empty until you park some data in a given spot
• In Turing while the program is running if you try to access an element not in the range of indices (from 1 to length) an “out of range” error will occur.
8
Why Do We Need Arrays?• The power of arrays stems from data organization.
• If we did not use arrays we would be forced to use individual variables in their place.
• Imagine creating a program that stored the name of every student in the school.
• You would need 1400 string variables or 1 string array
9
Arrays Creation• Just like any other variable an array must be
created before it can be used, it must be of a single data dype
• One way to create an array:
• E.g.
var studentNames : array 1..30 of string
var top10Scores : array 1..10 of int
var <arrayName> : array 1 .. <arrayLength> of <dataType>
10
Arrays Creation Continued• The three required pieces of information for
arrays are as follows: arrayName: Much like any other variable it needs to follow
the variable naming conventions (camelCase)
arrayLength: This is the number elements in the array
DataType: Much like all other variables, this is the type of data to be stored in the variable. ALL ELEMENTS OF AN ARRAY MUST BE THE SAME TYPE
11
Declaration and Initialization• When an array is created, space is allocated to hold
its elements. If values are not assigned manually, the elements are null (empty).
• This means they have NO value, not 0…nothing not “ “…nothing
• Remember the parking lot example…You have just reserved the spaces, they are empty until you fill them
12
Initializing Elements• What if you want to give your entire array
default (starting) values. This may be useful if you are tracking student
marks, the beginning of the semester each student starts with a 0
• Well, the answer is, you must go through each one and assign it a new value.
13
Initializing Elements• E.g. We have an array holding the Boolean
value of whether each student in the class is passing or failing, true for Pass, false for Fail. We are optimistic and we want everyone to start off passing.
studentPass(1) := true
studentPass(2) := true
…
studentPass(15) = true
• We will learn faster ways of doing this soon…
14
Initializing Elements• There is one last way to Give your array elements
starting values.• Immediately after creating an array on the same line
you can give all of the elements a starting value, but it must be ALL E.g.
• var ages : array 1 .. 5 of int := init (15,15,23,7,18)
• Notice 2 things: The values are surrounded by braces on the same
line as the declaration We are using a new keyword, init, which is short
for initialize.
15
Array’s Length• The length of an array is decided when that
array is created.
• It is possible to retrieve the length of the array through code using upper(arrayName) From this we can get the total number of
elements, also known as length of an array: E.g.
• var nums : array 1 .. 5 of int• put “The number of elements is ”, upper(nums)