Download presentation
Presentation is loading. Please wait.
1
CP1020 - Principles of Programming Steve Garner and Ian Coulson CP1020 - Week 8 Arrays
2
CP1020 - Principles of Programming Steve Garner and Ian Coulson Aims and Objectives zUnderstand what an array is zBe able to decide when to use an array zBe able declare an array zBe able to write data into and retrieve data from an array
3
CP1020 - Principles of Programming Steve Garner and Ian Coulson The Problem with Variables zA variable can contain data relating to a single characteristic of an object sSurname = "John Smith" iEmployeeNumber = 123456 z...but in most cases we want to refer to collections of data, e.g. all employees in the company
4
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.
5
CP1020 - Principles of Programming Steve Garner and Ian Coulson The solution! zAn 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
6
CP1020 - Principles of Programming Steve Garner and Ian Coulson Arrays Hold Collections of Variables zThe 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 yactually you can re-dimension arrays, zEach element in the array has a unique address - it’s name plus it’s element number
7
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 Integer Dim asNames(5) As String
8
CP1020 - Principles of Programming Steve Garner and Ian Coulson Accessing an Array zWe need to be able to address the individual elements in an array zWe 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
9
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
10
CP1020 - Principles of Programming Steve Garner and Ian Coulson Array Bounds & Option Base zThe bounds are the ‘size’ of an array zArrays have a lower address or lower bound, and an upper address or upper bound zWe can alter the default lower bound by using Option Base
11
CP1020 - Principles of Programming Steve Garner and Ian Coulson Option base zOption Base can be set to either 0 or 1 yOption Base 0 ‘sets the lower bound to 0 yOption Base 1 ‘sets the lower bound to 1 zDim asName(5) As String ‘ Option Base 1 ‘ Option Base 0 asName 01234 51234
12
CP1020 - Principles of Programming Steve Garner and Ian Coulson Using Arrays in Applications zWe can use variables and expressions to determine the value of an array element zthis is illustrated in this simple programme which stores up to 5 names then prints them out in reverse order
13
CP1020 - Principles of Programming Steve Garner and Ian Coulson The programme Dim asNames(4) As String Dim iCount As Integer For iCount = 0 To 4 Input “Enter a name >”; asNames(iCount) Next iCount For iCount = 4 To 0 Step -1 Print asNames(iCount) Next iCount
14
CP1020 - Principles of Programming Steve Garner and Ian Coulson Explicitly Setting Array Bounds zWe 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 5 1234 6 57 8 9 zWe can however state explicitly the lower and upper bounds that we want for the array Dim asName(5 To 9) As String
15
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?
16
CP1020 - Principles of Programming Steve Garner and Ian Coulson zA 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
17
CP1020 - Principles of Programming Steve Garner and Ian Coulson Using Multidimensional Arrays zUsing our declaration Dim asBoard(1 To 8, 1 To 8) As String zWe have a much clearer idea of where on the board the array variable points to
18
CP1020 - Principles of Programming Steve Garner and Ian Coulson Where To Use Multidimensional Arrays zQbasic 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”
19
CP1020 - Principles of Programming Steve Garner and Ian Coulson Multidimensional arrays zWe would advise you not to use too many dimensions, otherwise you will become confused zBest idea is only to use multidimensional arrays where they map clearly to the real-world
20
CP1020 - Principles of Programming Steve Garner and Ian Coulson Clearing Arrays zTo clear a complete array you can use the Erase command: Erase asNames zThis resets all fields to their ‘null’ values E.g. all numerical values are set to 0 and strings to be empty
21
CP1020 - Principles of Programming Steve Garner and Ian Coulson Finding Array Boundaries zTo find the bounds of a single dimension array we can use : iLowerBound = LBound( asNames ) iUpperBound = UBound( asNames ) zTo find multi-dimensional bounds: UpperBound = UBound( asAddress, 2 )
22
CP1020 - Principles of Programming Steve Garner and Ian Coulson Summary zBe able to decide when to use an array zBe able to set up an array zBe able to write data to and retrieve data from an array
23
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.