18/12/20151 5.2 Arrays 2D Arrays Defining, Declaring & Processing.
-
Upload
pearl-holland -
Category
Documents
-
view
236 -
download
0
Transcript of 18/12/20151 5.2 Arrays 2D Arrays Defining, Declaring & Processing.
04/21/2304/21/23 11
5.2 Arrays5.2 Arrays
2D Arrays2D Arrays
Defining, Declaring & ProcessingDefining, Declaring & Processing
2204/21/2304/21/23
Learning ObjectivesLearning Objectives
Explain the concept of 2D arrays.Explain the concept of 2D arrays.
Explain how to declare and reset them.Explain how to declare and reset them.
3304/21/2304/21/23
Declaring 2D ArraysDeclaring 2D Arrays
Dim …(…, …) As …Dim …(…, …) As …
1D Size1D Size 2D Size2D Size
11 22 33 4411
22
33
4404/21/2304/21/23
2D Arrays2D Arrays
e.g. a firm’s quarterly sales figures for the years e.g. a firm’s quarterly sales figures for the years 1990 – 1999.1990 – 1999. 4 (quarters) x 10 (years) = 40 items of data4 (quarters) x 10 (years) = 40 items of data
Dim SalesFigures(4, 10) As DecimalDim SalesFigures(4, 10) As Decimal
5504/21/2304/21/23
2D Arrays2D Arrays
Each row is a year.Each row is a year.
Each column is a Each column is a quarter.quarter.
e.g.e.g. Sales was Sales was €€5680056800 in in
the the 33rdrd quarter of quarter of 19911991..SalesFigures(2,0) = 56800SalesFigures(2,0) = 56800
Sales was Sales was €€9640096400 in in the the 44thth quarter of quarter of 19991999..
SalesFigures(4,9) = SalesFigures(4,9) = 9640096400
5680056800
9640096400
11 22 33 4411
22
33
44
55
66
77
88
99
1010
6604/21/2304/21/23
Uses for 2D ArraysUses for 2D Arrays
Useful for storing data for some mathematical Useful for storing data for some mathematical problems.problems.
Limited use for ‘Limited use for ‘businessbusiness’ type problems ’ type problems because all data items have to be of the same because all data items have to be of the same type.type.
Resetting 2D arraysResetting 2D arrays
To reset 2D arrays you will need to 2 To reset 2D arrays you will need to 2 nested loops to loop through each column nested loops to loop through each column and each row.and each row.
For Column = 1 To ….For Column = 1 To ….For Row =1 To ….For Row =1 To ….
Array (Column, Row) = ….Array (Column, Row) = ….
Next RowNext Row Next ColumnNext Column
7704/21/2304/21/23
Pseudocode will use the following structures:
DECLARE <identifier> : ARRAY[<lbound>:<ubound>] OF <datatype>
So for an array of elements numbered from 1 – 10:
DECLARE <identifier> : ARRAY[<1>:<10>] OF <datatype>
DECLARE <identifier> : ARRAY[<lbound1>:<ubound1>,[<lbound>:<ubound2>] OF <datatype>
Extension “Store BIKE IDs” Program 5.2aExtension “Store BIKE IDs” Program 5.2a
Super Bikes owns a rectangular parking area with 30 rows; each row has 4 bike spaces.Each bike is always parked in the same space.The array BikeSpace[30,4] stores the bike registrations.Soni uses a flowchart to help him design a module to populate the array with the bike registrations.Input is terminated using the rogue value “BK000”.Write this program and allow the user to see the contents of the array. Also include a “Reset” option.
Extension “Chess Board1” Program 5.2bExtension “Chess Board1” Program 5.2b
Liliane wants to write a program to play chess.
She will represent the board of 4 x 4 squares, using a 2-dimensional array.
If a chess piece is on a square, it will take a value of 1.
Write a program to accept row and column numbers and place a 1 at this position and re-display the board.
To display the initially empty board:Remember to use:
Console.Write(…) Console.WriteLine()
As appropriate and as used in presentation 3.2,
Allow a Reset option.
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Extension “Chess Board2” Program 5.2cExtension “Chess Board2” Program 5.2c
Produce a different version of the previous “Chess Board1” program.
Liliane's next task is to indicate that there are pieces occupying the first two rows of the 4 x 4 board.
Each square in rows 1 and 2 will be given the value 1.
Add initial code that occupies the first two rows of the 4 x 4 board with 1’s and clears the other rows. This is all this version needs to do.
Extension “Chess Board3” Program Extension “Chess Board3” Program 5d5d
Produce a new version of the previous “Chess Board2” program that uses DO While Loops (as you probably used For To Next Loops originally). Occupies the first two rows of the 4 x 4 board
with 1’s and clears the other rows but with DO While Loops.
Extension “Tiles” Program 5eExtension “Tiles” Program 5eAhmed runs his own business. He lays floor tiles in rooms for Ahmed runs his own business. He lays floor tiles in rooms for customers by combining white tiles with tiles of one other colour to customers by combining white tiles with tiles of one other colour to make a pattern. Here is one example:make a pattern. Here is one example:
The width and length of a room will measure at least 100 cm and The width and length of a room will measure at least 100 cm and less than 1000 cm.less than 1000 cm.
The size of one floor tile is 30 cm × 30 cm.The size of one floor tile is 30 cm × 30 cm.
If the room measurements are not exact multiples of 30 cm, the If the room measurements are not exact multiples of 30 cm, the number of tiles must be rounded up so that Ahmed has enough number of tiles must be rounded up so that Ahmed has enough tiles.tiles.
Ahmed wants the program to add an extra 10% to the number of Ahmed wants the program to add an extra 10% to the number of tiles required in case any tiles get broken during the work.tiles required in case any tiles get broken during the work.
The program must calculate the total number of tiles required, The program must calculate the total number of tiles required, TilesRequiredTilesRequired, after the values for , after the values for RoomLengthRoomLength and and RoomWidthRoomWidth have been input and validated have been input and validated (using 1 logic (using 1 logic expression)expression)..
131304/21/2304/21/23
Extension “Tile Designs” Program 5fExtension “Tile Designs” Program 5f
141404/21/2304/21/23
Ahmed combines white tiles with tiles of one other colour to make a pattern. Ahmed combines white tiles with tiles of one other colour to make a pattern. He draws a design.He draws a design.
Here is one example:Here is one example:
Ali stores the design in a 2-dimensional array, Ali stores the design in a 2-dimensional array, FloorDesignFloorDesign. The length . The length and width of the room will be no more than 9 tiles each.and width of the room will be no more than 9 tiles each.
Write a program for Ahmed which:Write a program for Ahmed which: Initially, makes every tile white.Initially, makes every tile white. Asks the user to enter the size of a design.Asks the user to enter the size of a design.
e.g. 2 tiles by 3 tilese.g. 2 tiles by 3 tiles Asks the user to enter the design Asks the user to enter the design
e.g. Note that with this method the user will enter each row of the design and be e.g. Note that with this method the user will enter each row of the design and be expected to know to press the Enter key after each row. If you can think of a better way expected to know to press the Enter key after each row. If you can think of a better way please do so.please do so.
Then calculates Then calculates the number of white tiles and the number of coloured tiles in the the number of white tiles and the number of coloured tiles in the design.design.
Allows the user to reset and start again.Allows the user to reset and start again.
151504/21/2304/21/23
PlenaryPlenary
Explain the concept of 2D arrays.Explain the concept of 2D arrays.
Explain how to declare and reset them.Explain how to declare and reset them.
161604/21/2304/21/23
Declaring 2D ArraysDeclaring 2D Arrays
Dim …(…, …) As …Dim …(…, …) As …
1D Size1D Size 2D Size2D Size
11 22 33 4411
22
33
Resetting 2D arraysResetting 2D arrays
To reset 2D arrays you will need to 2 To reset 2D arrays you will need to 2 nested loops to loop through each column nested loops to loop through each column and each row.and each row.
For Column = 1 To ….For Column = 1 To ….For Row =1 To ….For Row =1 To ….
Array (Column, Row) = ….Array (Column, Row) = ….
Next RowNext Row Next ColumnNext Column
171704/21/2304/21/23