CP 1020 Week 8 Arrays CP 1020 Principles

  • Slides: 23
Download presentation
CP 1020 - Week 8 Arrays CP 1020 - Principles of Programming Steve Garner

CP 1020 - Week 8 Arrays CP 1020 - Principles of Programming Steve Garner and Ian Coulson

Aims and Objectives z. Understand what an array is z. Be able to decide

Aims and Objectives z. Understand what an array is z. Be able to decide when to use an array z. Be able declare an array z. Be able to write data into and retrieve data from an array CP 1020 - Principles of Programming Steve Garner and Ian Coulson

The Problem with Variables z. A variable can contain data relating to a single

The Problem with Variables z. A variable can contain data relating to a single characteristic of an object s. Surname = "John Smith" i. Employee. Number = 123456 z. . . but in most cases we want to refer to collections of data, e. g. all employees in the company CP 1020 - Principles of Programming Steve Garner and Ian Coulson

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

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. CP 1020 - Principles of Programming Steve Garner and Ian Coulson

The solution! z An array is like having a variable with more than one

The solution! z An array is like having a variable with more than one compartment to store one piece of data in each. as. Name(1) = “Fred” as. Name(2) = “Julie” as. Name (1) Fred as. Name(3) = “Kim” as. Name(4) = “John” as. Name(5) = “Chris” (2) Julie (3) Kim (4) John (5) Chris Array name element number data stored in that element CP 1020 - Principles of Programming Steve Garner and Ian Coulson

Arrays Hold Collections of Variables z. The elements in an array variable can only

Arrays Hold Collections of Variables z. 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 yactually you can re-dimension arrays, z. Each element in the array has a unique address - it’s name plus it’s element number CP 1020 - Principles of Programming Steve Garner and Ian Coulson

Defining an Array We use the standard keywords used to declare variables: Dim We

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 ai. Counters(14) As Integer Dim as. Names(5) As String CP 1020 - Principles of Programming Steve Garner and Ian Coulson

Accessing an Array z. We need to be able to address the individual elements

Accessing an Array z. We need to be able to address the individual elements in an array z. We use the array name and the element number to access it Dim as. Name(5) As String as. Name (1) “Jane” (2) “Pete” as. Name(2) = “Pete” (3) “Lucy” (4) “Dave” as. Name(3) = “Lucy” INPUT “enter a name “; as. Name(4) CP 1020 - Principles of Programming Steve Garner and Ian Coulson (5) “Ian”

How big is an array? Dim s. Names(2) As String will create an array

How big is an array? Dim s. Names(2) As String will create an array that has 3 elements s. Name(0) Lowest element s. Name(1) s. Name(2) Be careful! By default QBasic sets the lowest array element as 0, so in our case this means we have three elements CP 1020 - Principles of Programming Steve Garner and Ian Coulson

Array Bounds & Option Base z. The bounds are the ‘size’ of an array

Array Bounds & Option Base z. The bounds are the ‘size’ of an array z. Arrays have a lower address or lower bound, and an upper address or upper bound z. We can alter the default lower bound by using Option Base CP 1020 - Principles of Programming Steve Garner and Ian Coulson

Option base z. Option Base can be set to either 0 or 1 y.

Option base z. Option Base can be set to either 0 or 1 y. Option Base 0 0 y. Option Base 1 1 ‘sets the lower bound to ‘ Option Base 0 z. Dim as. Name(5) As String as. Name 0 1 2 3 4 as. Name 1 2 3 4 5 ‘ Option Base 1 CP 1020 - Principles of Programming Steve Garner and Ian Coulson

Using Arrays in Applications z. We can use variables and expressions to determine the

Using Arrays in Applications z. We 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 CP 1020 - Principles of Programming Steve Garner and Ian Coulson

The programme Dim as. Names(4) As String Dim i. Count As Integer For i.

The programme Dim as. Names(4) As String Dim i. Count As Integer For i. Count = 0 To 4 Input “Enter a name >”; as. Names(i. Count) Next i. Count For i. Count = 4 To 0 Step -1 Print as. Names(i. Count) Next i. Count CP 1020 - Principles of Programming Steve Garner and Ian Coulson

Explicitly Setting Array Bounds z We can just declare how many elements we want

Explicitly Setting Array Bounds z We can just declare how many elements we want in our array and QBasic will set them up using the Option Base setting: Dim as. Name(5) As String Option Base 1 as. Name 1 2 3 4 5 z We can however state explicitly the lower and upper bounds that we want for the array Dim as. Name(5 To 9) As String as. Name 5 6 7 8 CP 1020 - Principles of Programming Steve Garner and Ian Coulson 9

Declaring Multi-Dimension Arrays Consider a chess board, it has 64 squares, how would we

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 as. Board(64) As String ‘assumes option base 1 That isn't very obvious is it? Where on the board is as. Board(12), is it in the middle or on an edge? CP 1020 - Principles of Programming Steve Garner and Ian Coulson

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

z. A better solution would be to declare it as a multidimensional array Dim as. Board(8, 8) As String ‘assumes option base 1 or explicitly Dim as. Board(1 To 8, 1 To 8) As String CP 1020 - Principles of Programming Steve Garner and Ian Coulson

Using Multidimensional Arrays z. Using our declaration Dim as. Board(1 To 8, 1 To

Using Multidimensional Arrays z. Using our declaration Dim as. Board(1 To 8, 1 To 8) As String as. Board 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 as. Board(6, 3) 5, 1 5, 2 5, 3 5, 4 5, 5 5, 6 5, 7 5, 8 as. Board(3, 7) 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 z We have a much clearer idea of where on the board the array variable points to CP 1020 - Principles of Programming Steve Garner and Ian Coulson

Where To Use Multidimensional Arrays z. Qbasic will allow us multiple dimensions! What about

Where To Use Multidimensional Arrays z. Qbasic will allow us multiple dimensions! What about a three dimensional chess board? Dim as. Board(1 To 8, 1 To 8) As String as. Board(1, 4, 3) = “White Knight” CP 1020 - Principles of Programming Steve Garner and Ian Coulson

Multidimensional arrays z. We would advise you not to use too many dimensions, otherwise

Multidimensional arrays z. We would advise you not to use too many dimensions, otherwise you will become confused z. Best idea is only to use multidimensional arrays where they map clearly to the real-world CP 1020 - Principles of Programming Steve Garner and Ian Coulson

Clearing Arrays z. To clear a complete array you can use the Erase command:

Clearing Arrays z. To clear a complete array you can use the Erase command: Erase as. Names z. This resets all fields to their ‘null’ values E. g. all numerical values are set to 0 and strings to be empty CP 1020 - Principles of Programming Steve Garner and Ian Coulson

Finding Array Boundaries z. To find the bounds of a single dimension array we

Finding Array Boundaries z. To find the bounds of a single dimension array we can use: i. Lower. Bound = LBound( as. Names ) i. Upper. Bound = UBound( as. Names ) z. To find multi-dimensional bounds: Upper. Bound = UBound( as. Address, 2 ) CP 1020 - Principles of Programming Steve Garner and Ian Coulson

Summary z. Be able to decide when to use an array z. Be able

Summary z. Be able to decide when to use an array z. Be able to set up an array z. Be able to write data to and retrieve data from an array CP 1020 - Principles of Programming Steve Garner and Ian Coulson

Review questions 1 write a Dim statement to create an array to store the

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 10 th element of your array 3 The name Alan Shearer is stored in the 71 st element, write a statement to print this out Return to view another lecture CP 1020 - Principles of Programming Steve Garner and Ian Coulson