08/09/20151 7 Arrays Defining, Declaring & Processing.

47
20/06/22 20/06/22 1 7 Arrays 7 Arrays Defining, Declaring & Defining, Declaring & Processing Processing

Transcript of 08/09/20151 7 Arrays Defining, Declaring & Processing.

Page 1: 08/09/20151 7 Arrays Defining, Declaring & Processing.

19/04/2319/04/23 11

7 Arrays7 Arrays

Defining, Declaring & ProcessingDefining, Declaring & Processing

Page 2: 08/09/20151 7 Arrays Defining, Declaring & Processing.

2219/04/2319/04/23

Learning ObjectivesLearning Objectives

Define an array and an element.Define an array and an element.

Explain why arrays are useful and so Explain why arrays are useful and so when they should be used.when they should be used.

State how to declare an array.State how to declare an array.

State how to process an array i.e. use it State how to process an array i.e. use it (store or retrieve data).(store or retrieve data).

Explain the concept of 2D arrays and how Explain the concept of 2D arrays and how to declare them.to declare them.

Page 3: 08/09/20151 7 Arrays Defining, Declaring & Processing.

3319/04/2319/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

Page 4: 08/09/20151 7 Arrays Defining, Declaring & Processing.

4419/04/2319/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.

Page 5: 08/09/20151 7 Arrays Defining, Declaring & Processing.

5519/04/2319/04/23

How toHow to declare declare anan array? array?

Similar to variables.Similar to variables.Dim …(…) As …Dim …(…) As …

Array nameArray name

The number of elements required.The number of elements required.

Data TypeData Type

Page 6: 08/09/20151 7 Arrays Defining, Declaring & Processing.

6619/04/2319/04/23

Processing Processing anan Array Array

……(…) =(…) =

Array NameArray Name A number or a variableA number or a variable

(with a stored number) (with a stored number)

representing the requiredrepresenting the required

element.element.

e.g.e.g. ExamMarks(3) = MarkExamMarks(3) = Mark Will store the contents of the Will store the contents of the MarkMark variable in variable in

the the 33rdrd element of the element of the ExamMarksExamMarks array. array.

Page 7: 08/09/20151 7 Arrays Defining, Declaring & Processing.

7719/04/2319/04/23

InitialisingInitialising an array an array

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

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

Page 8: 08/09/20151 7 Arrays Defining, Declaring & Processing.

8819/04/2319/04/23

Program 7 Number ArrayProgram 7 Number Array

Specification:Specification: Allow the user to enter up to 5 numbers and Allow the user to enter up to 5 numbers and

store them in an array.store them in an array. Output an appropriate message if the user Output an appropriate message if the user

attempts to store a 6attempts to store a 6thth number. number. Allow the user to display the contents of the Allow the user to display the contents of the

array at any time, and to enter a number to be array at any time, and to enter a number to be searched for in the array.searched for in the array.

Display the result of this search.Display the result of this search.

Page 9: 08/09/20151 7 Arrays Defining, Declaring & Processing.

Program 7 Number ArrayProgram 7 Number Array

Create a new project named ‘Create a new project named ‘Number ArrayNumber Array’.’.

txtNumbertxtNumber

butAddToArraybutAddToArray

butDisplayArraybutDisplayArray

txtNumberSearchtxtNumberSearch

lstArraylstArray

butNumberSearchbutNumberSearch lblNumberSearchlblNumberSearch

Page 10: 08/09/20151 7 Arrays Defining, Declaring & Processing.

101019/04/2319/04/23

Program 7 Number ArrayProgram 7 Number Array

Declare a global array and a variable for the element Declare a global array and a variable for the element of the array.of the array. ‘‘Declare array.Declare array. Dim Numbers(5) As Integer Dim Numbers(5) As Integer

‘‘Will keep a count of the Will keep a count of the numbernumber of of numbersnumbers currently held currently held ‘‘in the in the NumbersNumbers array. array. Dim HowManyNumbers As IntegerDim HowManyNumbers As Integer

Go back to Election Program

