2
\$\begingroup\$

This program is supposed to prompt the user for hours and wage of 4 employees. Then the program will use a selection sort to sort the data into ascending value using the wages. The information like employee id and hours and what not should be adjusted to match the sorted data. My program works but are there improvements that can be made since I did have to create 4 minValue variables?

#include "stdafx.h" 
#include <iostream>
#include <iomanip>
using namespace std;

// Constant for the array size.
const int ARRAY_SIZE = 4;

// Function Prototypes
void getEmployeeInfo(long [], int [], double [], double [], int);
//void bubbleSort(long empId[],int hours[],double payRate[],double wages[],int size); 
void selectionSort(long empId[],int hours[],double payRate[],double wages[],int size); 
void displayWages(long empId[], double wages[], int size);

int main()
{
    // Array of employee ID numbers
    long empId[ARRAY_SIZE] = { 5658845, 4520125, 7895122,
                               8777541};

    // Array to hold the hours worked for each employee
    int hours[ARRAY_SIZE] = {0};

    // Array to hold the hourly pay rate for each employee
    double payRate[ARRAY_SIZE] = {0};

    // Array to hold the gross wages for each employee
    double wages[ARRAY_SIZE] = {0};

    // Get the employee payroll information and store
    // it in the arrays.
    getEmployeeInfo(empId, hours, payRate, wages, ARRAY_SIZE);

    // Display the UNSORTED payroll information.
    cout << "This is the unsorted payroll information: " << endl; 
    displayWages(empId, wages, ARRAY_SIZE);

    // Sort the payroll information in ascending order with a bubble sort. 
    // bubbleSort(empId, hours, payRate, wages, ARRAY_SIZE); 

    // Sort the payroll information with in ascending order a selection sort. 
    selectionSort(empId, hours, payRate, wages, ARRAY_SIZE); 

    // Display the SORTED payroll information. 
    cout << "This is the sorted payroll information: " << endl; 
    displayWages (empId, wages, ARRAY_SIZE); 

    system("PAUSE"); 

    return 0;
}

// ********************************************************
// The getEmployeeInfo function receives four parallel    *
// arrays as arguments. The 1st array contains employee   *
// IDs to be displayed in prompts. It asks for input and  *
// stores hours worked and pay rate information in the    *
// 2nd and 3rd arrays. This information is used to        *
// calculate gross pay, which it stores in the 4th array. *
// ********************************************************
void getEmployeeInfo(long emp[], int hrs[], double rate[],
                     double pay[], int size)
{
    cout << "Enter the requested information "
         << "for each employee.\n";

    // Get the information for each employee.
    for (int count = 0; count < size; count++)
    {
        cout << "\nEmployee #: " << emp[count] << "\t";

        // Get this employee's hours worked.
        cout << "Hours worked: ";
        cin  >> hrs[count];

        // Validate hours worked.
        while (hrs < 0)
        {
            cout << "\nHours worked must be 0 or more. "
                 << "Please re-enter: ";
            cin  >> hrs[count];
        }

        // Get this employee's pay rate.
        cout << "\tPay rate: $";
        cin  >> rate[count];

        // Validate the pay rate.
        while (rate[count] < 6.00)
        {
            cout << "\nPay rate must be 6.00 or more. "
                 << "Please re-enter: $";
            cin  >> rate[count];
        }

        // Calculate this employee's gross pay.
        pay[count] = hrs[count]*rate[count]; 

        // ADD statement to calculate wages by multiplying
                // hours with rate of pay;
    }
}

// ********************************************************
// The bubbleSort function sorts the information based on *
// the wages array.                                       *
// ********************************************************
//void bubbleSort(long empId[],int hours[],double payRate[],double wages[],int size)
//{
//    bool swap; 
//    double temp1; 
//    int temp2;
//    long temp3;
//
//    do
//    {
//        swap = false; 
//        for (int count = 0; count < (size - 1); count++)
//        {
//            // compare the wages, because that is what you are sorting on
//            if (wages[count] > wages [count + 1])
//            {
//                // swap all the data between the two array positions:
//                temp1 = wages[count]; 
//                wages[count] = wages[count + 1]; 
//                wages[count + 1] = temp1; 
//
//                temp1 = payRate[count]; 
//                payRate[count] = payRate[count + 1]; 
//                payRate[count + 1] = temp1;
//
//                temp2 = hours[count]; 
//                hours[count] = hours[count + 1]; 
//                hours[count + 1] = temp2; 
//
//                temp3 = empId[count]; 
//                empId[count] = empId[count + 1]; 
//                empId[count + 1] = temp3; 
//
//                swap = true; 
//            }
//        }
//
//    } while (swap); 
//}

