26
22/03/22 22/03/22 1 Data Structures Arrays

19/10/20151 Data Structures Arrays. 219/10/2015 Learning Objectives Explain initialising arrays and reading data into arrays. Design and write routine/s

Embed Size (px)

Citation preview

20/04/2320/04/23 11

Data StructuresArrays

2220/04/2320/04/23

Learning ObjectivesLearning Objectives

Explain initialising arrays and reading data into arrays.

Design and write routine/s to perform a simple serial search on an array.

3320/04/2320/04/23

What is anWhat is an Array Array??

A data structure that stores as many items A data structure that stores as many items as required using a single variable.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.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 A storage ‘slot’ in an array is called an

element element e.g.e.g.

5656 7878 4242 8080 656511 22 33 44 55

4420/04/2320/04/23

Why use arrays?Why use arrays?

To store a single number you would declare To store a single number you would declare one integer variable.one integer variable.

To store 3 numbers you would need 3 To store 3 numbers you would need 3 variables.variables.

Clearly declaring tens, hundreds, etc… of Clearly declaring tens, hundreds, etc… of variables is difficult and this why arrays variables is difficult and this why arrays are used.are used.

5520/04/2320/04/23

Declaring an array Declaring an array e.g. Dim Names(5) As Stringe.g. Dim Names(5) As String

1.1. The name of the array so that it can find it The name of the array so that it can find it again. again.

2.2. How many items of data are going to be How many items of data are going to be stored / elements, so that it knows how stored / elements, so that it knows how much space to reserve.much space to reserve.

3.3. What sort of data is going to be stored in What sort of data is going to be stored in the array so that the computer knows the array so that the computer knows what part of memory it will have to be what part of memory it will have to be stored in.stored in.

6620/04/2320/04/23

Setting up / Creating an ArraySetting up / Creating an Array

When an array has been declared the When an array has been declared the computer will need to set up / create the array computer will need to set up / create the array which will involve:which will involve:

1.1. Calculating the size of the array according to the Calculating the size of the array according to the number of items of data and data type.number of items of data and data type.

2.2. Using the size of the array and data type to decide Using the size of the array and data type to decide where to locate it and how much space to reserve.where to locate it and how much space to reserve.

Different types of data are stored in different locations of Different types of data are stored in different locations of memory.memory.

3.3. Storing the name of the array in a “look up” table Storing the name of the array in a “look up” table together with the address of the first and last together with the address of the first and last elements and the array’s data type.elements and the array’s data type.

7720/04/2320/04/23

Array “look up” tableArray “look up” table

Name Of Name Of ArrayArray

Start Start AddressAddress

End End AddressAddress Data TypeData Type

NamesNames 121121 126126 StringString

…… …… …… ……

…… …… …… ……

…… …… …… ……

8820/04/2320/04/23

Names ArrayNames Array

BegumBegum

MarcoMarco

AhmedAhmed

VafiVafi

DanDan

AbhiAbhi

Names(1)Names(1)

Names(2)Names(2)

Names(3)Names(3)

Names(4)Names(4)

Names(5)Names(5)

Names(6)Names(6)

9920/04/2320/04/23

To read data into an array To read data into an array

Simply tell the computer what the data is Simply tell the computer what the data is and tell it the position to place it in 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 Will place Rashid in position 11 in the array

(incidentally, erasing any other data that (incidentally, erasing any other data that happened to be in there first).happened to be in there first).

101020/04/2320/04/23

To read data from an arrayTo read data from an array

Tell the computer which position in the Tell the computer which position in the array and assign the data to another array and assign the data to another value.value. e.g. Result = Names(2) e.g. Result = Names(2) Will place the name in element 2 of the Will place the name in element 2 of the

Names array into the variable called Result.Names array into the variable called Result.

111120/04/2320/04/23

Searching an arraySearching an arrayHow would you search an array for a name?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.

BegumBegum

MarcoMarco

AhmedAhmed

VafiVafi

DanDan

AbhiAbhi

Names(1)Names(1)

Names(2)Names(2)

Names(3)Names(3)

Names(4)Names(4)

Names(5)Names(5)

Names(6)Names(6)

121220/04/2320/04/23

Searching an arraySearching an array

Searching for a particular person in the array Searching for a particular person in the array involves a simple loop and a question.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 = 1Counter = 1

While Counter is less than 21, DoWhile 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 CounterElse Add 1 to Counter

EndwhileEndwhile

Print “Name not in array”Print “Name not in array”

EndEnd

131320/04/2320/04/23

Searching an arraySearching an array

Searching for a particular person in the array Searching for a particular person in the array involves a simple loop and a question.involves a simple loop and a question. e.g. search for Liu in the array Namese.g. search for Liu in the array Names

Dim Counter As Integer = 1Dim Counter As Integer = 1

For Counter = 1 To 6For Counter = 1 To 6 If Names(Counter) = “Liu” Then If Names(Counter) = “Liu” Then

