Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture Roger Sutton CO331 Visual Programming 13: Multi-dimensional Arrays 1.

Similar presentations


Presentation on theme: "Lecture Roger Sutton CO331 Visual Programming 13: Multi-dimensional Arrays 1."— Presentation transcript:

1 Lecture Roger Sutton CO331 Visual Programming 13: Multi-dimensional Arrays 1

2 CO331 Visual Programming 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 Dim grid(3, 2) As Integer Dim info(19, 27) As Integer, largestRowIndex As Integer largestRowIndex = Ubound(info, 1)

3 CO331 Visual Programming 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 ReDim command. However, only the last dimension can be modified. E.g. The keyword Preserve causes values already stored in the array to be retained. 3 Dim table(, ) As Integer = {{1, 0, 1},{0, 1, 0}} ReDim Preserve info(19, 37)

4 CO331 Visual Programming Example – Magic Squares Re: A numeric magic square consists 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!) 4

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

6 CO331 Visual Programming Example: n = 5 Consider the following: 6 i \ j

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

8 CO331 Visual Programming Example – cont’d Input design: Need to 1.ensure entries of storage array grid are set to 0. 2.clear display picture box. 3.receive dimension of square, performing any validation checks. Output design: Once arrangement of numbers has been determined and stored in grid 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. 8

9 CO331 Visual Programming Example – cont’d Algorithm design: findOddSolution (n, g(, )) 9 Start clear(g) i = 0 j =(n-1) \ 2 k = 1 k > n*n g(i, j) = k nextI = (n+ i – 1) Mod n nextJ = (n+ j + 1) Mod n 1 End Yes No 2 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 Is number greater than max possible? Store number Determine next position

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

11 CO331 Visual Programming Example – cont’d Method Implementation: 11

12 CO331 Visual Programming 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 12

13 CO331 Visual Programming Example – cont’d Form1 code: October 08 13

14 CO331 Visual Programming Example – cont’d Form1 code - cont’d: 14

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

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


Download ppt "Lecture Roger Sutton CO331 Visual Programming 13: Multi-dimensional Arrays 1."

Similar presentations


Ads by Google