Page 11: 08/09/20151 7 Arrays Defining, Declaring & Processing.

111119/04/2319/04/23

Program 7 Number ArrayProgram 7 Number Array

butAddToArray code:butAddToArray code: ‘‘Will be used to add the number to be stored in the array.Will be used to add the number to be stored in the array. Dim Number As Integer Dim Number As Integer Number = txtNumber.Text Number = txtNumber.Text ‘Store the number entered.‘Store the number entered. If HowManyNumbers = 5 Then If HowManyNumbers = 5 Then ‘Is array full?‘Is array full?

MsgBox(“The array is FULL!”) MsgBox(“The array is FULL!”) ‘Inform user array is full!‘Inform user array is full! Else Else ‘Array not full.‘Array not full.

‘‘Increment How Many NumbersIncrement How Many Numbers to move to next element of the to move to next element of the NumbersNumbers array.array.HowManyNumbers = HowManyNumbers + 1 HowManyNumbers = HowManyNumbers + 1 ‘‘Store the number in the next element of the array.Store the number in the next element of the array.Numbers(HowManyNumbers) = NumberNumbers(HowManyNumbers) = Number'Clear the list box which may be displaying previously entered numbers.'Clear the list box which may be displaying previously entered numbers.lstArray.Items.ClearlstArray.Items.CleartxtNumber. Text = “” txtNumber. Text = “” ‘Clear txtNumber.‘Clear txtNumber.txtNumber.Focus txtNumber.Focus ‘Place cursor in txtNumber.‘Place cursor in txtNumber.

End IfEnd If

Go back to Election Program

Page 12: 08/09/20151 7 Arrays Defining, Declaring & Processing.

Program 7 Number ArrayProgram 7 Number Array

butDisplayArray code:butDisplayArray code: ‘‘Clear the list box from last time.Clear the list box from last time. lstArray.Items.Clear()lstArray.Items.Clear() ‘‘Will be used to represent each element of the array.Will be used to represent each element of the array. Dim Dim ElementElement As Integer As Integer 'Go through each used element in the array 'Go through each used element in the array 'and add each to the items of lstArray.'and add each to the items of lstArray. For ElementFor Element = = 11 To To 55

lstArray.Items.Add(Numbers(Element))lstArray.Items.Add(Numbers(Element)) Next ElementNext Element

Page 13: 08/09/20151 7 Arrays Defining, Declaring & Processing.

131319/04/2319/04/23

Program 7.1 Number ArrayProgram 7.1 Number ArraybutNumberSearchbutNumberSearch code: code:

Dim Element As IntegerDim Element As Integer Dim SearchNumber As IntegerDim SearchNumber As Integer SearchNumber = txtNumberSearch.TextSearchNumber = txtNumberSearch.Text 'Go through each used element in the array to search for the number required.'Go through each used element in the array to search for the number required. For Element = 1 To 5For Element = 1 To 5

‘‘Has the number been found?Has the number been found?If Numbers(Element) = SearchNumber Then If Numbers(Element) = SearchNumber Then

lblNumberSearch.Text = "This number is in element " & Element & " of the array.“lblNumberSearch.Text = "This number is in element " & Element & " of the array.“ Exit SubExit Sub

End IfEnd If‘‘Keep looking until number is found or all 5 elements have been searched.Keep looking until number is found or all 5 elements have been searched.

Next Element Next Element

‘‘If loop has finished then the number was not found so display a suitable If loop has finished then the number was not found so display a suitable message.message.

lblNumberSearch.Text = “This number is not in the array.”lblNumberSearch.Text = “This number is not in the array.”

Go back to Election ProgramGo back to Election Program

Page 14: 08/09/20151 7 Arrays Defining, Declaring & Processing.

141419/04/2319/04/23

Program 7 Number ArrayProgram 7 Number Array

Run the program and test it.Run the program and test it.

Page 15: 08/09/20151 7 Arrays Defining, Declaring & Processing.

151519/04/2319/04/23

Program 7 Number ArrayProgram 7 Number Array

Add a reset button with the following code:Add a reset button with the following code:

