I am having trouble with this. How to properly do this?

I need some help with this, please.

The directions were:

Expand the payroll program to combine two sorting techniques (Selection and Exchange sorts) for better efficiency in sorting the employee’s net pay.

This is a continuation. Here is my previous coding for the one prior:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <iostream>
#include <fstream>
#include <iomanip>


using namespace std;

class payroll{
public:
   int employeeid;
   string firstname, lastname;
   char taxstatus, typeemp;
   double hoursworked, hourlyrate, overtimehours, overtimepay, grosspay, grosssalary, taxrate, taxamount, netpay, minnp, maxnp;
   double minnet(double, int);
   double maxnet(double, int);
   void printminmax(double, double);
};//end class

class Employee :public payroll{
public:
   ifstream input;
   payroll empi[100];
   int counter;
   void headers();
   virtual void grosspaycalc() = 0;
   virtual void taxratecalc() = 0;
   virtual void netpaycalc() = 0;
   virtual void employee_report() = 0;
   virtual void displayinfo() = 0;
   void netpaysort();
};//end

class Employee_Pay : public Employee{
public:
   Employee_Pay();//constructor
   ~Employee_Pay();//destructor
   void grosspaycalc(){
       for (int i = 0; i<counter; i++) {
           if(empi[i].typeemp == 'S')
           {
           double gross_pay = empi[i].grosspay/52; 
           empi[i].hourlyrate = gross_pay/40; 
           if (empi[i].hoursworked>40) {
           empi[i].overtimehours = empi[i].hoursworked - 40;
           empi[i].overtimepay = 1.5*empi[i].overtimehours*empi[i].hourlyrate;
           }
           else
           {
            empi[i].overtimehours = 0;
           empi[i].overtimepay = 0;
           }
           }
           else if (empi[i].hoursworked>40){
               empi[i].overtimehours = empi[i].hoursworked - 40;
               empi[i].overtimepay = 1.5*empi[i].overtimehours*empi[i].hourlyrate;
               empi[i].grosspay = empi[i].overtimepay + (empi[i].hoursworked*empi[i].hourlyrate);
           }//end if
           else
           {
           empi[i].overtimehours = 0;
           empi[i].overtimepay = 0;
           empi[i].grosspay = empi[i].hoursworked*empi[i].hourlyrate;
           }
       }//end for
   }//end grosspay
   
   void taxratecalc(){
       for (int i = 0; i<counter; i++) {
           if (empi[i].grosspay>1000) {
               empi[i].taxrate = .30;
           }//end if
           if (empi[i].grosspay>800 && empi[i].grosspay <= 1000) {
               empi[i].taxrate = .20;
           }//end if
           if (empi[i].grosspay>500 && empi[i].grosspay <= 800) {
               empi[i].taxrate = .10;
           }//end if
           if (empi[i].grosspay >= 0 && empi[i].grosspay <= 500) {
               empi[i].taxrate = 0;
           }//end if
           if ((empi[i].taxstatus == 'H' || empi[i].taxstatus == 'h') && (empi[i].grosspay>500)){
               empi[i].taxrate = empi[i].taxrate - .05;
           }//end if
           if (empi[i].taxstatus == 'S' || empi[i].taxstatus == 's'){
               empi[i].taxrate = empi[i].taxrate + .05;
           }//end if
           if (empi[i].taxstatus == 'M' || empi[i].taxstatus == 'm'){
               empi[i].taxrate = empi[i].taxrate;
           }//end if
       }//end for
   }//end taxrate
   
   void netpaycalc(){
       for (int i = 0; i<counter; i++) {
           empi[i].taxamount = empi[i].grosspay*empi[i].taxrate;
           empi[i].netpay = empi[i].grosspay - empi[i].taxamount;
       }//end for
   }//end
   
   void displayinfo(){
    
       //if (empi[i].typeemp == 'N' || empi[i].typeemp == 'n') {
           cout << setiosflags(ios::fixed | ios::showpoint | ios::left) << setprecision(2);
            for(int i=0; i<counter; i++)
           cout << setw(10) << empi[i].employeeid << setw(12) << empi[i].firstname << setw(12) << empi[i].lastname << setw(17) << empi[i].taxstatus << setw(12) << empi[i].typeemp << setw(12) << empi[i].hoursworked << setw(12) << empi[i].hourlyrate << setw(12) << empi[i].grosspay << setw(15) << empi[i].overtimehours << setw(14) << empi[i].overtimepay << setw(12) << empi[i].taxrate * 100 << setw(12) << empi[i].taxamount << setw(12) << empi[i].netpay << endl;
       //}//end if
   }//end
   
   void employee_report(){
       grosspaycalc();
       taxratecalc();
       netpaycalc();
      sortnetpay();
   }//end input
};//end Employee_Pay class

Employee_Pay::Employee_Pay():Employee(){
   input.open("pay.txt");
   counter=0;
   while (input >> empi[counter].firstname >> empi[counter].lastname >> empi[counter].employeeid >> empi[counter].taxstatus >> empi[counter].typeemp
   >> empi[counter].hoursworked >> empi[counter].hourlyrate >> empi[counter].grosspay) {
       counter++;
   }//end while
}//end of constructor
Employee_Pay::~Employee_Pay(){
   input.close();
}// end destructor
//end function

