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.

Post on 21-Dec-2015

212 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

CP1020 - Week 9

Working with arrays

What are we doing this week ?

How to store information in an arrayHow to retrieve information from an arrayA few examples using multidimensional

arrays

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 names2. input name3. next name

number of name

The code

DIM asNames(3) AS STRINGDIM iCount AS INTEGER

FOR iCount = 0 TO 3

NEXT iCountINPUT “ Enter a name > ”; asNames(iCount)

Lets extend this to store the names of four families

John Jill Gordon LouiseBill Sue Rita ChrisCyril Ranjit Celine KenJune Peter David Alison

We need a multi-dimensional array to store the family names. Each column belongs to one family

1

2

3

4member

1 2 3 4family

The design

1. For families 0 to 3

2. Next family

1.1 for family member 0 to 3

1.2 next family member1.1.1 get name

The code

DIM asName(3,3) AS STRINGDIM iFamName AS INTEGERDim 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)

A second example

We 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

Treasure map - design

1) Get three treasure co-ordinates1.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

The code

DIM asTreasure(1 TO 3, 1 TO 3) AS STRINGDIM iCount, iRow, iCol AS INTEGER

FOR iCount = 1 TO 3INPUT “What are your treasure co-ordinates “; iRow; iColasTreasure(iRow, iCol) = “X”

NEXT iCount

The design to display the map

We would also like to display the map on the screen

2) Display map2.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 line2.2 next row

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

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

John Rita Leeroy

21 32 101

asNames

aiAge

The design

1. For person 0 to 2

2. Next person

1.1. Get name1.2. Get age

The code

DIM asName(2) AS STRINGDIM aiAge(2) AS INTEGERDIM iCount AS INTEGER

FOR iCount = 0 to 2

NEXT iCount

INPUT “persons name >”; asName(iCount)INPUT “persons age >”; aiAge(iCount)

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 design1. For 5 foods

1.1 get position 1.2 get name of food

2. next food

The code

DIM asFood(4) AS STRINGDIM iPosition AS INTEGERDIM 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

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 design1 while positions < 5

1.1 get name of food1.2 get position1.3 if position is empty enter data

2 loop

The code

DIM asFood(4) AS STRINGDIM iPosition AS INTEGERDIM 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

ElsePRINT “this position has already been filled! Try again”

END IFLOOP

Summary

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

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

top related