Download presentation
Presentation is loading. Please wait.
Published byBeatrice Anthony Modified over 9 years ago
1
19/10/20151 Data Structures Arrays
2
219/10/2015 Learning Objectives Explain initialising arrays and reading data into arrays. Design and write routine/s to perform a simple serial search on an array.
3
319/10/2015 What is an Array? A data structure that stores as many items as required using a single variable. All items must be of the same data type. All items must be of the same data type.e.g. An array of integers. An array of integers. An array of strings. An array of strings. … A list box is an example of an array. A list box is an example of an array. A storage ‘slot’ in an array is called an element e.g. A storage ‘slot’ in an array is called an element e.g. 5678428065 12345
4
419/10/2015 Why use arrays? To store a single number you would declare one integer variable. To store 3 numbers you would need 3 variables. Clearly declaring tens, hundreds, etc… of variables is difficult and this why arrays are used.
5
519/10/2015 Declaring an array e.g. Dim Names(5) As String 1.The name of the array so that it can find it again. 2.How many items of data are going to be stored / elements, so that it knows how much space to reserve. 3.What sort of data is going to be stored in the array so that the computer knows what part of memory it will have to be stored in.
6
619/10/2015 Setting up / Creating an Array When an array has been declared the computer will need to set up / create the array which will involve: 1. Calculating the size of the array according to the number of items of data and data type. 2. Using the size of the array and data type to decide where to locate it and how much space to reserve. Different types of data are stored in different locations of memory. 3. Storing the name of the array in a “look up” table together with the address of the first and last elements and the array’s data type.
7
719/10/2015 Array “look up” table Name Of Array Start Address End Address Data Type Names121126String ………… ………… …………
8
819/10/2015 Names Array Begum Marco Ahmed Vafi Dan Abhi Names(1) Names(2) Names(3) Names(4) Names(5) Names(6)
9
919/10/2015 To read data into an array Simply tell the computer what the data is and tell it the position to place it in e.g. Names(11) = Rashid e.g. Names(11) = Rashid Will place Rashid in position 11 in the array (incidentally, erasing any other data that happened to be in there first). Will place Rashid in position 11 in the array (incidentally, erasing any other data that happened to be in there first).
10
1019/10/2015 To read data from an array Tell the computer which position in the array and assign the data to another value. e.g. Result = Names(2) e.g. Result = Names(2) Will place the name in element 2 of the Names array into the variable called Result. Will place the name in element 2 of the Names array into the variable called Result.
11
1119/10/2015 Searching an array How would you search an array for a name? Write down the steps you go through. Write down the steps you go through. Turn this into a program using pseudocode. Turn this into a program using pseudocode. Begum Marco Ahmed Vafi Dan Abhi Names(1) Names(2) Names(3) Names(4) Names(5) Names(6)
12
1219/10/2015 Searching an array Searching for a particular person in the array involves a simple loop and a question. e.g. search for Liu in the array NAME$ e.g. search for Liu in the array NAME$ Counter = 1 While Counter is less than 21, Do If NAME$(Counter) = Liu Then Print “Found” and End. If NAME$(Counter) = Liu Then Print “Found” and End. Else Add 1 to Counter Else Add 1 to CounterEndwhile Print “Name not in array” End
13
1319/10/2015 Searching an array Searching for a particular person in the array involves a simple loop and a question. e.g. search for Liu in the array Names e.g. search for Liu in the array Names Dim Counter As Integer = 1 For Counter = 1 To 6 If Names(Counter) = “Liu” Then If Names(Counter) = “Liu” Then lblResultOfSearch.Text = “Found” Exit Sub End If End If Next Counter lblResultOfSearch.Text = “Name not in array”
14
1419/10/2015 Initialising an array ‘All programs assume that the start values are 0 and arrays may contain values from previous processing. If they do then programs will use this data and give incorrect results. Dim Letters(26) As Integer Loop through each array element from element 1 to the last element. FOR i = 1 TO 26 ‘ LastElement e.g. 26 letters of the alphabet. Letters(i) = 0 ‘Place a zero in all array elements Letters(i) = 0 ‘Place a zero in all array elementsNEXT
15
“Letter Tally” Program
16
1619/10/2015 Obviously use a loop with a Mid function to extract letter by letter from the text entered. Then the long manual way would be to: If Character = “a” Then If Character = “a” Then Letters (1) = Letters (1) + 1 ElseIf Character = “b” Then ElseIf Character = “b” Then Letters (2) = Letters (2) + 1 and so on…. and so on…. However, this is not efficient and is not the approach exams are really expecting. See next slide. See next slide.
17
“Letter Tally” Program Use the Asc function to subtract the ASCII code of “a” from the ASCII code of each letter. The difference + 1 will tell you which element of the Letters(….) array to use. CharacterIndex = ASC(Character)-ASC(“a”) + 1 Letters(CharacterIndex) = Letters(CharacterIndex) + 1 Notes: 1. 1. Make sure you include a reset button. 2. 2. This does not deal with capitals, if you have time try to see if you can get the program to do so.
18
1819/10/2015 Two Dimensional Arrays The array that has been described so far is really only a list of single data items. It is possible to have a number of pieces of information about each student e.g. name, address etc… The array would then have 6 students and more than one thing about each, this is called a 2D array.
19
1919/10/2015 Declaring 2D Arrays Dim …(…, …) As … 1D Size 2D Size 0123 01 2
20
2019/10/2015 2D Arrays e.g. a firm’s quarterly sales figures for the years 1990 – 1999. 4 (quarters) x 10 (years) = 40 items of data 4 (quarters) x 10 (years) = 40 items of data Dim SalesFigures(4, 10) As Decimal
21
2119/10/2015 2D Arrays Each row is a year. Each column is a quarter. e.g. Sales was €56800 in the 3 rd quarter of 1990. Sales was €56800 in the 3 rd quarter of 1990. SalesFigures(2,0) = 56800 Sales was €96400 in the 4 th quarter of 1998. Sales was €96400 in the 4 th quarter of 1998. SalesFigures(3,8) = 96400 56800 96400 0123 01 2 3 4 5 6 7 8 9
22
2219/10/2015 Uses for 2D Arrays Useful for storing data for some mathematical problems. Limited use for ‘business’ type problems because all data items have to be of the same type.
23
2319/10/2015 Exam Questions 1. 1.Describe how an array is initialised in the memory of a computer. [4] 2. 2.Describe how an array may be searched serially to find a specific data item. [4]
24
2419/10/2015 Exam Answers
25
2519/10/2015 An array Question An array is to be used to store information. State three parameters that need to be given about the array before it can be used, explaining the reason why each is necessary. (6)
26
2619/10/2015 Answer The size of the array (how many data items it will hold)… So that this amount of space can be reserved in memory. So that this amount of space can be reserved in memory. The type of data to be stored in the array… So that it can be set up in the correct area of memory. So that it can be set up in the correct area of memory. The name of the array… So that it can be identified when it needs to be used. So that it can be identified when it needs to be used. The number of dimensions of the array… So that the computer knows what form the data is going to be stored in. So that the computer knows what form the data is going to be stored in. Note: these marks go in pairs.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.