Multidimensional Arrays Brent M Dingle Texas AM University
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) 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? 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? 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; n And then w w w Fall 2001 student number and so on up to student number 1’s grade would be stored in grade[1] 2’s grade would be stored in grade[2] 3’s grade would be stored in grade[3] 10’s grade would be stored in grade[10] (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_2 D_ARY = array[1. . 10, 1. . 5] of real; VAR stud_grade : GRADES_2 D_ARY; 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. 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] 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] 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 ! =) Fall 2001 (c)opyright Brent M. Dingle 2001
The table for the above 2 -d array is as follows: Fall 2001 (c)opyright Brent M. Dingle 2001
Some work Using the table on the previous slide, What is the value of: n n n Fall 2001 stud_grade[1, stud_grade[2, stud_grade[4, stud_grade[9, stud_grade[6, 1] 1] 5] 3] 2] (c)opyright Brent M. Dingle 2001
Some work (cont) Give up? n n n Fall 2001 stud_grade[1, stud_grade[2, stud_grade[4, stud_grade[9, stud_grade[6, 1] 1] 5] 3] 2] = = = 54 78 89 75 78 (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. Fall 2001 (c)opyright Brent M. Dingle 2001
Suggested Problems (no grade) page 398 n 4, 5, 6, 7 pages 401 – 405 (programming exercises) n Fall 2001 8, 10, 13, 15 ***, 16, 19, 21 (c)opyright Brent M. Dingle 2001
End Multidimensional Arrays Thus ends Chapter 10 Fall 2001 (c)opyright Brent M. Dingle 2001
- Slides: 15