Thread: Program Won't Run Quicksort in Main?? in Unix

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    6

    Program Won't Run Quicksort in Main?? in Unix

    Code:
    Main 
    
    #include <stdio.h>
    #include <string.h>
    #include "employeeRecord.h"
    
    #define  REPLINEA   "     Employee            Payrate    RegHours   Gross        Fed       SSI       Net   \n"
    #define  REPLINEB   "     Name                           OvtHours   Pay          State     Defr      Pay   \n"
    #define  REPLINEC   "     ========            =======    ========   ========     =====     =====    ====== \n"
    #define  REPFORMATA "     %-20s%7.2f%12.2f%11.2f%10.2f%10.2f%10.2f\n"
    #define  REPFORMATB "     %39.2f%21.2f%10.2f\n"
    #define  REPFORMATC "     %-20s%7.2f%12.2f%11.2f%10.2f%10.2f%10.2f\n"
    
    
    enum totalsList {FEDTAX, STATETAX, SSITAX, GROSS, HOURS, PAYRATE, DEFERRED, REGULARHOURS, OVERTIMEHOURS,
             EMPLOYEES,NETPAY,TOTALS};
    void printReportHeadings(FILE * reportFile); // 3.1
    void initializeAccumulators(float total[]); //3.2
    void inputEmployeeData(employeeRecord &employeeData); // 3.3
    float calculateGross(employeeRecord &employeeData); // 3.4
    extern void calculateTaxes(employeeRecord &employeeData); // 3.5
    void calcNetPay(employeeRecord &employeeData); // 3.6
    void calcRegularHours(employeeRecord &employeeData); // 3.7
    void calcOvertimeHours(employeeRecord &employeeData); // 3.8
    void QSortEmployee (employeeRecord employeeData[], int start, int finish);
    void printEmployeeData(FILE *reportFile,employeeRecord &employeeData); // 3.9
    void addDetailsToAccumulator(float total[], employeeRecord &employeeData); //3.10
    bool addEmployeeAnsw(); // 3.11
    void printSummaryReport(FILE * reportFile,float  total[]); // 3.12
    void printTotal(FILE *reportFile, float total[]); // 3.12.1
    void printAverage(FILE *reportFile, float total[]); // 3.12.2
    
    int main(void)
    {
        float total[TOTALS];
        FILE * reportFile; // step 1 for a file
        reportFile = fopen("./report.lst","wt"); // step 2 for a file
        printReportHeadings(reportFile); // 3.1
        initializeAccumulators(total); // 3.2
        employeeRecord employeeData[1];
        bool addEmployee = true; 
        while(addEmployee) 
        {
            int i = 0; 
            inputEmployeeData(employeeData[i]); // 3.3
            employeeData[i].gross = calculateGross(employeeData[i]); // 3.4
            calculateTaxes(employeeData[i]); // 3.5
            calcNetPay(employeeData[i]); // 3.6
            calcRegularHours(employeeData[i]); // 3.7
            calcOvertimeHours(employeeData[i]); // 3.8
            QSortEmployee (employeeRecord employeeData[i]); 
            printEmployeeData(reportFile,employeeData[i]); // 3.9
            addDetailsToAccumulator(total, employeeData[i]); // 3.10
             addEmployee = addEmployeeAnsw(); // 3.11 
            i++
        }
        printSummaryReport(reportFile, total); //3.12
    
        fclose(reportFile); // step 4 for a file
        while (getchar() != '\n'); // same as fflush(stdin)
        return 0;
    }
    
    void printReportHeadings(FILE * reportFile) // 3.1
    {
        fprintf(reportFile,REPLINEA);
        fprintf(reportFile,REPLINEB);
        fprintf(reportFile,REPLINEC);
    }
    
    void initializeAccumulators(float total[]) //3.2
    {
        for (int i = 0; i < TOTALS; i++)
        {
            total[i] = 0;
        }
    }
    
    
    void inputEmployeeData(employeeRecord &employee) // 3.3
    {
        printf("  Enter employee's last name: ");
        scanf("%s",employee.lastName);
        printf("  Enter employee's first name: ");
        scanf("%s",employee.firstName);
        printf("  Enter the hours worked: ");
        scanf("%f",&employee.hours);
        printf("  Enter the hourly pay rate: ");
        scanf("%f",&employee.payRate);
        printf("  Enter the deferred earnings: ");
        scanf("%f",&employee.deferred);
    }
    
    void QSortEmployee(employeeRecord employeeData[], int start, int finish)
    {
        int left = start,
        right = finish;
        char * pivot = employeeData[(start + finish) / 2].lastName;
        while (left < right)
        {
            while ((strcmp(employeeData[left].lastName, pivot) < 0) && (left < right))
        {
            left++;
        }
        while ((strcmp(employeeData[right].lastName, pivot) > 0) && (right > left))
        {
            right--;
        }
        if (left <= right)
        {
            employeeRecord tempEmployee = employeeData[left];
            employeeData[left] = employeeData[right];
            employeeData[right] = tempEmployee;
            left++;
            right--;
        }
    }
    if (start < finish)
    {
        QSortEmployee(employeeData, start, right);
    }
    if (left < finish)
    {
        QSortEmployee(employeeData, left, finish);
    }
    }
    
    float calculateGross(employeeRecord &employee) // 3.4
    {
        if (employee.hours <= 40)
          return employee.hours * employee.payRate;
        else
          return (40 * employee.payRate) + (1.5*employee.payRate*(employee.hours-40));
    }
    
    void calcNetPay(employeeRecord &employee) // 3.6
    {
    
        employee.netPay = employee.gross-employee.fedTax-employee.stateTax-
                    employee.ssiTax-employee.deferred;
    }
    
    
    void calcRegularHours(employeeRecord &employee) // 3.7
    {
        employee.regularHours = (employee.hours < 40? employee.hours:40);
    
    }
    
    
    void calcOvertimeHours(employeeRecord &employee) // 3.8
    {
        employee.overtimeHours = (employee.hours <= 40? 0:employee.hours -40);
    }
    
    
    void printEmployeeData(FILE *reportFile,employeeRecord &employee) // 3.9
    {
        char fullName[15+2+10+1];
    
        strcpy(fullName, employee.lastName);
        strcat(fullName, ", ");
        strcat(fullName, employee.firstName);
        fprintf(reportFile,REPFORMATA,fullName,employee.payRate,employee.regularHours,
            employee.gross,employee.fedTax,employee.ssiTax,employee.netPay);
        fprintf(reportFile,REPFORMATB,employee.overtimeHours,employee.stateTax,employee.deferred);
    }
    
    
    void addDetailsToAccumulator(float tot[], employeeRecord &employee) //3.10
    {
        tot[FEDTAX] += employee.fedTax;
        tot[STATETAX] += employee.stateTax;
        tot[SSITAX] += employee.ssiTax;
        tot[GROSS] += employee.gross;
        tot[HOURS] +=  employee.hours;
        tot[PAYRATE] +=  employee.payRate;
        tot[DEFERRED] += employee.deferred;
        tot[NETPAY] += employee.netPay;
        tot[REGULARHOURS] += employee.regularHours;
        tot[OVERTIMEHOURS] += employee.overtimeHours;
        tot[EMPLOYEES] += 1;
    }
    
    
    bool addEmployeeAnsw() // 3.11
    {
        char continueAnsw;
    
        while (getchar() != '\n'); // same as fflush(stdin)
        printf("continue (y/n)? ");
        scanf("%c", &continueAnsw);
        if (continueAnsw == 'n' || continueAnsw == 'N')
            return false;
        else
            return true;
    }
    
    
    void printSummaryReport(FILE * reportFile, float tot[]) // 3.12
    {
        printTotal(reportFile, tot); // 3.12.1
        printAverage(reportFile, tot); // 3.12.2
    }
    
    
    void printTotal(FILE *reportFile, float tot[]) // 3.12.1
    {
        fprintf(reportFile,REPFORMATC,"Total",tot[PAYRATE],tot[REGULARHOURS],tot[GROSS],tot[FEDTAX],tot[SSITAX],tot[NETPAY]);
        fprintf(reportFile,REPFORMATB,tot[OVERTIMEHOURS],tot[STATETAX],tot[DEFERRED]);
    }
    
    
    void printAverage(FILE *reportFile, float tot[]) // 3.12.2
    {
        fprintf(reportFile,REPFORMATC,"Average",tot[PAYRATE]/tot[EMPLOYEES],tot[REGULARHOURS]/tot[EMPLOYEES],
            tot[GROSS]/tot[EMPLOYEES],tot[FEDTAX]/tot[EMPLOYEES],tot[SSITAX]/tot[EMPLOYEES],
            tot[NETPAY]/tot[EMPLOYEES]);
    
        fprintf(reportFile,REPFORMATB,tot[OVERTIMEHOURS]/tot[EMPLOYEES],tot[STATETAX]/tot[EMPLOYEES],
            tot[DEFERRED]/tot[EMPLOYEES]);
    }
    
    
    // Header File 
    typedef char STR15[15+1];
    typedef char STR10[10+1];
    typedef struct employeeRecord
    {
        STR15 lastName;
        STR10 firstName;
        float hours,
            payRate,
            deferred,
            gross,
            fedTax,
            stateTax,
            ssiTax,
            netPay,
            regularHours,
            overtimeHours;
    
    } employeeRecord;
    
    //Extern File
    #include "employeeRecord.h"
    
    
    
    #define FEDTAXRATE 0.15
    
    #define STATETAXRATE 0.07
    
    #define SSITAXRATE 0.0775

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work? It would be better if you posted the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should also post any warnings/errors you receive.

    Code:
    void inputEmployeeData(employeeRecord &employeeData); // 3.3
    float calculateGross(employeeRecord &employeeData); // 3.4
    extern void calculateTaxes(employeeRecord &employeeData); // 3.5
    void calcNetPay(employeeRecord &employeeData); // 3.6
    void calcRegularHours(employeeRecord &employeeData); // 3.7
    void calcOvertimeHours(employeeRecord &employeeData); // 3.8
    If you didn't receive any errors with the above, you might be doing something wrong. Perhaps trying to compile as C++ instead of C?

  4. #4
    Registered User
    Join Date
    Jul 2015
    Posts
    6
    the QSortEmployee didn't run everything else is fine

  5. #5
    Registered User
    Join Date
    Apr 2014
    Posts
    10
    Quote Originally Posted by Jimi T Sun Li View Post
    the QSortEmployee didn't run everything else is fine
    You might want to double check your brackets in that function.

  6. #6
    Registered User
    Join Date
    Jul 2015
    Posts
    6
    Let me see if I alligned the bracket on the function .....

  7. #7
    Registered User
    Join Date
    Jul 2015
    Posts
    6
    Code:
    void QSortEmployee(employeeRecord employeeData[], int start, int finish)
    {
        int left = start,
        right = finish;
        char * pivot = employeeData[(start + finish) / 2].lastName;
        while (left < right)
        {
            while ((strcmp(employeeData[left].lastName, pivot) < 0) && (left < right))
            {
                left++;
            }
                while ((strcmp(employeeData[right].lastName, pivot) > 0) && (right > left))
                {
                    right--;
                }
            if (left <= right)
            {
                employeeRecord tempEmployee = employeeData[left];
                employeeData[left] = employeeData[right];
                employeeData[right] = tempEmployee;
                left++;
                right--;
            }
        }
        if (start < finish)
        {
            QSortEmployee(employeeData, start, right);
        }
        if (left < finish)
        {
            QSortEmployee(employeeData, left, finish);
        }
    }
    Still doesn't work

  8. #8
    Registered User
    Join Date
    Jul 2015
    Posts
    6
    The QSortEmployee function call in main contains an errror... "cannot convert 'employeeRecord' to 'employeeRecord*' for argument '1' to 'void QSortEmployee(employeeRecord

  9. #9
    Registered User
    Join Date
    Jul 2015
    Posts
    6
    [Error] cannot convert 'employeeRecord' to 'employeeRecord*' for argument '1' to 'void QSortEmployee(employeeRecord*, int, int)'

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Jimi T Sun Li View Post
    [Error] cannot convert 'employeeRecord' to 'employeeRecord*' for argument '1' to 'void QSortEmployee(employeeRecord*, int, int)'
    So, fix the call of the function so it is correct.

    Edit: While fixing the call think about where the call to the sort function should be; is it in the correct location?

    Tim S.
    Last edited by stahta01; 07-25-2015 at 05:42 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 09-29-2010, 12:18 PM
  2. How to program in unix
    By Cpro in forum Linux Programming
    Replies: 21
    Last Post: 02-12-2008, 10:54 AM
  3. Call a remote C# program from a C program on unix
    By goup in forum C Programming
    Replies: 4
    Last Post: 11-09-2006, 07:46 PM
  4. unix `ls' command in C program
    By Moony in forum C Programming
    Replies: 3
    Last Post: 06-28-2006, 10:00 AM
  5. Using quicksort and radix sort for an anagram program
    By RazielX in forum C Programming
    Replies: 2
    Last Post: 05-03-2004, 09:33 AM

Tags for this Thread