void headers();
int main(int argc, char * argv[]){
   const int MAXSIZE = 70;
   Employee_Pay hourly_report;
   
   cout << setw(60) << "PAYROLL SYSTEM" << endl;
	
   << endl;
   headers();
   //for (int i = 0; i<MAXSIZE; i++) {
       hourly_report.employee_report();
       hourly_report.displayinfo();
   //}//end for
   return 0;
}// end main

void headers(){
   cout << setiosflags(ios::fixed | ios::showpoint | ios::left);
   cout << setw(10) << "EMP ID" << setw(12) << "FIRST NAME" << setw(12) << "LAST NAME" << setw(17) << "MARITAL STATUS" << setw(12) << "PAY TYPE" << setw(12) << "HOUR WORK" << setw(12) << "HOUR RATE" << setw(12) << "GROSS PAY" << setw(15) << "OVERTIME HOUR" << setw(14) << "OVERTIME PAY" << setw(12) << "TAX RATE" << setw(12) << "TAX AMOUNT" << setw(12) << "NET PAY" << endl;
  
   << endl;
}//end


//sort netpay
void Employee::netpaysort(){
  for (int pass = 1; pass <= counter; pass = pass + 1) {
       for (int i = 0; i<counter - 1; i = i + 1) {
           if (empi[i].netpay<empi[i + 1].netpay) {
              swap(empi[i].netpay, empi[i + 1].netpay);
          }//end if
       }//end for
  }//end for
}//end

//minimum netpay
double payroll::minnet(double minnp, int n){
    if(n==0){
        minnp=netpay;
    }
    if(netpay<minnp){
        minnp=netpay;
    }
    cout<<endl<<"The minimum net pay for "<<n<<" employees is "
<<setw(8)<<setprecision(2)<<fixed<<left<<showpoint<<minnp<<endl;
    return minnp; 
}//end minimum


//maximum netpay
double payroll::maxnet(double maxnp, int n){
    if (n==0){
        maxnp=netpay;
    }
    if(netpay>maxnp){
        maxnp=netpay;
    }
    cout<<endl<<"The maximum net pay for "<<n<<" employees is "
<<setw(8)<<setprecision(2)<<fixed<<left<<showpoint<<minnp<<endl;
	 return maxnp;
}//end maximum
Last edited on
replace sortnetpay twice. so you end up with 3 functions (ideally rename the old one) eg
sortnetpay_bs, .._es ..._ss (abbreviations for the 3 sorts) and where you call that just pick the one you want. you can, alternately, put all 3 sorts into 1 routine and pass in which one to use, but its a bit messy (its ok to have the messy all in one here if it does not bother you: when testing algorithms or performance tweaks you can have the same code written a few ways but eventually you weed out the junk until you keep only the best approach, but for this homework they want you to not weed out the junk yet).

a lot of professors obsess over the minor differences in the very slow sorts.
@jonnin

I was just given this code

//Sort the employee object array in ascending



//initial loop to the netPay array


Since this is a continuation, where on the code that I had listed do I insert this in? This is my first semester in C++?
Last edited on
what did the prof say to do? Its a function and stray code. The stray code can go in a new function. Try following the associated instructions and if they do not make sense, post them here so we can try to see what is wanted.

also please use code tags, you can edit your old posts too. use <> on the format bar or the word code and \code in [] to make it formatted and readable.
This is the direction:

"The first is to run the Exsel sort from the text and send it to the professor and then change the program into class and object. You are able to swap the entire object (wow).

The third step is to incorporate your Exsel sort into your previous compiling assignment."

The code that I listed on the original post is the code that I had compiled previously. I am extremely RAW at this......I am absolutely lost.
oh, ok. it sounds like he wants you to write the same sort you did in excel in c++ as a function, like the bubble sort you wrote in the existing code. It sounds like it needs to swap the whole objects, etc.
Last edited on
I have been trying and legitimately don't know how.
ok, assuming you have done the first part (something in excel and sent back)
can you write the new sort, maybe just for an array or vector of integers to start out, into a new function? If not, can you state clearly what you do not understand?
I have added a new sort and now I have two sorts - netpaysort1 and netpaysort2. My output shows netpay in ascending order. I think that's what I was asked to do for using two sorting techniques (Selection and Exchange sorts).

I am now working on the minimum and maximum netpay parts, which are not showing in the output. Can you please suggest what to fix in the code above.
if you have sorted by net pay, the max and min are the first and last items in the container, no need to do anything more.
As you see in the code, I am trying to add two results at the and of output -- something like:

maximum net pay for 10 employees is:
minimum net pay for 10 employees is:
yes, I see it. But I can't really make a lot of sense from it.
what do you get with (ignoring the formatting variables for a moment ... grumble printf ftw... )
cout << "minimum net pay for 10 employees is: " << employee[0]->netPay << endl?
and max is [9] instead of [0]? This assumes you sorted it first, but you did, as far as I can tell?
Last edited on
In fact, it is maximum and minimum net pay for all employees. I just mentioned 10 as an example.
Yes, I fixed the sorting part and output shows netpay in ascending order.
I am now trying to fix the code so that at the end of output shows "maximum and minimum net pay for all employees".
Topic archived. No new replies allowed.