Download presentation
Presentation is loading. Please wait.
Published byPearl Holland Modified over 9 years ago
1
18/12/20151 5.2 Arrays 2D Arrays Defining, Declaring & Processing
2
218/12/2015 Learning Objectives Explain the concept of 2D arrays. Explain how to declare and reset them.
3
318/12/2015 Declaring 2D Arrays Dim …(…, …) As … 1D Size 2D Size 1234 12 3
4
418/12/2015 2D Arrays e.g. a firm’s quarterly sales figures for the years 1990 – 1999. 4 (quarters) x 10 (years) = 40 items of data 4 (quarters) x 10 (years) = 40 items of data Dim SalesFigures(4, 10) As Decimal
5
518/12/2015 2D Arrays Each row is a year. Each column is a quarter. e.g. Sales was €56800 in the 3 rd quarter of 1991. Sales was €56800 in the 3 rd quarter of 1991. SalesFigures(2,0) = 56800 Sales was €96400 in the 4 th quarter of 1999. Sales was €96400 in the 4 th quarter of 1999. SalesFigures(4,9) = 96400 56800 96400 1234 12 3 4 5 6 7 8 9 10
6
618/12/2015 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.
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 Column = 1 To …. For Row =1 To …. Array (Column, Row) = …. Array (Column, Row) = …. Next Row Next Column Next Column 718/12/2015
8
Pseudocode will use the following structures: DECLARE : ARRAY[ : ] OF So for an array of elements numbered from 1 – 10: DECLARE : ARRAY[ : ] OF DECLARE : ARRAY[ :,[ : ] OF
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,3.2 Allow a Reset option. 0000 0000 0000 0000
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). 1318/12/2015
14
Extension “Tile Designs” Program 5f 14 18/12/2015 Ahmed combines white tiles with tiles of one other colour to make a pattern. He draws a design. Here is one example: 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. 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 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 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. Then calculates the number of white tiles and the number of coloured tiles in the design. Allows the user to reset and start again. Allows the user to reset and start again.
15
1518/12/2015 Plenary Explain the concept of 2D arrays. Explain how to declare and reset them.
16
1618/12/2015 Declaring 2D Arrays Dim …(…, …) As … 1D Size 2D Size 1234 12 3
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 Column = 1 To …. For Row =1 To …. Array (Column, Row) = …. Array (Column, Row) = …. Next Row Next Column Next Column 1718/12/2015
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.