1 / 23

CP1020 - Week 8

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.

linnea
Download Presentation

CP1020 - Week 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CP1020 - Week 8 Arrays

  2. 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

  3. 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

  4. 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. (1) Fred asName (2) Julie (3)Kim (4) John (5) Chris Array nameelement numberdata stored in that element 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”

  6. 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

  7. 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 itDim aiCounters(14) As IntegerDim asNames(5) As String

  8. (1) “Jane” asName (2) “Pete” (3) “Lucy” (4) “Dave” (5) “Ian” Accessing an Array • We need to be able to address the individual elements in an array • We use the array name and the element number to access itDim asName(5) As StringasName(2) = “Pete”asName(3) = “Lucy”INPUT “enter a name “;asName(4)

  9. Lowest element 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

  10. Array Bounds & Option Base • The bounds are the ‘size’ of an array • Arrays 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

  11. ‘ Option Base 0 0 1 2 3 4 asName 1 2 3 4 5 asName ‘ Option Base 1 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

  12. 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

  13. 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. asName Option Base 1 1 2 3 4 5 5 6 7 8 9 asName 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 • We can however state explicitly the lower and upper bounds that we want for the arrayDim asName(5 To 9) As String

  15. 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. A better solution would be to declare it as a multidimensional arrayDim asBoard(8,8) As String ‘assumes option base 1 or explicitly Dim asBoard(1 To 8, 1 To 8) As String

  17. asBoard 1,1 1,2 1,3 1,4 1,5 1,6 1,7 1,8 2,1 2,2 2,3 2,4 2,5 2,6 2,7 2,8 3,1 3,2 3,3 3,4 3,5 3,6 3,7 3,8 4,1 4,2 4,3 4,4 4,5 4,6 4,7 4,8 asBoard(6,3) asBoard(3,7) 5,1 5,2 5,3 5,4 5,5 5,6 5,7 5,8 6,1 6,2 6,3 6,4 6,5 6,6 6,7 6,8 7,1 7,2 7,3 7,4 7,5 7,6 7,7 7,8 8,1 8,2 8,3 8,4 8,5 8,6 8,7 8,8 Using Multidimensional Arrays • Using our declarationDim asBoard(1 To 8, 1 To 8) As String • We have a much clearer idea of where on the board the array variable points to

  18. 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”

  19. 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

  20. 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

  21. 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 )

  22. 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

  23. 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

More Related