‘‘Set HowManyNumbers back to 1 for a new set of Set HowManyNumbers back to 1 for a new set of numbers.numbers.HowManyNumbers = 0HowManyNumbers = 0

‘‘Clear all text boxes, labels and the list box.Clear all text boxes, labels and the list box.

lblNumberSearch.Text = “”lblNumberSearch.Text = “”

txtNumberSearch.Text = “”txtNumberSearch.Text = “”txtNumber.Text = “”txtNumber.Text = “”lstArray.Items.ClearlstArray.Items.CleartxtNumber.Focus ‘txtNumber.Focus ‘Place cursor in txtNumber.Place cursor in txtNumber.

Page 16: 08/09/20151 7 Arrays Defining, Declaring & Processing.

161619/04/2319/04/23

Program 7 Number ArrayProgram 7 Number Array

Now:Now:1.1. Enter 5 numbers.Enter 5 numbers.2.2. Click the “Display Array” button.Click the “Display Array” button.3.3. Click the reset button.Click the reset button.4.4. Enter 2 new numbers.Enter 2 new numbers.5.5. Click the “Display Array” button.Click the “Display Array” button.

What happens? What should happen? What happens? What should happen? Why does this happen?Why does this happen?

Page 17: 08/09/20151 7 Arrays Defining, Declaring & Processing.

171719/04/2319/04/23

InitialisingInitialising an array an array Add the following code to the Reset button.Add the following code to the Reset button.

‘‘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 data processing. If they do then programs will use this data ‘‘and give incorrect results.and give incorrect results.

'Loop through the array and clear each number.'Loop through the array and clear each number.Dim Element As IntegerDim Element As IntegerFor Element = 1 To 5For Element = 1 To 5

Numbers(Element) = 0Numbers(Element) = 0

Next ElementNext Element

Go back to Election ProgramGo back to Election Program

Page 18: 08/09/20151 7 Arrays Defining, Declaring & Processing.

181819/04/2319/04/23

Program 7 Number ArrayProgram 7 Number Array

Run the program and test it.Run the program and test it.

Page 19: 08/09/20151 7 Arrays Defining, Declaring & Processing.

Commenting on Arrays

In presentation 7 I will only ask for comments to arrays.Your comments MUST explain:

What does the array hold? And if it is important: How many elements and why this

number? And when it is being used:

What are you storing in/retrieving from the array and why?When (after and before what) are you doing this and why does it have to be done there?

When in the procedure code or, if it is on its own, in which procedure (button, checkbox, textbox, etc…)?

Page 20: 08/09/20151 7 Arrays Defining, Declaring & Processing.

202019/04/2319/04/23

Extension “Add 10 Numbers” Extension “Add 10 Numbers” Program 1Program 1

Write a program to allow a user to enter ten numbers Write a program to allow a user to enter ten numbers and display the sum.and display the sum.

Use an array Use an array NOTNOT FirstNumber, SecondNumber, ThirdNumber, etc….FirstNumber, SecondNumber, ThirdNumber, etc….You can adapt the “Add Two Numbers” in You can adapt the “Add Two Numbers” in 3.1 Working with Data3.1 Working with Data but but note that this program did not use an array.note that this program did not use an array.

Extension:Extension: Use a global “Numbers” array and “HowManyNumbers” variable Use a global “Numbers” array and “HowManyNumbers” variable

as before, but do as before, but do NOTNOT use a global “RunningTotal” variable, use a global “RunningTotal” variable, use use a loop to add up all the elements in the arraya loop to add up all the elements in the array when the “ when the “SumSum” ” button is clicked.button is clicked.

This is not because a global running total variable would not work, This is not because a global running total variable would not work, as it would, but because exams may want you to do it this way and I as it would, but because exams may want you to do it this way and I want to make sure you can.want to make sure you can.

Add a reset button.Add a reset button. Adapt the program so that user can see a list box with all the Adapt the program so that user can see a list box with all the

numbers entered. numbers entered.

Page 21: 08/09/20151 7 Arrays Defining, Declaring & Processing.

212119/04/2319/04/23

