Question

I have to organize my array with a bubble sort to display who works the most hours to who works the least amount of hours. After that, I need to display the employee who makes the most amount of pay in the company. However, I'm not sure how I would go about using a bubble sort with pointer variables and a structure member. At the moment I have a generic code for my bubble sort in the arraySort function but it needs improvement. Thank you!

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std; 

struct incomeInfo {
    string id;
    string name;
    int hours;
    double hRate;
    double regPay = 0;
    double otPay = 0;
};

const int ARRAY_SIZE = 25; 
incomeInfo income[ARRAY_SIZE]; 

void getIncome(incomeInfo[], int&);
void compute(incomeInfo *, int);
void display(incomeInfo[], int);
void summary(incomeInfo[], int);
void sortArray(incomeInfo[], int);
void mostPay(incomeInfo[], int);

int main()
{

    incomeInfo income[ARRAY_SIZE];
    int count = 0;                          

    getIncome(income, count);
    compute(income, count);
    display(income, count);
    summary(income, count);
    sortArray(income, count);
    mostPay(income, count);

    return 0;
}

void getIncome(incomeInfo income[], int &count)
{

    ifstream inputFile;                 
    char line[50];                      


    inputFile.open("Payroll.txt");


    if (inputFile.fail())
    {
        cout << "\n\n\tError openning file: " << "\n\n\t";
        system("pause");
        exit(1);
    }

    else
    {
        while (!inputFile.eof())    
        {
            inputFile.getline(line, 50, ',');        
            income[count].id = line;
            inputFile.getline(line, 50, ',');
            income[count].name = line;
            inputFile.getline(line, 50, ',');
            income[count].hours = atoi(line);       
            inputFile.getline(line, 50, ',');
            income[count].hRate = atof(line);           

            count++;
        }
    }
    inputFile.close();

    return;
}


void compute(incomeInfo *ptrI, int count)    
{                                                
    for (int i = 0; i<count; i++)

    if (ptrI->hours <= 40)
    {
        ptrI->regPay = ptrI->hours * ptrI->hRate;
        ptrI++;
    }

    else if (ptrI->hours > 40)
    {
        ptrI->regPay = 40 * ptrI->hRate;
        ptrI->otPay = (ptrI->hours - 40) * (ptrI->hRate + (ptrI->hRate* .5));
        ptrI++;
    }
        return;
}


void display(incomeInfo income[], int count)
{
    cout << fixed << showpoint << setprecision(2);
    cout << setw(15) << left << "ID" << setw(16) << "Name";
    cout << left << setw(8) << "Hours" << setw(14) << "Hourly Rate" << setw(14) << "Regular Pay" << setw(14) << "Overtime Pay" <<endl;


    for (int i = 0; i < count; i++)
    {
        cout << setw(14) << left << income[i].id << setw(15) << income[i].name;
        cout << right << setw(6) << income[i].hours << setw(12) << income[i].hRate;
        cout << setw(14) << income[i].regPay << setw(14) << income[i].otPay << endl;
    }

    return;
}


void sortArray(incomeInfo income[], int count)
{
    {
        bool swap;
        int temp;

        do
        {
            swap = false;
            for (int count = 0; count < (size - 1); count++)
            {
                if (array[count] > array[count + 1])
                {
                    temp = array[count];
                    array[count] = array[count + 1];
                    array[count + 1] = temp;
                    swap = true;
                }
            }
        } while (swap);
    }

}


void summary(incomeInfo income[], int count)
{
        cout << endl << endl << "Total payroll amount for the company = $";
        cout << (income[0].regPay + income[0].otPay) + (income[1].regPay + income[1].otPay) + (income[2].regPay + income[2].otPay) + (income[3].regPay + income[3].otPay) + (income[4].regPay + income[4].otPay) << endl;
}


void mostPay(incomeInfo[], int count)
{
    cout << endl << endl << "Employee who earns most money: ";
}
Was it helpful?

Solution 2

Your code won't compile in its current state since you reference 'size' and 'array' variables that do not exist, but here's what a bubble sort looks like:

void sortArray(incomeInfo income[], int count) {
    for (int i = 0; i < count; i++)
        for (int j = 1; j < count; j++)
            if (income[j - 1].hours > income[j].hours)
                std::swap(income[j - 1], income[j]);
}

OTHER TIPS

The logic for the bubble sort would be the same, except you would be using an array of pointers. Only the if statement needs to change, so that it compares using the values in the structures that the array pointers point to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top