1 / 15

Arrays

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.

brie
Télécharger la présentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. An array of reals 1.39 0.0 1.69 1.74

  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. 1.39 0.0 1.69 1.74 numsis this array’s name nums(1) nums(2) nums(3) nums(4)

  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 Integervalue 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 Integervalue. For example: arr(3) arr(studentNumber) arr(studentNumber – 2)

  7. 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.

  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. varstudentNames : array 1..30 of string vartop10Scores : 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. • varnums : array 1 .. 5 of int • put “The number of elements is ”, upper(nums)

More Related