Presentation is loading. Please wait.

Presentation is loading. Please wait.

PROGRAMMING ARRAYS.

Similar presentations


Presentation on theme: "PROGRAMMING ARRAYS."— Presentation transcript:

1 PROGRAMMING ARRAYS

2 Objectives Understand different ways of organising data within programming. Know what is meant by an array. Begin using arrays in your programming.

3 The Problem So far we have seen one way of storing data within our programs by separate variables of different types. if we had 100 customers, we would still need 100 variables (customer1, customer2, customer3, customer4…customer100). What if customer 101 comes along? Your client would not be able to add more variables! To overcome this and aid organisation, we use ARRAYS.

4 One-dimensional Arrays
Imagine then if we could take a variable split it into a number of boxes. If we needed 100 customers, we can simply create 100 little boxes within a box that is, an array. We would have 100 boxes, all with the same name and a different index value. E.g. customer(0), customer(1), customer(2), customer(3)…customer(100).

5 An Example The array has the name arrCustomers
arrCustomers(100) arrCustomers John Jean Kim The array has the name arrCustomers It was declared in a similar way to a normal variable: Dim arrCustomers(100) as string It has 100 slots in memory. Each slot has it’s own INDEX value. IMPORTANT: Note how VB starts from 0 (not 1), for the first item in the list. Items are referenced in a similar way: arrCustomers(1) = “Jean” console.writeline(arrCustomers(0))

6 Using Arrays in Parallel
arrCustomers(0) … arrCustomers(100)are the elements of arrCustomers. arrBalance(0) … arrBalance(100) are the elements of the array arrBalance. (0)…(1)…(100) this is the INDEX value, i.e. the unique reference to that slot in the array. console.writeline(arrCustomers(0)) would display “John” on the screen. console.writeline(arrCustomers(100)) would display “Kim” on the screen. console.writeline(arrCustomers(0) & “ “ & formatCurrency(arrBalance(0)) would display “John £12.54” arrBalance(100) = would assign the value to arrBalance, item 100.

7 Declaring ONE Dimensional Array
Dim arrName(size) As elementType Examples: Dim weeklyRent(52) As Single Dim mySalary(2000)As Integer

8 Why bother using arrays?
Are all of the data to be stored related? The data in the array must need to be stored You intend to do something with the data. It is more efficient to than use a lot of variables.

9 Two Dimensional Arrays
So far we have seen that arrays are like a column of data which can store a set of values within one variable. If you wanted to store related data then you could have separate arrays. For example you could have an array for: Football Team Names STRINGS Matches Played INTEGER Matches Won INTEGER Matches Drawn INTEGER Matches Lost INTEGER Points INTEGER

10 Thinking more into it Having an array for each of these would be rather large and would take us back a step in efficiency and structured programming. If you stored it on paper, you would store it like this: Well, so would a computer, with the added INDEXing system, starting from ZERO. This is called a TWO DIMENSIONAL ARRAY.

11 Declaring a 2D Array Dim ArrayName(Rows, Columns) AS type For example:
Dim football(3,5) as string would create the table above (remember we start from zero).

12 Using a 2D Array Example: Football(0,1) = “Middlesbrough”
Football(0,2) = “Aresenal” Football(0,3) = “Manchester United”) Football(1,1) = “20” note we put it in quotes as it is a STRING array. Football(2,1) = “15”


Download ppt "PROGRAMMING ARRAYS."

Similar presentations


Ads by Google