Presentation is loading. Please wait.

Presentation is loading. Please wait.

CP1020 - Week 9 Working with arrays. What are we doing this week ? zHow to store information in an array zHow to retrieve information from an array zA.

Similar presentations


Presentation on theme: "CP1020 - Week 9 Working with arrays. What are we doing this week ? zHow to store information in an array zHow to retrieve information from an array zA."— Presentation transcript:

1 CP1020 - Week 9 Working with arrays

2 What are we doing this week ? zHow to store information in an array zHow to retrieve information from an array zA few examples using multidimensional arrays

3 Use of a for loop to write to an array Suppose we want to store a series of names in an array. 0 1 2 3 John Jill Gordon Louise The design: 1. for 0 to 3 names 2. input name 3. next name number of name

4 The code DIM asNames(3) AS STRING DIM iCount AS INTEGER FOR iCount = 0 TO 3 NEXT iCount INPUT “ Enter a name > ”; asNames(iCount)

5 Lets extend this to store the names of four families JohnJillGordonLouise BillSueRitaChris CyrilRanjitCelineKen JunePeterDavidAlison We need a multi-dimensional array to store the family names. Each column belongs to one family 1 2 3 4 member 1 2 3 4 family

6 The design 1. For families 0 to 3 2. Next family 1.1 for family member 0 to 3 1.2 next family member 1.1.1 get name

7 The code DIM asName(3,3) AS STRING DIM iFamName AS INTEGER Dim iFamMember AS INTEGER FOR iFamName = 0 TO 3 NEXT iFamName PRINT “enter the names of this family “ FOR iFamMember = 0 TO 3 NEXT iFamMember INPUT asName(iFamName, iFamMember)

8 A second example zWe wish to store the positions of Xs on a treasure map. The map is split into a grid of 3 columns and 3 rows. There are 3 treasures hidden. X X X

9 Treasure map - design 1) Get three treasure co-ordinates 1.1 For iTreasure = 1 to 3 1.2 next co-ordinates 1.1.1 get row and column co-ordinates 1.1.2 store X in the array at those positions

10 The code DIM asTreasure(1 TO 3, 1 TO 3) AS STRING DIM iCount, iRow, iCol AS INTEGER FOR iCount = 1 TO 3 INPUT “What are your treasure co-ordinates “; iRow; iCol asTreasure(iRow, iCol) = “X” NEXT iCount

11 The design to display the map zWe would also like to display the map on the screen 2) Display map 2.1 For Row = 1 to 3 2.1.1 For Column = 1 to 3 2.1.2 if array element = X print it else print a blank space 2.1.3 next column 2.1.4 move to next line 2.2 next row

12 The code to display the map FOR iRow = 1 TO 3 FOR iCol = 1 TO 3 IF asTreasure(iRow, iCol) = “X” PRINT asTreasure(iRow, iCol) ; ELSE PRINT “ “; ENDIF NEXT iCol PRINT NEXT iRow

13 Using another array to store corresponding information Suppose we want to store the ages of each family member we could use a second array of the same dimension. We can't use the same array as they are of different data types JohnRita Leeroy 21 32 101 asNames aiAge

14 The design 1. For person 0 to 2 2. Next person 1.1. Get name 1.2. Get age

15 The code DIM asName(2) AS STRING DIM aiAge(2) AS INTEGER DIM iCount AS INTEGER FOR iCount = 0 to 2 NEXT iCount INPUT “persons name >”; asName(iCount) INPUT “persons age >”; aiAge(iCount)

16 Choose an array position Suppose we want to store data in a particular place in array. E.g. we want to store our favourite foods in order of preference. The design 1. For 5 foods 1.1 get position 1.2 get name of food 2. next food

17 The code DIM asFood(4) AS STRING DIM iPosition AS INTEGER DIM iCount AS INTEGER For iCount = 0 to 4 INPUT “The order for your preference 0 to 4”; iPosition INPUT “Your food ”; asFood(iPosition) Next iCount

18 An improvement What happens if the position you want to write to in the array already has data written in. We must test to see if the array element is empty and then write to it. The design 1 while positions < 5 1.1 get name of food 1.2 get position 1.3 if position is empty enter data 2 loop

19 The code DIM asFood(4) AS STRING DIM iPosition AS INTEGER DIM iCount AS INTEGER DO WHILE iCount <5 INPUT “The order for your preference”; iPosition IF asFood(iPosition) = “” THEN INPUT “Your food ”; asFood(iPosition) iCount = iCount + 1 Else PRINT “this position has already been filled! Try again” END IF LOOP

20 Summary zWe have looked at one of the most important aspects of arrays - using loops to increment through them

21 Review questions 1 Write a piece of code that will allow the user to store their five favourite holiday destinations Return to view another lecture


Download ppt "CP1020 - Week 9 Working with arrays. What are we doing this week ? zHow to store information in an array zHow to retrieve information from an array zA."

Similar presentations


Ads by Google