Top Banner
CP1020 - Week 9 Working with arrays
21

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.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

CP1020 - Week 9

Working with arrays

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

What are we doing this week ?

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

arrays

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

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

Page 4: 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.

The code

DIM asNames(3) AS STRINGDIM iCount AS INTEGER

FOR iCount = 0 TO 3

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

Page 5: 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.

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

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

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

Page 7: 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.

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)

Page 8: 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.

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

Page 9: 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.

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

Page 10: 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.

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

Page 11: 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.

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

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

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

Page 13: 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.

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

Page 14: 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.

The design

1. For person 0 to 2

2. Next person

1.1. Get name1.2. Get age

Page 15: 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.

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)

Page 16: 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.

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

Page 17: 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.

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

Page 18: 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.

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

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

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

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

Summary

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

Page 21: 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.

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