Complex Array Structures Brent M. Dingle Texas A&M University Chapter 10 – Section 2, part A (and some from Mastering Turbo Pascal 5.5, 3rd Edition by Tom Swan) Fall 2001 (c)opyright Brent M. Dingle 2001
(c)opyright Brent M. Dingle 2001 Array of Grades Pretend we had 50 students. Each has a numeric grade 0 to 100 Each has a letter grade, A, B, C, D, F We want to store that information in the computer. How do we do it (with what we know so far) ? Fall 2001 (c)opyright Brent M. Dingle 2001
(c)opyright Brent M. Dingle 2001 Parallel Arrays Parallel arrays associate data by storing things in some significant order. In the example above we would associate a number with each student 1 to 50. Then we could use TWO arrays of types: TYPE GradeType = ‘A’..’F’; ScoreList = array[1..50] of real; GradeList = array[1..50] of GradeType; VAR NumberGrades : ScoreList; LetterGrades : GradeList; Fall 2001 (c)opyright Brent M. Dingle 2001
Parallel Arrays – picture Fall 2001 (c)opyright Brent M. Dingle 2001
Parallel Arrays (cont) So as you can see each student number is used to index into either array. Notice this means if we change the order of one array, we MUST change the order of the second array in exactly the same way. Fall 2001 (c)opyright Brent M. Dingle 2001
(c)opyright Brent M. Dingle 2001 Arrays of Strings So far we have seen arrays of type char, integer, real, perhaps boolean. What about strings? Sure why not: TYPE NAME_LIST = array [1..20] of string; The above is completely valid. Fall 2001 (c)opyright Brent M. Dingle 2001
(c)opyright Brent M. Dingle 2001 Challenge (no grade) Using an array of strings (all last names) Create a program that will alphabetize them. This strongly relates to section 10.1 Fall 2001 (c)opyright Brent M. Dingle 2001
So if strings are arrays… Can we have an array of arrays? Yep. The easiest way to explain this is using an array of enumerated types and an array of numbers. Fall 2001 (c)opyright Brent M. Dingle 2001
(c)opyright Brent M. Dingle 2001 A room’s exits Let us create some very simple rooms, that have only exits north, east, south and/or west. Let us create a map of rooms where each room is given a number 1 to [number_of_rooms]. How would we represent this in a computer? Fall 2001 (c)opyright Brent M. Dingle 2001
(c)opyright Brent M. Dingle 2001 A picture to help Fall 2001 (c)opyright Brent M. Dingle 2001
(c)opyright Brent M. Dingle 2001 How do we ever Represent something like that? It’s actually pretty easy. Come to class and find out how. (of course I may need to be asked =) Fall 2001 (c)opyright Brent M. Dingle 2001
End Complex Arrays, part A Fall 2001 (c)opyright Brent M. Dingle 2001