Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//Here i have a program that reads a file of employee data into an array of structs. I have a function that searches the array and finds the oldest person and prints out that person's data. But now i need to modify a function that sorts through the array and moves the oldest persons data to the 1st position in the array, the 2nd oldest to the 2nd position and so on and does the same for all 1000 employees so that it will sort then print all employee data from oldest to youngest. How could i do this?

Here is the first few lines of data to give an idea of the layout(date of birth is layed out YYYYMMDD

114680858 19670607 Matilda Vincent MI

114930037 19471024 Desdemona Hanover ID

115550206 19790110 Xanadu Perlman ND

116520629 19630921 Alexander Hall SD





C++
struct employees // employee data
{
int ss_number;//social security
int dob;//date of birth YYYY/MM/DD Ex.) 19870314=1987/03/14
string f_name;
string l_name;
string state; //state of residence };


void read_file()//read file into array of 1000 structs
{
ifstream data("/home/www/class/een118/labs/database1.txt");
employees array[1000]
if(!data.fail())
{
int i;
for(int i=0;i<1000;i++)
{
data>>array[i].ss_number
>>array[i].dob
>>array[i].f_name
>>array[i].l_name
>>array[i].state;
}
for(int i=0;i<1000;i++)
{
cout<<array[i].ss_number>>" "<<array[i].dob>>" "<<array[i].f_name>>" "<<
array[i].l_name>>" "<<array[i].state;
}}}


void print_person(employees e)
{
cout<<e.ss_number>>" "<<e.dob>>" "<<e.f_name>>" "<<e.l_name>>" "<<e.state;
}


void find_oldest(employees array[])// oldest person = smallest dob
{
int i;
int index=0
int oldest=1000000000;//dummy variable
for(i=1;i<1000;i++)//1000 is array length
{
if(array[i].dob<oldest)>
{
index=i;
oldest=array[i].dob;
}
}
print_person(array[i]);
}


int main()
{
employees array[1000];
read_file(array);
find_oldest(array);
}
Posted
Updated 20-Apr-14 11:55am
v3

1 solution

Once you have fixed the code (as it stands it doesn't even compile), no one prevents you using the std::sort[^] function.
 
Share this answer
 
Comments
[no name] 20-Apr-14 17:47pm    
Of course it might be a prerequisite that he implement his own sort for his homework. But yeah as soon as he fixes the compilation errors, he is probably going to be trying to stuff 1000 records into the array that can only hold 10. 5 anyway :-).
CPallini 20-Apr-14 19:34pm    
Thank you.
dudicus1414 20-Apr-14 17:58pm    
i set the array to 10 for testing purposes, i just changed it though.I am however able to Compile it and everything works fine. Im just not sure how to do the sorting stuff. and yes i do have to implement my own sorting function(s).
CPallini 20-Apr-14 19:35pm    
You could then start with the sorting algorithm Wikipedia page:
http://en.wikipedia.org/wiki/Sorting_algorithm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900