How to quickly sort user input

The question is pretty simple, but the more I search, the more confused I am. I have to quickly sort the variable from the structure code I wrote. However, I don't understand how I should quickly sort a variable of type em.fName if it changes every iteration of the loop. Every example I'm looking for seems to sort int variables or predefined character arrays. Can anyone point me in the right direction where to start and / or provide an example in C? Thank you very much. I posted the complete code here as I didn't want to clutter up the post.

    typedef struct EmployeeRecord
    {
    STR21 lName;
    STR11 fName;
    char fullName[33];
    float hrs;
    float pay;
    float dfr;
    float gross;
    float ft;
    float st;
    float ssit;
    float net;

    } EmployeeRecord;

int main(void)
{
    EmployeeRecord em;

    do
    {
        counter++;
        em.dfr = em.ft = em.st = em.ssit = em.gross = em.hrs = em.pay = em.net = ovrtHrs = 0; //initalized to 0 and then changed by function if greater than 0
        printf("Please enter your FIRST name: ");
        scanf("%s",fName);
        printf("Please enter your LAST name: ");
        scanf("%s",lName);
        printf("Enter your payrate: ");
        scanf("%f",&em.pay);
        printf("Please enter your total hours: ");
        scanf("%f",&em.hrs);
        printf("Please enter the amount of deferred earnings: ");
        scanf("%f",&em.dfr);

        strcpy(fullName,lName); // combine last + ", " + first into full
                strcat(fullName,", ");
                strcat(fullName,fName);

        payTotal = payTotal + em.pay;
        dfrTotal = dfrTotal + em.dfr;

        em.gross = grossModule(&em.hrs,em.pay,&ovrtHrs); // call 3.4
        CalculateTaxes(&em.gross,&em.dfr,&em.ft,&em.st,&em.ssit); // call 3.0
        em.net = netModule(&em.gross,&em.ft,&em.st,&em.ssit); // cal 3.5

        hoursTotal = hoursTotal + em.hrs;
        overtimeTotal = overtimeTotal + ovrtHrs; // TOTALS
        grossTotal = grossTotal + em.gross;
        stateTotal = stateTotal + em.st;
        ssiTotal = ssiTotal + em.ssit;
        netTotal = netTotal + em.net;
        fedTotal = fedTotal + em.ft;

        fprintf(myreport,REPORTCOLUMNFORMATS,em.fullName,em.pay,em.hrs,em.gross,em.ft,em.ssit,em.net);
        fprintf(myreport,REPORTCOLUMNFORMATS3,ovrtHrs,em.st,em.dfr);


        printf("Would you like to continue, y/n? \n");
        scanf("%s",&ask);

    }while(ask == 'y' || ask == 'Y');

      

+3


source to share


1 answer


Here are some pointers:



  • You only read one EmployeeRecord and then overwrite it, if you want to sort it you have to keep them all. You can work with realloc

    to dynamically resize the reserved space or keep it simple:

    EmployeeRecord employees[100]; // will fail if more than 100 employees are added
    ...
    // after em is complete:
    employees[counter++] = em; // remove the other counter++
    
          

  • The sort is taken from wikibooks.org and replaced pointers with normal int, working with index instead of pointer math (easier to understand) and adding a comparison function that you will have to fill in:

    void swap(int a, int b)
    {
        EmployeeRecord tmp = employees[a];
        employees[a] = employees[b];
        employees[b] = tmp;
    }
    
    int compare(int a, int b)
    {
        // return -1 if employees[a] comes before employees[b]
        // return  1 if employees[a] comes after employees[b]
        // return  0 "if they have the same order"
        //           (stable algorithms e.g. mergesort won't change the order of those)
    }
    
    void quicksort(int begin, int end)
    {
        int ptr;
        int split;
        if (end - begin <= 1)
            return;
        ptr = begin;
        split = begin + 1;
        while (++ptr <= end) {
            if (compare(ptr, split) > 0) { // sorts ascending
                swap(ptr, split);
                ++split;
            }
        }
        swap(begin, split - 1);
        quicksort(begin, split - 1);
        quicksort(split, end);
    }
    
          

  • Sorting and generating a report after the user has decided not to continue anymore.

+2


source







All Articles