1 / 16

13: Multi-dimensional Arrays

13: Multi-dimensional Arrays. Multidimensional arrays. These are collections of data viewed in two dimensions as a table of rows and column.

sadah
Télécharger la présentation

13: Multi-dimensional Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 13: Multi-dimensional Arrays

  2. Multidimensional arrays These are collections of data viewed in two dimensions as a table of rows and column. • A particular table element is identified by two indexes: the first identifies the row and the second the column.E.g. an array may be declared:This is a 4 row x 3 column table called grid whose elements are of type integer. • The largest index value of an array can be found using Ubound (the smallest index value using LBound) E.g. produces the value 19. Dim grid(3, 2) As Integer Dim info(19, 27) As Integer, largestRowIndex As Integer largestRowIndex = Ubound(info, 1)

  3. Multidimensional arrays – cont’d As in the case of one dimension arrays, initialisation may be performed when the array is declared.E.g. Values are entered as rows or by assignment statements later. Once created an array has a fixed size that can only be changed explicitly using the ReDimcommand. However, only the last dimension can be modified. E.g. The keyword Preserve causes values already stored in the array to be retained. Dim table( , ) As Integer = {{1, 0, 1},{0, 1, 0}} ReDim Preserve info(19, 37)

  4. Example – Magic Squares Re:www.markfarrar.co.uk/msfmsq01.htm • A numeric magic squareconsists of a series of numbers arranged in a square in such a manner that each row, each column and both the corner diagonals sum to the same amount called the magic total. • The magic total may be determined from the formula: where n is the number of cells on each side of the magic square. The aim of the program is to display magic squares for odd values of n, from 3 to 9. (Even number magic squares are more challenging!)

  5. Example – cont’d Analysis: • What is to be input?n, the number of cells on each side of the square. • How is problem to be solved?– Construct an algorithm • Place 1 in middle cell of top row • Move diagonally up to next cell (rows and columns wrap around - above top row becomes bottom row, outside right most column becomes leftmost column) • If cell occupied move to cell directly below otherwise enter next number • If not last number ( ) go back to step (ii) • What is to be output?Grid of numbers and magic total.

  6. Example: n = 5 Consider the following: 18 25 2 9 17 24 1 8 15 17 23 5 7 14 16 23 4 4 6 13 20 22 10 21 3 10 12 19 9 11 18 25 2

  7. Example – cont’d Label: lblTitle Form design: Magic Squares Label: lblPrompt Button: cmdFind Enter odd n < 10: Find Textbox: txtNumber PictureBox: picBox Label:lblMagicTotal Button: cmdExit Magic total is #### Exit Run

  8. Example – cont’d Input design:Need to • ensure entries of storage array grid are set to 0. • clear display picture box. • receive dimension of square, performing any validation checks. Output design: Once arrangement of numbers has been determined and stored ingrid array, we require code to format and output solution. Suggest this might be a chequered board with numbers contrasting with the colour of their cell. Accordingly we require ‘Square’ object and a means to output black and white squares with appropriate numbers.

  9. Example – cont’d Algorithm design: findOddSolution(n, g( , )) Start n – side of square g – grid array i – row position, j – column position Set initial position: Top row, centre k - number to be placed in grid clear(g) i = 0 j =(n-1) \ 2 k = 1 2 Yes k > n*n Is number greater than max possible? No g(i, j) = k nextI = (n+ i– 1) Mod n nextJ = (n+ j + 1) Mod n Store number Determine next position End 1

  10. Example – cont’d Algorithm design: - cont’d 1 Is cell occupied? No No g (nextI,nextJ) > 0 Yes Yes nextI = (i+ 1) Mod n nextJ = j Move down one cell i = nextI j = nextJ k = k +1 Confirm next position Increase number by 1 2

  11. Example – cont’d Method Implementation:

  12. Example – cont’d Function implementation: magicNumber(n): determines magic number of magic square of order n clear(grid): method to set grid elements to zero

  13. Example – cont’d Form1 code:

  14. Example – cont’d Form1 code - cont’d:

  15. Example – cont’d Form1 code - cont’d: drawBoard(n): method to draw chequered board of size n

  16. Example – cont’d Form1 code - cont’d: insertNumbers(n, g): method to display numbers stored in grid on board Run

More Related