/* Filename: emparray.cpp Author: Br. David Carlson Date: January 24, 1998 Revised: February 11, 2001 to give better formatted output. Last Revised: February 21, 2013 This file implements the functions LoadArray, AverageRate, PrintAboveAvg, BinarySearch, and SelectionSort that are prototyped in emparray.h. All of these have to do with an array of employee structures. */ #include #include "emparray.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. In the function name return OKFlag or TooMuchDataFlag to indicate how this function ended. */ int LoadArray(EmpArrayType EmpArray, int & EmpCount) { EmployeeType Employee; int Flag; EmpCount = 0; cout << "Enter the data for the first employee:" << endl; Flag = ReadEmployee(Employee); while ((Flag == OKFlag) && (EmpCount < EmpMax)) { EmpArray[EmpCount] = Employee; EmpCount++; cout << "Enter the data for employee " << EmpCount + 1 << ":" << endl; Flag = ReadEmployee(Employee); } if (Flag != OKFlag) { cin.clear(); // clear the flags so that further input can occur return OKFlag; } else // array ran out of room return TooMuchDataFlag; } /* 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(EmpArrayType 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(EmpArrayType 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 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(EmpArrayType EmpArray, int Low, int High, const EmployeeType & 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(EmpArrayType EmpArray, int Count) { int i, k, MinIndex; EmployeeType 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; } } }