Code:
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstring>
using namespace std;
// Needed for sufficient size for arrays
const int Name_Length = 15;
const int City_Length = 15;
// Structure declared to store employee data
struct EmployeeInfo
{
int EmployeeID;//will store employee's IDs
char EmployeeName[Name_Length];// will store employee's names
char City[City_Length];// will store employee's city
};
void OpenOutputFile (ofstream&, char[]);
void PrintHeading (ofstream&);
void showArray(ofstream&,EmployeeInfo [], int);// displays original unsorted data
void BubbleSortArray(EmployeeInfo [], int);// bubblesorts the data
void selectionSortArray(EmployeeInfo [], int);//selectionsorts the data
int main()
{
char variable;
const int Num_Workers = 50;
//Initialized the employee structure with employee data
EmployeeInfo Employees[Num_Workers] = {
{735, "Ann J.Weeks" ,"Corpus Christi"},
{341, "Tom Wonder" ,"Denver"},
{646, "Mat Gonzales", "Ft Worth"},
{229, "Art Smith", "Mustang Mott"},
{842, "Ed Moreno", "Durango"},
{620, "I.M.Blimpee", "Beeville"},
{741, "Ed White", "Red River"},
{222, "May Thomas", "Ft Worth"},
{165, "Joe Bell", "Newark"},
{182, "Alice Beach", "San Jose"},
{943, "Tom Thumb", "Dallas"},
{150, "Nan Newman", "Victoria"},
{250, "Dan Duran", "Corsicana"},
{350, "Michael Nyu", "Las Vegas"},
{228, "Al DeLeon", "Brownsville"},
{344, "Ted Tildon", "Miami"},
{828, "M.J.Barns", "Cleveland"},
{110, "Dick Reyes", "New York"},
{987, "Ralph Dietz", "Cuero"},
{777, "Ann Hatter", "Big Springs"},
{191, "M.K.Moore", "Atlanta"},
{545, "Mack Foyt", "Daytona"},
{878, "Kim Mills ", "Austin"},
{900, "Roy Shaw", "Paradise"},
{351, "Herb Rilley", "San Francisco"},
{291, "Ann Woorley", "Seattle"},
{854, "T.N.Teal","Baltimore"},
{404, "Sean New", "Lubbock"},
{607, "Tom Russell", "Salt Lake City"},
{305, "Bill Black", "Jackson"},
{199, "Sue Shuman", "Chicago"},
{395, "Nan Newman", "Bullhead City"},
{809, "Avis Hall", "Helena"},
{504, "Tammera Tool", "South Bend"},
{841, "Nancy Gomez", "Miami"},
{149, "Mel Dale", "Oshkosh"},
{492, "Nell Morgan", "Atlanta"},
{613, "Dan Morales", "Jackson"},
{386, "Sue Bush", "Evansville"},
{929, "Keith Aaron", "LayFayette"},
{481, "Tom Herron", "Denver"},
{853, "Paul Driver", "Chicago"},
{729, "Gayle Turner", "San Jose"},
{205, "A.D.Daniel", "Beeville"},
{482, "Irma Perez", "Charlotte"},
{774, "Don Guzmann", "New Braunfels"},
{338, "Mac Deaver", "Plano"},
{194, "Hope Allen", "Carbondale"},
{743, "Raul Pena", "Cleveland"},
{108, "Merri Clark", "Sugarland"}
};
EmployeeInfo Employees2[Num_Workers] = {
{735, "Ann J.Weeks" ,"Corpus Christi"},
{341, "Tom Wonder" ,"Denver"},
{646, "Mat Gonzales", "Ft Worth"},
{229, "Art Smith", "Mustang Mott"},
{842, "Ed Moreno", "Durango"},
{620, "I.M.Blimpee", "Beeville"},
{741, "Ed White", "Red River"},
{222, "May Thomas", "Ft Worth"},
{165, "Joe Bell", "Newark"},
{182, "Alice Beach", "San Jose"},
{943, "Tom Thumb", "Dallas"},
{150, "Nan Newman", "Victoria"},
{250, "Dan Duran", "Corsicana"},
{350, "Michael Nyu", "Las Vegas"},
{228, "Al DeLeon", "Brownsville"},
{344, "Ted Tildon", "Miami"},
{828, "M.J.Barns", "Cleveland"},
{110, "Dick Reyes", "New York"},
{987, "Ralph Dietz", "Cuero"},
{777, "Ann Hatter", "Big Springs"},
{191, "M.K.Moore", "Atlanta"},
{545, "Mack Foyt", "Daytona"},
{878, "Kim Mills ", "Austin"},
{900, "Roy Shaw", "Paradise"},
{351, "Herb Rilley", "San Francisco"},
{291, "Ann Woorley", "Seattle"},
{854, "T.N.Teal","Baltimore"},
{404, "Sean New", "Lubbock"},
{607, "Tom Russell", "Salt Lake City"},
{305, "Bill Black", "Jackson"},
{199, "Sue Shuman", "Chicago"},
{395, "Nan Newman", "Bullhead City"},
{809, "Avis Hall", "Helena"},
{504, "Tammera Tool", "South Bend"},
{841, "Nancy Gomez", "Miami"},
{149, "Mel Dale", "Oshkosh"},
{492, "Nell Morgan", "Atlanta"},
{613, "Dan Morales", "Jackson"},
{386, "Sue Bush", "Evansville"},
{929, "Keith Aaron", "LayFayette"},
{481, "Tom Herron", "Denver"},
{853, "Paul Driver", "Chicago"},
{729, "Gayle Turner", "San Jose"},
{205, "A.D.Daniel", "Beeville"},
{482, "Irma Perez", "Charlotte"},
{774, "Don Guzmann", "New Braunfels"},
{338, "Mac Deaver", "Plano"},
{194, "Hope Allen", "Carbondale"},
{743, "Raul Pena", "Cleveland"},
{108, "Merri Clark", "Sugarland"}
};
//Output file stream
ofstream dataFileOut;
//Input file name
//Output file name
char outputFile[80] = "RSLab06.out";
OpenOutputFile(dataFileOut, outputFile);
PrintHeading(dataFileOut);
showArray(dataFileOut,Employees, Num_Workers);//displays original unsorted data
BubbleSortArray(Employees, Num_Workers);// bubblesorts the data
dataFileOut << "\nData after Bubble sort----------------------------" << endl;
showArray(dataFileOut,Employees, Num_Workers);//displays sorted data
dataFileOut << "\nData after Selection sort--------------------------" << endl;
selectionSortArray(Employees2, Num_Workers);
showArray(dataFileOut,Employees2, Num_Workers);//displays sorted data
system("pause");
return 0;
}
//Function definition of showArray
void showArray(ofstream& dataFileOut,EmployeeInfo array[], int size)
{
for (int count = 0; count < size; count++)
{
dataFileOut << array[count].EmployeeID << " ";
dataFileOut << "\t\t" << array[count].EmployeeName << setw(2) << " ";
dataFileOut << "\t\t" << array[count].City << " ";
dataFileOut << endl;
}
}
//Function definition of the bubble sort function
void BubbleSortArray(EmployeeInfo array[], int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size-1); count++)
{
// Determines the condition if the data on the left is
// greater than the right, it will swap all the way through
// until the data is sorted. This is done for the ID, Name,
// and the city of the employees.
if (array[count].EmployeeID > array[count+1].EmployeeID)
{
temp = array[count].EmployeeID;//stores data into a temp variable
array[count].EmployeeID = array[count+1].EmployeeID;// assigns
// the data of the right element into the left element
array[count+1].EmployeeID = temp;// assigns the copied data
// that was stored into the temp variable into the right.
swap = true;
}
}
}while (swap);
}
// Function definition of the selection sort function
void selectionSortArray(EmployeeInfo array[], int size)
{
int startScan, minIndex, minValue;
for(startScan = 0; startScan < (size-1); startScan++)
{
minIndex = startScan;
minValue = array[startScan].EmployeeID;
for( int index = startScan + 1; index < size; index++)
{
if (array[index].EmployeeID < minValue)
{
minValue = array[index].EmployeeID;
minIndex = index;
}
}
array[minIndex].EmployeeID = array[startScan].EmployeeID;
array[startScan].EmployeeID = minValue;
}
}
void OpenOutputFile (ofstream& dataFileOut, char outputFile[])
{
//Open output file
dataFileOut.open(outputFile);
//If not successful open, display message and exit with error
if (!dataFileOut)
{
cout << "\n\n\n\t\t\t Error opening file!"
<< outputFile << endl;
exit(1);
}
} //End OpenOutputFile
void PrintHeading(ofstream& dataFileOut)
{
dataFileOut << "Employee ID|" << "Employee Name|\t" << " City|"
<< endl;
}