CP1020 - Week 8

23
CP1020 - Principles of Programming Steve Garner and Ian Coulson CP1020 - Week 8 Arrays

description

CP1020 - Week 8. Arrays. Aims and Objectives. Understand what an array is Be able to decide when to use an array Be able declare an array Be able to write data into and retrieve data from an array. The Problem with Variables. - PowerPoint PPT Presentation

Transcript of CP1020 - Week 8

Page 1: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

CP1020 - Week 8

Arrays

Page 2: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

Aims and Objectives

Understand what an array is

Be able to decide when to use an array

Be able declare an array

Be able to write data into and retrieve data from an array

Page 3: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

The Problem with Variables

A variable can contain data relating to a single characteristic of an object

sSurname = "John Smith"iEmployeeNumber = 123456

...but in most cases we want to refer to collections of data,e.g. all employees in the company

Page 4: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

The problem

For instance we want to store in variables a collection of names:

Fred; Julie;

Kim; John;

Chris.

This would require 5 dim statements with 5 variable names! The problem increases with increasing data to store.

Page 5: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

The solution!

An array is like having a variable with more than one compartment to store one piece of data in each.

asName(1) = “Fred”asName(2) = “Julie”

asName(3) = “Kim”asName(4) = “John”asName(5) = “Chris”

(1) Fred

(3) Kim

(2) Julie

(4) John

(5) Chris

asName

Array name element number data stored in that element

Page 6: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

Arrays Hold Collections of Variables

The elements in an array variable can only be of the same data type.The size of the array has to be set when the array variable is declared actually you can re-dimension arrays,

Each element in the array has a unique address - it’s name plus it’s element number

Page 7: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

Defining an Array

We use the standard keywords used to declare variables:

Dim

We need to say what the size of the array is when we declare it

Dim aiCounters(14) As IntegerDim asNames(5) As String

Page 8: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

Accessing an ArrayWe need to be able to address the individual

elements in an arrayWe use the array name and the element number

to access it

Dim asName(5) As String

asName(2) = “Pete”

asName(3) = “Lucy”INPUT “enter a name “; asName(4)

(1) “Jane”

(3) “Lucy”

(2) “Pete”

(4) “Dave”

(5) “Ian”

asName

Page 9: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

How big is an array?Dim sNames(2) As String will

create an array that has 3 elements

sName(0)

sName(1)

sName(2)

Be careful! By default QBasic sets the lowest array element as 0, so in our case this means we have three elements

Lowest element

Page 10: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

Array Bounds & Option Base

The bounds are the ‘size’ of an arrayArrays have a lower address or lower

bound, and an upper address or upper bound

We can alter the default lower bound by using Option Base

Page 11: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

Option base

Option Base can be set to either 0 or 1 Option Base 0 ‘sets the lower bound to 0 Option Base 1 ‘sets the lower bound to 1

Dim asName(5) As String

‘ Option Base 1

‘ Option Base 0asName 0 1 2 3 4

asName 51 2 3 4

Page 12: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

Using Arrays in Applications

We can use variables and expressions to determine the value of an array element this is illustrated in this simple programme which stores up to 5 names then

prints them out in reverse order

Page 13: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

The programme

Dim asNames(4) As StringDim iCount As Integer

For iCount = 0 To 4Input “Enter a name >”; asNames(iCount)

Next iCount

For iCount = 4 To 0 Step -1Print asNames(iCount)

Next iCount

Page 14: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

Explicitly Setting Array Bounds

We can just declare how many elements we want in our array and QBasic will set them up using the Option Base setting:

Dim asName(5) As String

Option Base 1 asName 51 2 3 4

asName 65 7 8 9

We can however state explicitly the lower and upper bounds that we want for the arrayDim asName(5 To 9) As String

Page 15: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

Declaring Multi-Dimension Arrays

Consider a chess board, it has 64 squares, how would we define an array variable to hold the pieces that are on the board?

Dim asBoard(64) As String

‘assumes option base 1

That isn't very obvious is it? Where on the board is asBoard(12), is it in the middle or on an edge?

Page 16: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

A better solution would be to declare it as a multidimensional array

Dim asBoard(8,8) As String ‘assumes option base 1

or explicitly

Dim asBoard(1 To 8, 1 To 8) As String

Page 17: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

Using Multidimensional ArraysUsing our declaration

Dim asBoard(1 To 8, 1 To 8) As String

1,1 1,81,71,61,51,41,31,2

2,1 2,82,72,62,52,42,32,2

3,1 3,83,73,63,53,43,33,2

4,1 4,84,74,64,54,44,34,2

5,1 5,85,75,65,55,45,35,2

6,1 6,86,76,66,56,46,36,2

7,1 7,87,77,67,57,47,37,2

8,1 8,88,78,68,58,48,38,2

asBoard

asBoard(3,7)asBoard(6,3)

We have a much clearer idea of where on the board the array variable points to

Page 18: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

Where To Use Multidimensional Arrays

Qbasic will allow us multiple dimensions!

What about a three dimensional chess board?

Dim asBoard(1 To 8, 1 To 8, 1 To 8) As String

asBoard(1,4,3) = “White Knight”

Page 19: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

Multidimensional arrays

We would advise you not to use too many dimensions, otherwise you will become confused

Best idea is only to use multidimensional arrays where they map clearly to the real-world

Page 20: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

Clearing Arrays

To clear a complete array you can use the Erase command:

Erase asNames

This resets all fields to their ‘null’ values

E.g. all numerical values are set to 0 and strings to be empty

Page 21: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

Finding Array Boundaries

To find the bounds of a single dimension array we can use: iLowerBound = LBound( asNames )

iUpperBound = UBound( asNames )

To find multi-dimensional bounds:UpperBound = UBound( asAddress, 2 )

Page 22: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

Summary

Be able to decide when to use an array

Be able to set up an array

Be able to write data to and retrieve data from an array

Page 23: CP1020 - Week 8

CP1020 - Principles of Programming Steve Garner and Ian Coulson

Review questions

1 write a Dim statement to create an array to store the names of 100 famous footballers

2 write the statement that would assign the name “Michael Owen” into the 10th element of your array

3 The name Alan Shearer is stored in the 71st element, write a statement to print this out

Return to view another lecture