1 / 10

Advanced Higher Computing Based on Heriot-Watt University Scholar Materials

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.

dacey
Télécharger la présentation

Advanced Higher Computing Based on Heriot-Watt University Scholar Materials

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. Advanced Higher ComputingBased on Heriot-Watt University Scholar Materials 2D Arrays Alford Academy Business Education and Computing

  2. Lesson Objectives 2 Dimensional Arrays Alford Academy Business Education and Computing

  3. 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

  4. 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

  5. 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

  6. 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

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

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

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

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

More Related