lblResultOfSearch.Text = “Found”lblResultOfSearch.Text = “Found”

Exit SubExit Sub End If End If

Next CounterNext Counter

lblResultOfSearch.Text = “Name not in array”lblResultOfSearch.Text = “Name not in array”

141420/04/2320/04/23

InitialisingInitialising an array an array

‘‘All programs All programs assume that the start values are 0 assume that the start values are 0 and arrays may contain values from previous and arrays may contain values from previous processing. If they do then programs will use this processing. If they do then programs will use this data and give incorrect results.data and give incorrect results.

Dim Letters(26) As IntegerDim Letters(26) As Integer

Loop through each array element from element 1 to the last Loop through each array element from element 1 to the last element. element.

FOR i = 1 TO 26 FOR i = 1 TO 26 ‘‘LastElement LastElement e.g. e.g. 26 letters of the alphabet.26 letters of the alphabet. Letters(i) = 0 Letters(i) = 0 ‘Place a zero in all array elements ‘Place a zero in all array elements

NEXTNEXT

““Letter Tally” ProgramLetter Tally” Program

161620/04/2320/04/23

““Letter Tally” ProgramLetter Tally” Program

Obviously use a loop with a Mid function to Obviously use a loop with a Mid function to extract letter by letter from the text extract letter by letter from the text entered.entered.Then the long manual way would be to:Then the long manual way would be to:

If Character = “a” ThenIf Character = “a” ThenLetters (1) = Letters (1) + 1Letters (1) = Letters (1) + 1

ElseIf Character = “b” ThenElseIf Character = “b” ThenLetters (2) = Letters (2) + 1Letters (2) = Letters (2) + 1

and so on….and so on….

However, this is not efficient and is not the approach However, this is not efficient and is not the approach exams are really expecting.exams are really expecting.

See next slide.See next slide.

““Letter Tally” ProgramLetter 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. Make sure you include a reset button.2. This does not deal with capitals, if you have time try to see if

you can get the program to do so.

181820/04/2320/04/23

Two Dimensional ArraysTwo Dimensional Arrays

The array that has been described so far is The array that has been described so far is really only a list of single data items. really only a list of single data items.

It is possible to have a number of pieces of It is possible to have a number of pieces of information about each student e.g. name, information about each student e.g. name, address etc… address etc…

The array would then have 6 students and more The array would then have 6 students and more than one thing about each, this is called a 2D than one thing about each, this is called a 2D array.array.

191920/04/2320/04/23

Declaring 2D ArraysDeclaring 2D Arrays

Dim …(…, …) As …Dim …(…, …) As …

1D Size1D Size 2D Size2D Size

00 11 22 3300

11

22

202020/04/2320/04/23

2D Arrays2D Arrays

e.g. a firm’s quarterly sales figures for the years e.g. a firm’s quarterly sales figures for the years 1990 – 1999.1990 – 1999. 4 (quarters) x 10 (years) = 40 items of data4 (quarters) x 10 (years) = 40 items of data

Dim SalesFigures(4, 10) As DecimalDim SalesFigures(4, 10) As Decimal

212120/04/2320/04/23

2D Arrays2D Arrays

Each row is a year.Each row is a year.

Each column is a Each column is a quarter.quarter.

e.g.e.g. Sales was Sales was €€5680056800 in in

the the 33rdrd quarter of quarter of 19901990..SalesFigures(2,0) = 56800SalesFigures(2,0) = 56800

Sales was Sales was €€9640096400 in in the the 44thth quarter of quarter of 19981998..

SalesFigures(3,8) = SalesFigures(3,8) = 9640096400

5680056800

9640096400

00 11 22 3300

11

22

33

44

55

66

77

88

99

222220/04/2320/04/23

Uses for 2D ArraysUses for 2D Arrays

Useful for storing data for some mathematical Useful for storing data for some mathematical problems.problems.

Limited use for ‘Limited use for ‘businessbusiness’ type problems ’ type problems because all data items have to be of the same because all data items have to be of the same type.type.

232320/04/2320/04/23

Exam QuestionsExam 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]

242420/04/2320/04/23

Exam AnswersExam Answers

252520/04/2320/04/23

An array QuestionAn array Question

An array is to be used to store information.An array is to be used to store information.

State three parameters that need to be State three parameters that need to be given about the array before it can be given about the array before it can be used, explaining the reason why each is used, explaining the reason why each is necessary.necessary.

(6)(6)

262620/04/2320/04/23

AnswerAnswer

The size of the array (how many data items it will The size of the array (how many data items it will hold)…hold)… So that this amount of space can be reserved in So that this amount of space can be reserved in

memory.memory.

The type of data to be stored in the array…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…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…The number of dimensions of the array… So that the computer knows what form the data is So that the computer knows what form the data is

going to be stored in.going to be stored in.

Note: these marks go in pairs. Note: these marks go in pairs.