Extension “Election” Program 2Extension “Election” Program 2A town election is held to elect a new mayor.A town election is held to elect a new mayor.The people in the town can vote for whoever they prefer from the three The people in the town can vote for whoever they prefer from the three candidates A, B, C.candidates A, B, C.The voting is done by each voter typing A, B and C in a text box in a The voting is done by each voter typing A, B and C in a text box in a voting booth and then clicking a voting booth and then clicking a VoteVote button. button. This acts as input to a computer program.This acts as input to a computer program.The software should assume that there are a maximum number of 1000 The software should assume that there are a maximum number of 1000 people who will be voting people who will be voting (don’t worry you will not be asked to test 1000 votes (don’t worry you will not be asked to test 1000 votes but set it up so that it would accept 1000 votes)but set it up so that it would accept 1000 votes).. It uses an array, It uses an array, Votes()Votes() to store the votes that are cast. to store the votes that are cast.

Votes()Votes() is an array of is an array of 10001000 characters: characters: AA, , BB or or CC..

A second array, A second array, CandidateTotals()CandidateTotals(), contains , contains 33 integers and is used with integers and is used with the array, the array, Votes()Votes()..Make sure you initialise each element of each array.Make sure you initialise each element of each array.Write this program and when a “Finish” button is clicked the winner Write this program and when a “Finish” button is clicked the winner should be displayed.should be displayed.Make sure the program can deal correctly with two or three of the Make sure the program can deal correctly with two or three of the candidates receiving equal votes. candidates receiving equal votes.

See next slides for some hints!See next slides for some hints!

Page 22: 08/09/20151 7 Arrays Defining, Declaring & Processing.

Extension “Election” Program 2Extension “Election” Program 2

Page 23: 08/09/20151 7 Arrays Defining, Declaring & Processing.

Extension “Election” Program 2Extension “Election” Program 2

Declare two arrays Votes(….) & & CandidateTotals(….). See the introduction slide of this extension program for the numbers to use

in (….) – in red - slide 20 of this presentation.

Declare a variable to count how many votes are cast

– HowMany….. . As the Votes(….) array and the HowMany….. Variable will be needed by

both the Vote and Result buttons you will need to declare them …..?

Compare with the Program 7.1 Number Array (slide 10 of this presentation).

Page 24: 08/09/20151 7 Arrays Defining, Declaring & Processing.

Extension “Election” Program 2Extension “Election” Program 2

In the Vote button add each vote to the Votes(….) array.

Compare with the “Add” button of the Program 7.1 Number Array (slide 11 of this presentation).

Page 25: 08/09/20151 7 Arrays Defining, Declaring & Processing.

Extension “Election” Program 2Extension “Election” Program 2In the Result button search the Votes(….) array for each vote (A, B or C). Place totals in the CandidateTotals(….) array.

CandidateTotals(1) for how many A’s, CandidateTotals(2) for how many B’s and …..

Compare with the “Search” button of the Program 7.1 Number Array (slide 13 of this presentation).

However, you are no longer searching for a number entered by the user but first for “A”, then for “B” and then for “C”.

Use three separate If’s for “A”, “B” and “C”.For example if a “A” is found then increment CandidateTotals(1):CandidateTotals(1) = CandidateTotals(1) + 1

• Remember you are searching the Votes(….) array and incrementing the appropriate element of the CandidatesTotals array(….) when a “A”, “B” or “C” is found.

Page 26: 08/09/20151 7 Arrays Defining, Declaring & Processing.

Extension “Election” Program 2Extension “Election” Program 2

Compare the totals and declare the winner. CandidateTotals(1) for how many A’s,

CandidateTotals(2) for how many B’s and …..

Make sure you add a “Reset” button. Compare with the “Reset” button of the

Program 7.1 Number Array (slide 17 of this presentation).

Page 27: 08/09/20151 7 Arrays Defining, Declaring & Processing.

272719/04/2319/04/23

Extension “Letter Tally” Program 3Extension “Letter Tally” Program 3

