Download presentation
Presentation is loading. Please wait.
Published byGarey Rice Modified over 8 years ago
1
Fall 2001(c)opyright Brent M. Dingle 2001 Multidimensional Arrays Brent M. Dingle Texas A&M University Chapter 10 – Section 2, part B (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
2
Fall 2001(c)opyright Brent M. Dingle 2001 Multi-Dim but no enumerated So what if we want to make an array of things that are just stored in a grid indexed by numbers. None of this fancy enumerated indexing?
3
Fall 2001(c)opyright Brent M. Dingle 2001 For example back to Grades What if we wanted to store multiple grades for many students. For example we have 10 students, each with five grades. How could we use arrays to do that?
4
Fall 2001(c)opyright Brent M. Dingle 2001 Recall If we just had 10 students, each with one grade we would just say: TYPE GRADE_ARY = array[1..10] of real; VAR grade : GRADE_ARY; And then student number 1’s grade would be stored in grade[1] student number 2’s grade would be stored in grade[2] student number 3’s grade would be stored in grade[3] and so on up to student number 10’s grade would be stored in grade[10]
5
Fall 2001(c)opyright Brent M. Dingle 2001 So giving each student 5 grades requires a 2-d array 2-d stands for 2 dimensional (i.e. we use 2 indices to access it) To create this type of array we say: TYPE GRADES_2D_ARY = array[1..10, 1..5] of real; VAR stud_grade : GRADES_2D_ARY;
6
Fall 2001(c)opyright Brent M. Dingle 2001 Then to access the grades To access the grades we would have the first index correspond to the student number And the second index correspond to the grade number.
7
Fall 2001(c)opyright Brent M. Dingle 2001 Accessing grades (cont 1) So to access student number 1’s FIRST grade we would use stud_grade[1, 1] So to access student number 1’s SECOND grade we would use stud_grade[1, 2] So to access student number 1’s THIRD grade we would use stud_grade[1, 3] So to access student number 1’s FOURTH grade we would use stud_grade[1, 4] So to access student number 1’s FIFTH grade we would use stud_grade[1, 5]
8
Fall 2001(c)opyright Brent M. Dingle 2001 Accessing grades (cont 2) So to access student number 2’s FIRST grade we would use stud_grade[2, 1] So to access student number 2’s SECOND grade we would use stud_grade[2, 2] So to access student number 2’s THIRD grade we would use stud_grade[2, 3] So to access student number 2’s FOURTH grade we would use stud_grade[2, 4] So to access student number 2’s FIFTH grade we would use stud_grade[2, 5]
9
Fall 2001(c)opyright Brent M. Dingle 2001 Accessing grades (cont 3) Any guesses on how we access the FOURTH grade of student number 3? [pause] If you said stud_grade[3, 4] You would be right ! =)
10
Fall 2001(c)opyright Brent M. Dingle 2001 The table for the above 2-d array is as follows:
11
Fall 2001(c)opyright Brent M. Dingle 2001 Some work Using the table on the previous slide, What is the value of: stud_grade[1, 1] stud_grade[2, 1] stud_grade[4, 5] stud_grade[9, 3] stud_grade[6, 2]
12
Fall 2001(c)opyright Brent M. Dingle 2001 Some work (cont) Give up? stud_grade[1, 1] = 54 stud_grade[2, 1] = 78 stud_grade[4, 5] = 89 stud_grade[9, 3] = 75 stud_grade[6, 2] = 78
13
Fall 2001(c)opyright Brent M. Dingle 2001 Notation As an aside, notice Pascal will allow us to access the members of a 2-d array in two ways: stud_grade[2, 4] is the same as stud_grade[2][4]. For the most part use the first way (stud_grade[2, 4]) if you are using a 2-d array and the second way (stud_grade[2][4]) if you are using an array of arrays.
14
Fall 2001(c)opyright Brent M. Dingle 2001 Suggested Problems (no grade) page 398 4, 5, 6, 7 pages 401 – 405 (programming exercises) 8, 10, 13, 15 ***, 16, 19, 21
15
Fall 2001(c)opyright Brent M. Dingle 2001 End Multidimensional Arrays Thus ends Chapter 10
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.