Presentation is loading. Please wait.

Presentation is loading. Please wait.

2D Arrays Defining, Declaring & Processing

Similar presentations


Presentation on theme: "2D Arrays Defining, Declaring & Processing"— Presentation transcript:

1 2D Arrays Defining, Declaring & Processing
02/02/2019

2 Learning Objectives Explain the concept of 2D arrays.
Explain how to declare and reset them. 02/02/2019

3 Dim …(…, …) As … Declaring 2D Arrays 1D Size 2D Size 1 2 3 1 2
1 2 3 1 2 02/02/2019 3 3

4 2D Arrays e.g. a firm’s quarterly sales figures for the years 1990 – 1999. 4 (quarters) x 10 (years) = 40 items of data Dim SalesFigures(4, 10) As Decimal 02/02/2019 4 4

5 2D Arrays 1 2 3 1 2 3 4 5 6 7 8 9 Each row is a year.
1 2 3 1 2 3 4 5 6 7 8 9 56800 96400 Each row is a year. Each column is a quarter. e.g. Sales was €56800 in the 3rd quarter of 1991. SalesFigures(2,0) = 56800 Sales was €96400 in the 4th quarter of 1999. SalesFigures(3,8) = 96400 02/02/2019 5 5

6 Uses for 2D Arrays Useful for storing data for some mathematical problems. Limited use for ‘business’ type problems because all data items have to be of the same type. 02/02/2019 6 6

7 Resetting 2D arrays To reset 2D arrays you will need to 2 nested loops to loop through each column and each row. For Column = 1 To …. For Row =1 To …. Array (Column, Row) = …. Next Row Next Column 02/02/2019

8 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>

9 Extension “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.

10 Extension “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.

11 Extension “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.

12 Extension “Chess Board3” Program 5d
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.

13 Extension “Tiles” Program 5e
Ahmed runs his own business. He lays floor tiles in rooms for customers by combining white tiles with tiles of one other colour to make a pattern. Here is one example: The width and length of a room will measure at least 100 cm and less than 1000 cm. The size of one floor tile is 30 cm × 30 cm. If the room measurements are not exact multiples of 30 cm, the number of tiles must be rounded up so that Ahmed has enough tiles. Ahmed wants the program to add an extra 10% to the number of tiles required in case any tiles get broken during the work. The program must calculate the total number of tiles required, TilesRequired, after the values for RoomLength and RoomWidth have been input and validated (using 1 logic expression). 02/02/2019

14 Extension “Tile Designs” Program 5f
Ahmed combines white tiles with tiles of one other colour to make a pattern. He draws a design. Here is one example: Ali stores the design in a 2-dimensional array, FloorDesign. The length and width of the room will be no more than 9 tiles each. Write a program for Ahmed which: Initially, makes every tile white. Asks the user to enter the size of a design. e.g. 2 tiles by 3 tiles 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 expected to know to press the Enter key after each row. If you can think of a better way please do so. Then calculates the number of white tiles and the number of coloured tiles in the design. Allows the user to reset and start again. 02/02/2019

15 Plenary Explain the concept of 2D arrays.
Explain how to declare and reset them. 02/02/2019

16 Dim …(…, …) As … Declaring 2D Arrays 1D Size 2D Size 1 2 3 4 1 2 3
02/02/2019 16 16

17 Resetting 2D arrays To reset 2D arrays you will need to 2 nested loops to loop through each column and each row. For Column = 1 To …. For Row =1 To …. Array (Column, Row) = …. Next Row Next Column 02/02/2019


Download ppt "2D Arrays Defining, Declaring & Processing"

Similar presentations


Ads by Google