/* Filename: EmployeeSimpleArray.cpp Author: Br. David Carlson Date: January 24, 1998 Revised: February 11, 2001 to give better formatted output. Last Revised: September 24, 2017 This file implements the functions LoadArray, AverageRate, PrintAboveAvg, BinarySearch, and SelectionSort that are prototyped in EmployeeSimpleArray.h. All of these have to do with an array of employee structures. */ #include #include "EmployeeSimpleArray.h" /* Given: Nothing. Task: To read a sequence of employee data from the keyboard, storing it in EmpArray. Return: EmpArray The array of employee data. EmpCount The number of employees just entered. */ void LoadArray(EmpSimpleArrayType EmpArray, int & EmpCount) { EmployeeSimpleType Employee; EmpCount = 0; cout << "Enter the data for the first employee:" << endl; ReadEmployee(Employee); cout << endl; while ((! cin.fail()) && (EmpCount < EmpMax)) { EmpArray[EmpCount] = Employee; EmpCount++; cout << "Enter the data for employee " << EmpCount + 1 << ":" << endl; ReadEmployee(Employee); cout << endl; } if (! cin.fail()) cout << "Warning: not all of the data would fit into the array." << endl; } /* Given: EmpArray Array of employee data from index 0 to EmpCount - 1. EmpCount Number of records of data stored in EmpArray. Task: To find the average wage rate for the employees in EmpArray. Return: In the funtion name, the average wage rate. */ float AverageRate(EmpSimpleArrayType EmpArray, int EmpCount) { float Total; int k; assert(EmpCount > 0); // will abort program if condition not true Total = 0; for (k = 0; k < EmpCount; k++) Total = Total + EmpArray[k].WageRate; return Total / EmpCount; } /* Given: EmpArray The array of employee data. EmpCount The number of employee records in EmpArray. AvgRate The average wage rate for these employees. Task: To print the employee info for those employees whose wage rates are above the average. Return: Nothing. */ void PrintAboveAvg(EmpSimpleArrayType EmpArray, int EmpCount, float AvgRate) { int k, NumPrinted; char Reply; for (k = 0, NumPrinted = 0; k < EmpCount; k++) if (EmpArray[k].WageRate > AvgRate) { PrintEmployee(EmpArray[k]); NumPrinted++; if (NumPrinted % 19 == 0) { cout << "Enter g to go on: "; cin >> Reply; } } if (NumPrinted % 19 != 0) { cout << "Enter g to go on: "; cin >> Reply; } } /* Given: EmpArray Array of employee records (already in ascending order). Low The low index of the range to search. High The top index of the range to search. Target Record containing name for which to search. Task: To do a binary search for Target in the specified range of EmpArray. Return: In the function name, return the index of where Target was found or -1 if it was not found. */ int BinarySearch(EmpSimpleArrayType EmpArray, int Low, int High, const EmployeeSimpleType & Target) { int Mid, Diff; while (Low <= High) { Mid = (Low + High) / 2; Diff = EmpCompare(EmpArray[Mid], Target); if (Diff == 0) return Mid; else if (Diff < 0) Low = Mid + 1; else High = Mid - 1; } return -1; // If reach here, Target was not found. } /* Given: EmpArray The array of employee records to be sorted. Count The number of items in EmpArray. Task: To sort EmpArray into ascending order using selection sort, basing the order on the EmpCompare function. Return: EmpArray The sorted array. */ void SelectionSort(EmpSimpleArrayType EmpArray, int Count) { int i, k, MinIndex; EmployeeSimpleType Min; for (i = 0; i < Count - 1; i++) { // Find the minimum from index i to Count - 1. // Assume its the first item until we know better. Min = EmpArray[i]; MinIndex = i; for (k = i + 1; k < Count; k++) if (EmpCompare(EmpArray[k], Min) < 0) // Found a better min. { Min = EmpArray[k]; MinIndex = k; } if (MinIndex != i) // swap EmpArray[i] and the minimum { EmpArray[MinIndex] = EmpArray[i]; EmpArray[i] = Min; } } }