100 likes | 247 Vues
Advanced Higher Computing Based on Heriot-Watt University Scholar Materials. 2D Arrays. Lesson Objectives. 2 Dimensional Arrays. 1-Dimensional Arrays. From Higher we learned about 1 Dimensional arrays . An example is shown below:. 15. 15. 16. 19. 23. 28. 30. 31. 28. 24. 20. 17.
E N D
Advanced Higher ComputingBased on Heriot-Watt University Scholar Materials 2D Arrays Alford Academy Business Education and Computing
Lesson Objectives 2 Dimensional Arrays Alford Academy Business Education and Computing
1-Dimensional Arrays From Higher we learned about 1 Dimensional arrays. An example is shown below: 15 15 16 19 23 28 30 31 28 24 20 17 Average monthly temperatures VB Declaration Dim Month(11) as Integer The array has 12 data items in positions 0 .. 11 of the array Alford Academy Business Education and Computing
15 15 16 19 23 28 30 31 28 24 20 17 33 35 32 21 26 30 15 15 15 18 19 14 18 12 11 11 14 21 23 30 28 13 17 19 2-Dimensional Arrays As the name suggests, 2-D arrays have more than one dimension – in fact they have two! Jan Feb Mar Average monthly temperatures for Jan, Feb and March VB Declaration Dim Rainfall(2,11) as Integer The array has 3 rows (0.. 2) and 12 columns (0.. 11) Alford Academy Business Education and Computing
1 2 3 5 7 11 13 17 19 23 29 31 2-Dimensional Arrays – another example Column Index 0 1 2 3 0 Row Index 1 2 2-D Array of Prime Numbers VB Declaration Dim Primes(2,3) as Integer Primes(0,0) holds the value 1 Primes(0,2) holds the value 3 Do Scholar p115 Exercise and watch the Scholar animation on 2-D arrays Alford Academy Business Education and Computing
2-Dimensional Arrays – Initialising Suppose we have a small array eg an array called List that is 2x3 in its dimensions ie 2 rows and 3 columns This array is small enough to initialise its values to zero as follows:- VB Declaration Dim List(1,2) as Integer VB Declaration Dim List(1 to 2,1 to 3) as Integer List(0,0) = 0 List(0,1) = 0 List(0,2) = 0 List(1,0) = 0 List(1,1) = 0 List(1,2) = 0 List(1,1) = 0 List(1,2) = 0 List(1,3) = 0 List(2,1) = 0 List(2,2) = 0 List(2,3) = 0 More natural way of thinking about it! Alford Academy Business Education and Computing
2-Dimensional Arrays – Initialising Large Arrays Suppose we have a large array eg an array called List that is 20x30 in its dimensions ie 20 rows and 30 columns We would use a loop structure to initialise its values as follows:- VB Declaration Dim List(1 to 20,1 to 30) as Integer Dim row, col as Integer For row = 1 to 20 For col = 1 to 30 List(row,col) = 0 next col next row Alford Academy Business Education and Computing
2-Dimensional Arrays – Reading in the Prime Numbers Suppose we want to initialise the Prime 2-D array on Slide 4 with its values. We would use a loop structure to prompt the user for the values as follows:- VB Declaration Dim Prime(1 to 3,1 to 4) as Integer Dim row, col as Integer For row = 1 to 3 For col = 1 to 4 Prime(row,col) = Inputbox(“Please enter a prime number”) next col next row Alford Academy Business Education and Computing
2-Dimensional Arrays – Reading in and displaying the Prime Numbers Enter and test the following code to initialise a 2-D array with prime numbers and display these on screen Dim Prime(1 to 3,1 to 4) as Integer Dim row, col as Integer For row = 1 to 3 For col = 1 to 4 Prime(row,col) = Inputbox(“Please enter a prime number”) next col next row For row = 1 to 3 For col = 1 to 4 Print Tab(col * 5); Prime(row, col); next col print print next row Alford Academy Business Education and Computing
More Practice with 2-D Arrays and Theory Revision • Try the Extension Work on page 118 • Do all Review Questions on page 119 • Try the end of topic test on pages 119 - 120 Alford Academy Business Education and Computing