// ********************************************************
// The selectionSort function sorts the information based *
// on the wages array.                                    *
// ********************************************************
void selectionSort(long empId[],int hours[],double payRate[],double wages[],int size)
{
    int startScan, minIndex; 
    double minValue1, minValue2; 
    int minValue3; 
    long minValue4; 

    for (startScan = 0; startScan < (size - 1); startScan++)
    {
        minIndex = startScan; 
        minValue1 = wages[startScan]; 
        minValue2 = payRate[startScan]; 
        minValue3 = hours[startScan]; 
        minValue4 = empId[startScan]; 

        for (int index = startScan + 1; index < size; index++)
        {
            if (wages[index] < minValue1)
            {
                minValue1 = wages[index]; 
                minIndex = index; 

                minValue2 = payRate[index]; 
                minIndex = index; 

                minValue3 = hours[index]; 
                minIndex = index; 

                minValue4 = empId[index]; 
                minIndex = index; 
            }
        }
        wages[minIndex] = wages[startScan];
        wages[startScan] = minValue1;

        payRate[minIndex] = payRate[startScan];
        payRate[startScan] = minValue2;

        hours[minIndex] = hours[startScan]; 
        hours[startScan] = minValue3; 

        empId[minIndex] = empId[startScan]; 
        empId[startScan] = minValue4; 
    }
}

// ********************************************************
// The displayWages function displays employee ID numbers *
// and their wages.                                       *
// ********************************************************
void displayWages(long empId[], double wages[], int size)
{
    // Set up the numeric output formatting.
    cout << fixed << showpoint << setprecision(2) << endl;

    // Display the header.
    cout << "----------------------------\n";
    cout << "Employee               Wages\n";
    cout << "----------------------------\n\n";

    // Display each employee's pay.
    for (int count = 0; count < ARRAY_SIZE; count++)
    {
        cout << "Employee #" << empId[count] << "   $";
        cout << setw(7) << wages[count] << endl << endl;
    }

}
\$\endgroup\$
  • \$\begingroup\$ Have you learned about structs/classes yet? Your program would look so much better if you used an Employee struct or class. \$\endgroup\$ – JS1 Apr 7 '15 at 4:34
  • \$\begingroup\$ No my professor has not introduced classes to us yet. \$\endgroup\$ – Duck Apr 7 '15 at 4:36
3
\$\begingroup\$
  1. Please avoid the usage of namespace as follows

    using namespace std;
    

    Instead call it whenever necessary like

    std::cout << std::fixed << std::showpoint << std::setprecision(2) << "\n";
    
  2. To improve the performance instead use "\n" instead of std::endl
  3. Integral variables should be pre initialized and declare each in separate line to get better readability

    int minIndex = 0;
    double minValue1 =0.0;
    
  4. If possible declare the iterator variable within for loop itself:

    int  startScan ;
    for (startScan = 0; startScan < (size - 1); startScan++)
    

    use as follows:

    for (int startScan = 0; startScan < (size - 1); startScan++)
    
  5. In case of algorithm make following change to remove unnecessary assignment operations

    for (startScan = 0; startScan < (size - 1); startScan++)
    {
        minIndex = startScan; 
        minValue1 = wages[startScan]; 
    
        for (int index = startScan + 1; index < size; index++)
        {
            if (wages[index] < minValue1)
            {
               minIndex = index; 
            }
        }
    
        std::swap(wages[minIndex], wages[startScan]);
        std::swap(payRate[minIndex], payRate[startScan]);
        std::swap(hours[minIndex], hours[startScan]);
        std::swap(empId[minIndex], empId[startScan]);
    }
    
\$\endgroup\$
0
\$\begingroup\$

Create another array, calling it mapping, initially consisting of the numbers 0 to ARRAY_SIZE - 1, and then move entries around in just that one instead of moving around the four separate values. One change you have to make is, instead of looking at wages[index] to do the sort, look at wages[mapping[index]]. The other change is in the function when you're printing out the information, inside the brackets should be mapping[index] instead of just index.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.