Thread: Need help sorting structure by male/female and zip code

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    8

    Need help sorting structure by male/female and zip code

    I have been working on this program and cant figure out how to sort some of the functions by male or others by female and one needs to be sorted by zip code can you please help?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define FILE_NAME "payfile.txt"
    #define MAX 55 
    #define BUF_SIZE 100 
    
    typedef struct Address
    {
        char street[17]; 
        char city[12];  
        char state[3]; 
        char zip[6];  
    }Address;
    
    typedef struct Employee
    {
        char first[8]; 
        char init[2]; 
        char last[10]; 
        Address ad;
        int age;
    	char sex[1];
    	int years;
        double pay;
    }Employee;
    
    void strSub(char s1[], char s2[], int start, int length); 
    int getEmploy(Employee s[], int max); 
    void printEmploy(Employee s[], int num); 
    void printMen(Employee s[], int num);
    int topPaywoman(Employee s[], int num); 
    int lowPayman(Employee s[], int num);
    double avgPay(Employee s[], int num); 
    void printBelowavg(Employee s[], int num);
    void printRatiomen(Employee s[], int num);
    void printSenior(Employee s[], int num);
    void printRaise(Employee s[], int num);
    void sortzip(Employee arr[], int num);
    
    
    
    
    int main()
    {
        int numEmploy, topIndex, lowIndex;
        Employee employ[MAX]; 
    
        numEmploy = getEmploy( employ, MAX );
        if( numEmploy == 0 ) return 0;
        
    	printEmploy( employ, numEmploy );
    
        printMen( employ, numEmploy );
    	
    	topIndex = topPaywoman( employ, numEmploy );
        printf("\nThe woman with the most pay is %s %s earning $%.2f per week\n",
                employ[topIndex].first, employ[topIndex].last, employ[topIndex].pay);
    
        lowIndex = lowPayman( employ, numEmploy );
        printf("\nThe man with the least pay is %s %s earning $%.2f per week\n",
                employ[lowIndex].first, employ[lowIndex].last, employ[lowIndex].pay);
    	
    	printf("\nThe average pay is $%.2f per week\n", avgPay(employ, numEmploy));
    
        
    	printBelowavg( employ, numEmploy );
    
        printRatiomen( employ, numEmploy );
    
    	printSenior( employ, numEmploy );
    
    	printRaise( employ, numEmploy );
    
    	/*sort by zip code
    	sortzip( employ, numEmploy );
        printf("\nThe employee data sorted in ascending order by zip code ...");
        printEmploy( employ, numEmploy );*/
    
        return 0;
    }
    
    void strSub(char s1[], char s2[], int start, int length) {
        int i;
        for( i = 0; i < length; ++i)
            s2[i] = s1[start+i];
        s2[i] = 0;
    }
    
    
    int getEmploy(Employee s[], int maxNum) {
        int i = 0;
        char buf[BUF_SIZE];
    	char tmpa[32];
    	char tmp[32];
        FILE* fp = fopen( FILE_NAME, "r" );
         
        while( i<maxNum && fgets( buf, BUF_SIZE, fp ) )
        {
    
            strSub( buf, s[i].first, 0, 7 );
            strSub( buf, s[i].init, 8, 1 );
            strSub( buf, s[i].last, 10, 9 );
    
            strSub( buf, s[i].ad.street, 20, 16 );
            strSub( buf, s[i].ad.city, 37, 11 );
            strSub( buf, s[i].ad.state, 49, 2 );
            strSub( buf, s[i].ad.zip, 52, 5 );
    
            strSub( buf, tmp, 58, 2 );
            s[i].age = atoi(tmp);
    
    		strSub( buf, s[i].sex, 60, 2 );
           
    		strSub( buf, tmpa, 62, 2 );
    		s[i].years = atoi(tmpa);
            
            strSub( buf, tmp, 65, 5 );
            s[i].pay = atof(tmp);
            ++i;
        }
        fclose( fp );
        return i; 
    }
    
    
    void printEmploy(Employee s[], int num) {
        int i;
         printf( "The employees are\n");
    	 for( i = 0; i<num; ++i )
        {
           
    			printf( "%s %s %s %s %s %s %s %2d %s %1d %.2f\n", s[i].first, s[i].init, s[i].last, s[i].ad.street, 
    			s[i].ad.city, s[i].ad.state, s[i].ad.zip, s[i].age, s[i].sex, s[i].years, s[i].pay );
        }
    }
    
    void printMen(Employee s[], int num) {
        int i;
    	printf("\nThe men employeed are\n");
    	for( i = 0; i<num; ++i )
        {	
    		if(s[i].sex != 'F') /*need help sorting by men*/
    		{
    		printf("%s %s %s %s %s %s %s %2d %s %1d %.2f\n", s[i].first, s[i].init, s[i].last, s[i].ad.street, 
    			s[i].ad.city, s[i].ad.state, s[i].ad.zip, s[i].age, s[i].sex, s[i].years, s[i].pay);}
        }}
    
    int topPaywoman(Employee s[], int num)
    {
        int i, j = 0;
        double top = s[0].pay;
        for(i = 1; i < num; ++i)
        {
            if(s[i].pay > top /*&& code to sort by sex still needed*/) { top = s[i].pay; j = i; }
        }
    
        return j;
    }
    
    int lowPayman(Employee s[], int num){
        int i, j = 0;
        double low = s[0].pay;
        for(i = 1; i < num; ++i)
        {
            if(s[i].pay < low /*&& code to sort by sex still needed*/) { low = s[i].pay; j = i; }
        }
    
        return j;
    }
    double avgPay(Employee s[], int num)
    {
        int i;
        double sum = 0;
        for(i = 0; i < num; ++i)
            sum += s[i].pay;
    
        return sum/num;
    }
    
    
    void printBelowavg(Employee s[], int num) {
        int i;
        double avg = avgPay(s, num);
        printf("\nThe women with pay below the average are\n");
    	for( i = 0; i < num; ++i)
        {
            if( s[i].pay < avg /*&& code to sort by sex is needed*/)
                printf("%s %s %s %.2f\n", s[i].first, s[i].init, s[i].last, 
    s[i].pay);
        }
    }
    
    void printRatiomen(Employee s[], int num) {
        int i, above = 0, below = 0;
       	double avg = avgPay(s, num);
        for( i = 0; i < num; ++i)
        {
            above += s[i].pay > avg;
    		below += s[i].pay < avg;
    		
        }/*code to sort by sex is needed*/
    	printf("\nThe ratio of men above average pay to men below average pay is\n%d : %d\n", above, below);
    }
    void printSenior(Employee s[], int num) {
        int i;
        double avg = avgPay(s, num);
        printf("\nThe men who make more than $35000 per year and have worked for over 5 years and are over 30 years old are\n");
    	for( i = 0; i < num; ++i)
        {
            
    		if( s[i].pay > (35000/52) && s[i].years > 5 && s[i].age > 30/*&& code to sort for sex needed*/)
    		{printf("%s %s %.2f\n", s[i].first, s[i].last, s[i].pay);}
        }
    }
    void printRaise(Employee s[], int num) {
        int i, y = 350;
        printf("\nThe employees who get raises are\n");
    	for(i = 0; i < num; ++i)
        {
    		if( s[i].pay < y){
    			printf("%s %s\n", s[i].first, s[i].last);
    			printf("With a raise to %.2f\n", s[i].pay * 1.1);}
        }
    }
    /*void sortzip(Employee arr[], int num)
    {
        int i, j; 
        Employee temp; 
        for(i = 1; i < num; ++i)
        {   strcpy(temp.ad.zip, arr[i].ad.zip);
    
            j = i - 1;
            while(j >= 0 && temp.ad.zip < arr[j].ad.zip)
            {
                strcpy(arr[j+1].ad.zip, arr[j].ad.zip);
    			
    			j--;
            }
    		strcpy(arr[j+1].ad.zip, temp.ad.zip);
    
        }
    }
    */

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    If sorting is too complicated then you could also set up an index with one complete pass of the employee array:

    Code:
    int j = 0 , k = 0;
    for (i = 0; i < whatever; i++) {
       if (tolower (employed[i].sex) == 'm') {
          theMen[j++] = i;
       }
       else {
          theWomen[k++] = i;
       }
    }
    
    void printMensSalaries (Employee employed[] , int theMen[] , int totalMen )
    {
       int i;
       for (i = 0; i < totalMen; i++) {
           printf("%s makes %.2f\n" , employed[ theMen[i] ].last , employed[ theMen[i] ].pay);
       }
    }
    Note that updating the index is a linear algorithm so it is up to you if the trade off is worth it. Certain things will be easier and others will be unaffected.
    Last edited by whiteflags; 05-18-2010 at 12:03 AM.

  3. #3
    Registered User sbaginov's Avatar
    Join Date
    May 2010
    Location
    Italy
    Posts
    19
    Quote Originally Posted by usmcrewcheif View Post
    I have been working on this program and cant figure out how to sort some of the functions by male or others by female and one needs to be sorted by zip code can you please help?
    I would investigate the standard qsort() function that might be used like this:
    Code:
    qsort(Employee, elements_num, sizeof(single employee type), _Employee_cmp);
    Thus you should only pay attention on how to write _Employee_cmp:
    Code:
    int _Employee_cmp(const void *p1, const void *p2) {
        Employee e1 = *((Employee const *) p1);
        Employee e2 = *((Employee const *) p2);
    
        ....
    
        return (key1 < key2 ? -1 : (key1 > key2 ? 1 : 0));
    }
    In this way very complex keys creations/comparisons are possible and you should only create and pass different comparison functions:
    Code:
    int _male_employees_cmp(const void*, const void*);
    int _female_employees_cmp(const void*, const void*);
    int _zip_employees_cmp(const void*, const void*);
    
    qsort(Employee, elements_num, sizeof(single employee type), _male_employees_cmp);
    qsort(Employee, elements_num, sizeof(single employee type), _female_employees_cmp);
    qsort(Employee, elements_num, sizeof(single employee type), _zip_employees_cmp);
    HTH
    Last edited by sbaginov; 05-18-2010 at 01:13 AM. Reason: Further explanation

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Your sorting function should not involve strcpy at all.
    When you want to copy Employees around it's far easier than you think:
    Code:
    Employee a, b;
    
    a = b; // see that wasn't hard was it!
    It should however probably involve strcmp if you actually want to sort based on zip, which must be a nul-terminated string.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help working with functions, structures, and arrays
    By leway in forum C Programming
    Replies: 3
    Last Post: 05-09-2010, 05:24 PM
  2. Need help in structure comparison
    By lokachari in forum C Programming
    Replies: 6
    Last Post: 11-04-2009, 03:55 PM
  3. Zip Code Sorting
    By Shamino in forum C++ Programming
    Replies: 14
    Last Post: 10-26-2009, 04:20 PM
  4. zip code
    By Yarin in forum C++ Programming
    Replies: 3
    Last Post: 03-09-2008, 05:13 PM
  5. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM

Tags for this Thread