Data Structures Arrays 12292021 1 Learning Objectives Explain
Data Structures Arrays 12/29/2021 1
Learning Objectives Explain initialising arrays and reading data into arrays. Design and write routine/s to perform a simple serial search on an array. 12/29/2021 2
What is an Array? A data structure that stores as many items as required using a single variable. n All items must be of the same data type. e. g. n n n An array of integers. An array of strings. … A list box is an example of an array. n A storage ‘slot’ in an array is called an element e. g. 1 2 3 4 5 56 78 42 80 65 12/29/2021 n 3
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. 12/29/2021 4
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. 12/29/2021 5
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. 2. Calculating the size of the array according to the number of items of data and data type. 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. 12/29/2021 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. 6
Array “look up” table Name Of Array Start Address End Address Data Type Names 121 126 String … … … 12/29/2021 7
Names Array Names(1) Names(2) Names(3) Names(4) Names(5) Names(6) 12/29/2021 Begum Marco Ahmed Vafi Dan Abhi 8
To read data into an array Simply tell the computer what the data is and tell it the position to place it in n n 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). 12/29/2021 9
To read data from an array Tell the computer which position in the array and assign the data to another value. n n e. g. Result = Names(2) Will place the name in element 2 of the Names array into the variable called Result. 12/29/2021 10
Searching an array How would you search an array for a name? n n Write down the steps you go through. Turn this into a program using pseudocode. Names(1) Names(2) Names(3) Names(4) Names(5) 12/29/2021 Names(6) Begum Marco Ahmed Vafi Dan Abhi 11
Searching an array Searching for a particular person in the array involves a simple loop and a question. n e. g. search for Liu in the array NAME$ Counter = 1 While Counter is less than 21, Do n n If NAME$(Counter) = Liu Then Print “Found” and End. Else Add 1 to Counter Endwhile Print “Name not in array” End 12/29/2021 12
Searching an array Searching for a particular person in the array involves a simple loop and a question. n e. g. search for Liu in the array Names Dim Counter As Integer = 1 For Counter = 1 To 6 n n If Names(Counter) = “Liu” Then lbl. Result. Of. Search. Text = “Found” Exit Sub End If Next Counter lbl. Result. Of. Search. Text = “Name not in array” 12/29/2021 13
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 ‘Last. Element e. g. 26 letters of the alphabet. n Letters(i) = 0 ‘Place a zero in all array elements NEXT 12/29/2021 14
“Letter Tally” Program
“Letter Tally” Program 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: n If Character = “a” Then Letters (1) = Letters (1) + 1 n Else. If Character = “b” Then Letters (2) = Letters (2) + 1 n and so on…. However, this is not efficient and is not the approach exams are really expecting. n See next slide. 12/29/2021 16
“Letter Tally” Program Use the Asc function to subtract the ASCII code of “a” from the ASCII code of each letter. n n n The difference + 1 will tell you which element of the Letters(…. ) array to use. Character. Index = ASC(Character)-ASC(“a”) + 1 Letters(Character. Index) = Letters(Character. Index) + 1 Notes: 1. 2. Make sure you include a reset button. This does not deal with capitals, if you have time try to see if you can get the program to do so.
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 2 D array. 12/29/2021 18
Declaring 2 D Arrays Dim …(…, …) As … 1 D Size 0 2 D Size 1 2 3 0 1 2 12/29/2021 19
2 D Arrays e. g. a firm’s quarterly sales figures for the years 1990 – 1999. n 4 (quarters) x 10 (years) = 40 items of data Dim Sales. Figures(4, 10) As Decimal 12/29/2021 20
2 D Arrays 0 0 1 2 3 n Sales was € 56800 in 4 rd the 3 quarter of 1990. Sales. Figures(2, 0) = 56800 5 6 n Sales was € 96400 in the 4 th quarter of 1998. 7 Sales. Figures(3, 8) = 96400 8 9 12/29/2021 Each row is a year. Each column is a quarter. e. g. 1 2 3 56800 96400 21
Uses for 2 D 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. 12/29/2021 22
Exam Questions 1. Describe how an array is initialised in the memory of a computer. [4] 2. Describe how an array may be searched serially to find a specific data item. [4] 12/29/2021 23
Exam Answers 12/29/2021 24
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) 12/29/2021 25
Answer The size of the array (how many data items it will hold)… n So that this amount of space can be reserved in memory. The type of data to be stored in the array… n So that it can be set up in the correct area of memory. The name of the array… n So that it can be identified when it needs to be used. The number of dimensions of the array… n So that the computer knows what form the data is going to be stored in. Note: these marks go in pairs. 12/29/2021 26
- Slides: 26