A solution to a list of records Parallel Arrays A solution to a list of records
Record Definition A collection of two or more related data elements that may be of different types. Each of the data elements is called a Field. Example: a student record consists of fields: - a name (string) - a student id (int) - a gpa (double)
Record List We may also need to have a list of records. But Arrays can only be a list of a single data type. How can we make a "List of Records"?
Parallel Arrays Definition One solution to this problem is Parallel Arrays a set of two or more arrays of equal length where the i'th element of each array is related
Parallel Arrays Example Say we have this list of students on paper: Name ID gpa Alice 12345 2.7 Bob 67890 3.1 Cathy 54321 3.7 David 98765 3.5
Parallel Arrays Declaration Declare an array for each field, all with the same length. const int MAX = 4; string name[MAX]; int id[MAX]; double gpa[MAX];
Parallel Arrays Population with Literals So the i'th element of each array is related to the same student: name[0]="Alice"; id[0]=12345; gpa[0]=2.7; name[1]="Bob"; id[1]=67890; gpa[1]=3.1; name[2]="Cathy"; id[2]=54321; gpa[2]=3.7; name[3]="David"; id[3]=98765; gpa[3]=3.5;
Parallel Arrays Print Now we can print a report of students by looping through the arrays, using the same index for each student: for (int i=0; i<MAX; i++) { cout << name[i] << " " << id[i] << " " << gpa[i] << endl; }
Parallel Arrays Population with user input Or, we can ask the user to populate the arrays for (int i=0; i<MAX; i++) { cout << "Enter name, id and gpa: "; cin >> name[i] >> id[i] >> gpa[i]; }
Parallel Arrays MAX of one field We can find the student with the highest GPA: int ndxMaxGPA = 0; // assume 1st student has max for (int i=1; i<MAX; i++) { if (gpa[i] > gpa[ndxMaxGPA]) ndxMaxGPA = i; // save INDEX of max gpa } cout << "Stu with max GPA: " << name[ndxMaxGPA];
Parallel Arrays SEARCH by one field We can search by ID: int srchID, found = -1; // -1 = not found yet cout << "Enter ID to search for: "; cin >> srchID; for (int i=0; i<MAX; i++) { if (id[i] == srchID) found = i; // found in index i }
Parallel Arrays SEARCH by one field // continued from previous slide if (found == -1) cout << srchID << " not found!"; else cout << "Name = " << name[found] << " GPA = " << gpa[found];
Partial Parallel Arrays Declaration We can apply the idea of Partial Arrays to Parallel: const int MAX = 4; string name[MAX]; int id[MAX]; double gpa[MAX]; int numStudents = 0; // numStudents applies to all 3 arrays
Partial Parallel Arrays Population by user input We can ask the user to populate a Partial Parallel Array: cout << "How many students? "; cin >> numStudents; // should validate 0..MAX for (int i=0; i<numStudents; i++) { cout << "Enter name, id, gpa: "; cin >> name[i] >> id[i] >> gpa[i]; }
In the Future There are better ways to implement a List of Records But Parallel Arrays are a common data structure.
Vocabulary Term Definition Record A collection of related data elements, often of different types. Field One of the data elements of a record. Parallel Arrays set of two or more arrays of the same length, where the i'th element of each is related.