Write a program which will keep a tally of Write a program which will keep a tally of the number of times each letter appears in the number of times each letter appears in a given text (can copied and pasted in).a given text (can copied and pasted in).

Use an array of size 26 to store the totals Use an array of size 26 to store the totals for each letter. for each letter.

Make sure you Make sure you initialiseinitialise each element of each element of the array.the array.

See next slides for some hints!See next slides for some hints!

Page 28: 08/09/20151 7 Arrays Defining, Declaring & Processing.

Extension “Letter Tally” Program 3Extension “Letter Tally” Program 3

Page 29: 08/09/20151 7 Arrays Defining, Declaring & Processing.

292919/04/2319/04/23

Extension “Letter Tally” Program 3Extension “Letter Tally” Program 3

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 the next slide.See the next slide.

Page 30: 08/09/20151 7 Arrays Defining, Declaring & Processing.

Extension “Letter Tally” Program 3Extension “Letter Tally” Program 3

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.

Continued on the next slide.Continued on the next slide.

Page 31: 08/09/20151 7 Arrays Defining, Declaring & Processing.

Extension “Letter Tally” Program 3Extension “Letter Tally” Program 3

To display, the long manual way would be to: lblLettera.Text = Letters(1) lblLetterb.Text = Letters(1) lblLetterc.Text = Letters(1) …

However, a quicker way (if you have placed 26 labels in a group box as shown in the picture on slide 28, named something like grpLetters) is: Index = 26 For Each Ctrl in grpLetters.Controls

Ctrl.Text = Letters(Index)Index = Index - 1

Next CtrlNote if you not have placed 26 labels in a group box as shown in the picture on slide 28 then either do so or use the long manual way above.

Page 32: 08/09/20151 7 Arrays Defining, Declaring & Processing.

323204/19/2304/19/23

Declaring 2D ArraysDeclaring 2D Arrays

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

1D Size1D Size 2D Size2D Size

11 22 33 4411

22

33

Page 33: 08/09/20151 7 Arrays Defining, Declaring & Processing.

333304/19/2304/19/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

Page 34: 08/09/20151 7 Arrays Defining, Declaring & Processing.

343404/19/2304/19/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 19911991..SalesFigures(2,0) = 56800SalesFigures(2,0) = 56800

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

SalesFigures(4,9) = SalesFigures(4,9) = 9640096400

5680056800

9640096400

11 22 33 4411

22

33

44

55

66

77

88

99

1010

Page 35: 08/09/20151 7 Arrays Defining, Declaring & Processing.

353519/04/2319/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.

Page 36: 08/09/20151 7 Arrays Defining, Declaring & Processing.

Extension “Store BIKE IDs” Program 4Extension “Store BIKE IDs” Program 4

Super Bikes owns a rectangular parking area with 30 rows; each row has 4 bike spaces.Each bike is always parked in the same space.The array BikeSpace[30,4] stores the bike registrations.Soni uses a flowchart to help him design a module to populate the array with the bike registrations.Input is terminated using the rogue value “BK000”.Write this program and allow the user to see the contents of the array when a button is clicked. Also include a “Reset” button.

Page 37: 08/09/20151 7 Arrays Defining, Declaring & Processing.

Extension “Store BIKE IDs” Program 4Extension “Store BIKE IDs” Program 4Hints: Use an input box for each input in your loops:

InputBox(“message”, “title”, defaultValue) You don’t have to have a title or a default value but you must

have a message. Display the array in a list box with one loop and each

row as a single item e.g.: For Row = 1 To 30

lstDisplay.Items.Add(BikeSpace(Row, 1) & “ “ & BikeSpace(Row, 2) & “ “ & BikeSpace(Row, 3) & “ “& BikeSpace(Row, 4) & “ “)

Next Row You can use any loop you wish but note that exams

may force you to use a specified type. Also note that the “Cancel” in the InputBox will not

work as it returns “” and as it is in a loop, it just enters “” into the array and then asks for the next BikeID.

Page 38: 08/09/20151 7 Arrays Defining, Declaring & Processing.

Extension “Chess Board” Program 5aExtension “Chess Board” Program 5a

