Programming Fundamentals/Parallel Arrays
Overview
editA group of parallel arrays is a form of implicit data structure that uses multiple arrays to represent a singular array of records. It keeps a separate, homogeneous data array for each field of the record, each having the same number of elements. Then, objects located at the same index in each array are implicitly the fields of a single record.[1]
Discussion
editA data structure is a data organization and storage format that enables efficient access and modification. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data. Data structure options include arrays, linked lists, records, and classes.[2]
Parallel arrays use two or more arrays to represent a collection of data where each corresponding array index is a matching field for a given record. For example, if there are two arrays, one for names and one for ages, the array elements at index [2] for arrays "names" and "ages" would describe the name and age of the third person. This is because arrays begin counting at 0.
Pseudocode
editFunction Main Declare String Array names[5] Declare Integer Array ages[5] Assign names = ["Lisa", "Michael", "Ashley", "Jacob", "Emily"] Assign ages = [49, 48, 26, 19, 16] DisplayArrays(names, ages) End Function DisplayArrays (String Array names, Integer Array ages) Declare Integer index For index = 0 to Size(name) - 1 Output names[index] & " is " & ages[index] & " years old" End End
Output
editLisa is 49 years old Michael is 48 years old Ashley is 26 years old Jacob is 19 years old Emily is 16 years old
Key Terms
edit- parallel array
- An implicit data structure that uses multiple arrays to represent a singular array of records.