150 likes | 304 Views
Arrays. 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. What is an Array.
E N D
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
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. An array of reals 1.39 0.0 1.69 1.74
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. 1.39 0.0 1.69 1.74 numsis this array’s name nums(1) nums(2) nums(3) nums(4)
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 Integervalue from 1 to array’s length (total number of elements)
Indices (cont’d) • An index can be an Integer variable or any expression that evaluates to an Integervalue. For example: arr(3) arr(studentNumber) arr(studentNumber – 2)
Indices (cont’d) • In Turing, an array is declared with a specified length that cannotbe 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.
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
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. varstudentNames : array 1..30 of string vartop10Scores : array 1..10 of int var <arrayName> : array 1 .. <arrayLength> of <dataType>
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
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
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.
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…
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.
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. • varnums : array 1 .. 5 of int • put “The number of elements is ”, upper(nums)