Liliane wants to write a program to play chess.

She will represent the board of 4 x 4 squares, using a 2-dimensional array.

If a chess piece is on a square, it will take a value of 1.

Write a program to accept row and column numbers and place a 1 at this position and re-display the board.

See next slide for some hints.

Page 39: 08/09/20151 7 Arrays Defining, Declaring & Processing.

Extension “Chess Board” Program 5aExtension “Chess Board” Program 5aUse the form’s load event (double click the form) and create a loop like:

For Row = … To …lstBoard.Items.Add(ChessBoard(Row, 1) & “ “ & ChessBoard(Row, 2) & “ “ & ChessBoard(Row, 3) & “ “& ChessBoard(Row, 4) & “ “)

Next Row To display the initially empty board in a

label.

Also see the next slide.

Page 40: 08/09/20151 7 Arrays Defining, Declaring & Processing.

Extension “Chess Board” Program 5aExtension “Chess Board” Program 5a

Update the appropriate row and column position in the array to 1 when a chess piece is placed on a square.

Note that there is no need for an input box and nested loops here as the row and column entered will indicate where the 1 is to go in the ChessBoard array.

Then use lstBoard.Items.Clear to clear the label before re-displaying the chess board, otherwise it will appear as though nothing has happened.

Re-display the array with the same loop described on the previous slide.

Don’t forget the “Reset” button.

Page 41: 08/09/20151 7 Arrays Defining, Declaring & Processing.

Extension “Chess Board” Program 5bExtension “Chess Board” Program 5b

Liliane's next task is to indicate that there are pieces occupying the first two rows of the 4 x 4 board.

Each square in rows 1 and 2 will be given the value 1.

Add a “Start” button that occupies the first two rows of the 4 x 4 board with 1’s and clears the other rows.

Page 42: 08/09/20151 7 Arrays Defining, Declaring & Processing.

Extension “Chess Board” Program 5bExtension “Chess Board” Program 5b

Rewrite the program using a DO While Loops (as you probably used For To Next Loops originally).

Page 43: 08/09/20151 7 Arrays Defining, Declaring & Processing.

434319/04/2319/04/23

PlenaryPlenary

What is an array and an element?What is an array and an element? Array:Array:

A data structure that stores as many items as required using A data structure that stores as many items as required using a single variable.a single variable.

Element:Element:A storage ‘slot’ in an array.A storage ‘slot’ in an array.

Why arrays are useful and so when they should be Why arrays are useful and so when they should be used?used? Useful because they avoid the need to declare a Useful because they avoid the need to declare a

variable for each item of data.variable for each item of data. Should be used when you want to store more than a Should be used when you want to store more than a

few items of data of the same type.few items of data of the same type.

Page 44: 08/09/20151 7 Arrays Defining, Declaring & Processing.

444419/04/2319/04/23

PlenaryPlenary

How do we declare an array?How do we declare an array?

Page 45: 08/09/20151 7 Arrays Defining, Declaring & Processing.

454519/04/2319/04/23

How toHow to declare declare anan array? array?

Similar to variables.Similar to variables.Dim …(…) As …Dim …(…) As …

Array nameArray name

The number of elements required.The number of elements required.

Data TypeData Type

Page 46: 08/09/20151 7 Arrays Defining, Declaring & Processing.

464619/04/2319/04/23

PlenaryPlenary

How do we process an array i.e. use it How do we process an array i.e. use it (store or retrieve data)?(store or retrieve data)?

How are array elements numbered?How are array elements numbered?

Page 47: 08/09/20151 7 Arrays Defining, Declaring & Processing.

474719/04/2319/04/23

Processing Processing anan Array Array

……(…) =(…) =

Array NameArray Name A number or a variableA number or a variable

(with a stored number) (with a stored number)

representing the requiredrepresenting the required

element. element.

e.g.e.g. ExamMarks(3) = MarkExamMarks(3) = Mark Will store the contents of the Will store the contents of the MarkMark variable in variable in

the the 33rdrd element of the element of the ExamMarksExamMarks array. array.