/* testarr13.cpp CIS 150 06/23/2005 David Klick This program demonstrates how to search for information in parallel arrays. */ #include #include using std::cout; using std::cin; using std::setw; using std::left; const int ARRAY_SIZE = 4; int find(int id[], int length, int idnum); int main(void) { int n, pos; // declare and initialize arrays of names // fname array stores first names // lname array stores last names // id array stores id numbers // These are parallel arrays because the elements // with the same index in each array are related. char fnames[][15] = { "Bob", "Carol", "Ted", "Alice" }; char lnames[][12] = { "Hope", "O'Connor", "Danson", "Springs" }; int id[ARRAY_SIZE] = { 99, 27, 32, 18 }; // loop until user chooses to exit do { // find out which ID # the user wants to display cout << "Find which user id? (-999 to exit) "; cin >> n; // display desired name if user didn't choose to exit if (n != -999) { pos = find(id, ARRAY_SIZE, n); if (pos == -1) { cout << "Error: ID number not found\n"; } else { // display names associated with ID # cout << left << setw(10) << lnames[pos] << fnames[pos] << '\n'; } } } while (n != -999); return 0; } int find(int id[], int length, int idnum) { int i; for (i=0; i