Regular Sort of my Netpay

So I'm digging deeper into my payroll program and learning new things as I go. Last thing I did was find the average netpay of my employees from my input file now I need to sort the netpay with a regular sort. I made a sample sort that runs fine and my payroll program runs fine:


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
#include <iostream>
#include<fstream>//file input output stream
using namespace std; 

//function prototypes
int readalldata(long int[], int[], float[], const int);
void findovertimehours(int[], int[], int);
void findovertimepay(int[], float[], float[], int);
void findregularhours(int[], int[], int);
void findregularpay(int[], float[], float[], int);
void findgrosspay(float[], float[], float[], int);
void findtaxrate(float[], float[], int);
void findtaxamount(float[], float[], float[], int);
void findnetpay(float[], float[], float[], int);
void printalldata(long int[], int[], float[], float[], float[], float[], float[], int);

int main(){
     const int MAXSIZE=100;   //for maximum of 100 employees
     
     //decleration of variables
     int n;
     long int id[MAXSIZE];
     int hoursworked[MAXSIZE], overtimehours[MAXSIZE];
     int regularhours[MAXSIZE];
     float hourlyrate[MAXSIZE], regularpay[MAXSIZE],
     overtimepay[MAXSIZE], grosspay[MAXSIZE];
     float taxrate[MAXSIZE], taxamount[MAXSIZE], netpay[MAXSIZE];
     
     //function calls
     n=readalldata(id, hoursworked, hourlyrate, MAXSIZE); //get all data
     findovertimehours(hoursworked, overtimehours, n);
     findovertimepay(overtimehours, hourlyrate, overtimepay, n);
     findregularhours(hoursworked, regularhours, n);
     findregularpay(regularhours, regularpay, hourlyrate, n);
     findgrosspay(regularpay, overtimepay, grosspay, n);
     findtaxrate(grosspay, taxrate, n);
     findtaxamount(grosspay, taxamount, taxrate, n);
     findnetpay(grosspay, netpay, taxamount, n);
     printalldata(id, hoursworked, hourlyrate, overtimepay, 
                      grosspay, taxamount, netpay, n);
                      return 0;
}//MAIN
//function definitions
int readalldata(long int id[], int hoursworked[], float hourlyrate[], int n){
    ifstream fin("employee.txt");
    n=0;
    
    while(fin>>id[n]>>hoursworked[n]>>hourlyrate[n]) n++;
    
    fin.close();
    return n;
}//READALLDATA

void findovertimehours(int hoursworked[], int overtimehours[], int n){
     for(int i=0; i<n; i++){
             if(hoursworked[i]>40) overtimehours[i]=hoursworked[i]-40;
             else overtimehours[i]=0;
     }//FOR
}//FINDOVERTIMEHOURS

void findovertimepay(int overtimehours[], float hourlyrate[],
     float overtimepay[], int n){
     for(int i=0; i<n; i++){
             overtimepay[i]=overtimehours[i]*hourlyrate[i]*1.5;
     }//FOR
}//FINDOVERTIMEPAY

void findregularhours(int hoursworked[], int regularhours[], int n){
     for(int i=0; i<n; i++){
             if(hoursworked[i]>40) regularhours[i]=40;
             else regularhours[i]=hoursworked[i];
     }//FOR
}//FINDREGULARHOURS

void findregularpay(int regularhours[], float regularpay[],
                        float hourlyrate[], int n){
      for(int i=0; i<n; i++){
      regularpay[i] = regularhours[i]*hourlyrate[i];
      }//FOR
}//FINDREGULARPAY
void findgrosspay(float regularpay[], float overtimepay[],
                        float grosspay[], int n){
                        for(int i=0; i<n; i++){
                        grosspay[i]=regularpay[i]+overtimepay[i];
        }//FOR
}//FINDGROSSPAY

void findtaxrate(float grosspay[], float taxrate[], int n){
     for(int i=0; i<n; i++){
             if(grosspay[i]>4000.00) taxrate[i]=0.40;
             else if(grosspay[i]>3000.00) taxrate[i]=0.30;
             else if(grosspay[i]>1000.00) taxrate[i]=0.20;
             else taxrate[i]=0.10;
     }//FOR
}//FINDTAXRATE

void findtaxamount(float grosspay[], float taxamount[],
                         float taxrate[], int n){
for(int i=0; i<n; i++){
        taxamount[i]=grosspay[i]*taxrate[i];
        }//FOR
}//FINDTAXAMOUNT

void findnetpay(float grosspay[], float netpay[], float taxamount[], int n){
     for(int i=0; i<n; i++){
          netpay[i]=grosspay[i]-taxamount[i];
     }//FOR
}//FINDNETPAY

void printalldata(long int id[], int hoursworked[], float hourlyrate[],
                       float overtimepay[], float grosspay[], float taxamount[],
                       float netpay[], int n){
                       float totalNetPay=0;
                       
                       int i=0;
                       cout<<"EMP ID"<<"\t"<<"HOURS"<<"\t"<<"RATE"<<"\t"
           <<"OVERPAY"<<"\t"<<"   GROSSPAY"<<"\t"<<"      TAX"<<"\t"
           <<"NETPAY"<<endl;

for(i; i<n; i++){
       totalNetPay +=netpay[i];
        cout<<""<<id[i]<<"\t"<<hoursworked[i]<<"\t"<<hourlyrate[i]
        <<"\t"<<overtimepay[i]<<"\t\t"<<grosspay[i]<<"\t\t"
        <<taxamount[i]<<"\t"<<netpay[i]<<endl;
        }//FOR
        cout<<"The net pay average of the employees are: "<<totalNetPay/i<<endl;
        system("PAUSE");
}//PRINTALLDATA
//end source code   


SAMPLE SORT PROGRAM:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream.h>
main(){
       int table[]={5, 1, 3, 99, 8, 12, 7, 8, 14};
       int i, j;
       int n=9;
       for(i=0; i<n-1; i++){
                for(j=n-1; j>i; j--){
                           if(table[j]<table[j-1]){
                           int hold=table[j];
                           table[j]=table[j-1];
                           table[j-1]=hold;
                           }// end if
                           }// end j
                           }// end i
       cout<<"SORTED DATA"<<endl;
       for(i=0; i<n; i++){
                cout<<table[i]<<" ";
                }//FOR
                system("PAUSE");
                return 0;
                }//MAIN 


My questions are:
1.) I'm creating a new function to sort my employees netpay right?
2.) Do I need to add to the main program as well other than declaring a sort netpay variable?
3.) Any other tips and hints would be appreciated
Last edited on
2) Looks like you "should" be able to copy/paste this right into your main program:
1
2
3
4
5
6
7
8
9
10
11
12
13
void sort(int table[], int n)
{
  int i, j;
  for(i=0; i<n-1; i++){
    for(j=n-1; j>i; j--){
      if(table[j]<table[j-1]){
        int hold=table[j];
        table[j]=table[j-1];
        table[j-1]=hold;
      }// end if
    }// end j
  }// end i
}


Maybe change "table" to "arr" and "n" to "size" (or whatever, something generic but descriptive. Admittedly, "arr" is a pretty poor name).

3) Maybe think about making an "Accountant" class:
http://www.cplusplus.com/doc/tutorial/classes/

I think the function will be easier to write because you wont need to pass the arrays every time. At that point you could learn about multi-file projects, so the accountant class could be in a different file(s) than main(). Then if you need it for a project, just #include "accountant.h" and you'll be good to go.
Last edited on
Will this:

1
2
3
4
5
6
7
8
9
10
11
12
13
void sort(int table[], int n)
{
  int i, j;
  for(i=0; i<n-1; i++){
    for(j=n-1; j>i; j--){
      if(table[j]<table[j-1]){
        int hold=table[j];
        table[j]=table[j-1];
        table[j-1]=hold;
      }// end if
    }// end j
  }// end i
}


Read the information from my payroll program? I want to :
Sort the net pays. Also I want to display only the net pays before sorting and after sorting.
I can't speak for that specific sorting algorithm. If you've tested it and it works, then it would be able to sort any array that you would give it.

But I thought all of your arrays were parallel, this is really going to mess with that fact.
I think I found the right path I'll get back to you guys
Here's what I have but the sort doesn't show up when it executes
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
#include <iostream>
#include<fstream>//file input output stream
using namespace std; 

//function prototypes
int readalldata(long int[], int[], float[], const int);
void findovertimehours(int[], int[], int);
void findovertimepay(int[], float[], float[], int);
void findregularhours(int[], int[], int);
void findregularpay(int[], float[], float[], int);
void findgrosspay(float[], float[], float[], int);
void findtaxrate(float[], float[], int);
void findtaxamount(float[], float[], float[], int);
void findnetpay(float[], float[], float[], int);
void printalldata(long int[], int[], float[], float[], float[], float[], float[], int);
void sort(float[], int);

int main(){
     const int MAXSIZE=100;   //for maximum of 100 employees
     
     //decleration of variables
     int n;
     long int id[MAXSIZE];
     int hoursworked[MAXSIZE], overtimehours[MAXSIZE];
     int regularhours[MAXSIZE];
     float hourlyrate[MAXSIZE], regularpay[MAXSIZE],
     overtimepay[MAXSIZE], grosspay[MAXSIZE];
     float taxrate[MAXSIZE], taxamount[MAXSIZE], netpay[MAXSIZE];
     
     //function calls
     n=readalldata(id, hoursworked, hourlyrate, MAXSIZE); //get all data
     findovertimehours(hoursworked, overtimehours, n);
     findovertimepay(overtimehours, hourlyrate, overtimepay, n);
     findregularhours(hoursworked, regularhours, n);
     findregularpay(regularhours, regularpay, hourlyrate, n);
     findgrosspay(regularpay, overtimepay, grosspay, n);
     findtaxrate(grosspay, taxrate, n);
     findtaxamount(grosspay, taxamount, taxrate, n);
     findnetpay(grosspay, netpay, taxamount, n);
     printalldata(id, hoursworked, hourlyrate, overtimepay, 
                      grosspay, taxamount, netpay, n);
                      sort(netpay, n);
                      return 0;
}//MAIN
//function definitions
int readalldata(long int id[], int hoursworked[], float hourlyrate[], int n){
    ifstream fin("employee.txt");
    n=0;
    
    while(fin>>id[n]>>hoursworked[n]>>hourlyrate[n]) n++;
    fin.close();
    return n;
}//READALLDATA

void findovertimehours(int hoursworked[], int overtimehours[], int n){
     for(int i=0; i<n; i++){
             if(hoursworked[i]>40) overtimehours[i]=hoursworked[i]-40;
             else overtimehours[i]=0;
     }//FOR
}//FINDOVERTIMEHOURS

void findovertimepay(int overtimehours[], float hourlyrate[],
     float overtimepay[], int n){
     for(int i=0; i<n; i++){
             overtimepay[i]=overtimehours[i]*hourlyrate[i]*1.5;
     }//FOR
}//FINDOVERTIMEPAY

void findregularhours(int hoursworked[], int regularhours[], int n){
     for(int i=0; i<n; i++){
             if(hoursworked[i]>40) regularhours[i]=40;
             else regularhours[i]=hoursworked[i];
     }//FOR
}//FINDREGULARHOURS

void findregularpay(int regularhours[], float regularpay[],
                        float hourlyrate[], int n){
      for(int i=0; i<n; i++){
      regularpay[i] = regularhours[i]*hourlyrate[i];
      }//FOR
}//FINDREGULARPAY
void findgrosspay(float regularpay[], float overtimepay[],
                        float grosspay[], int n){
                        for(int i=0; i<n; i++){
                        grosspay[i]=regularpay[i]+overtimepay[i];
        }//FOR
}//FINDGROSSPAY

void findtaxrate(float grosspay[], float taxrate[], int n){
     for(int i=0; i<n; i++){
             if(grosspay[i]>4000.00) taxrate[i]=0.40;
             else if(grosspay[i]>3000.00) taxrate[i]=0.30;
             else if(grosspay[i]>1000.00) taxrate[i]=0.20;
             else taxrate[i]=0.10;
     }//FOR
}//FINDTAXRATE

void findtaxamount(float grosspay[], float taxamount[],
                         float taxrate[], int n){
for(int i=0; i<n; i++){
        taxamount[i]=grosspay[i]*taxrate[i];
        }//FOR
}//FINDTAXAMOUNT

void findnetpay(float grosspay[], float netpay[], float taxamount[], int n){
     for(int i=0; i<n; i++){
          netpay[i]=grosspay[i]-taxamount[i];
     }//FOR
}//FINDNETPAY

void printalldata(long int id[], int hoursworked[], float hourlyrate[],
                       float overtimepay[], float grosspay[], float taxamount[],
                       float netpay[], int n){
                       float totalNetPay=0;
                       
                       int i=0;
                       cout<<"EMP ID"<<"\t"<<"HOURS"<<"\t"<<"RATE"<<"\t"
           <<"OVERPAY"<<"\t"<<"   GROSSPAY"<<"\t"<<"      TAX"<<"\t"
           <<"NETPAY"<<endl;
           
for(i; i<n; i++){
       totalNetPay +=netpay[i];
        cout<<""<<id[i]<<"\t"<<hoursworked[i]<<"\t"<<hourlyrate[i]
        <<"\t"<<overtimepay[i]<<"\t\t"<<grosspay[i]<<"\t\t"
        <<taxamount[i]<<"\t"<<netpay[i]<<endl;
        }//FOR
        cout<<"The net pay average of the employees are: "<<totalNetPay/i<<endl;
        system("PAUSE");
}//PRINTALLDATA

void sort(float table[], int n)
{
  int i, j;
  
for(i=0; i<n-1; i++){
    for(j=n-1; j>i; j--){
      if(table[j]<table[j-1]){
        float hold=table[j];
        table[j]=table[j-1];
        table[j-1]=hold;
      }// end if
    }// end j
  }// end i
}
//end source code   


The program executes but the sort doesn't show up. Where would I put the output for this?
Last edited on
Figured it all out guys here's the finished product:

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
#include <iostream>
#include<fstream>//file input output stream
using namespace std; 

//function prototypes
int readalldata(long int[], int[], float[], const int);
void findovertimehours(int[], int[], int);
void findovertimepay(int[], float[], float[], int);
void findregularhours(int[], int[], int);
void findregularpay(int[], float[], float[], int);
void findgrosspay(float[], float[], float[], int);
void findtaxrate(float[], float[], int);
void findtaxamount(float[], float[], float[], int);
void findnetpay(float[], float[], float[], int);
void printalldata(long int[], int[], float[], float[], float[], float[], float[], int);
void sort(float[], int);

int main(){
     const int MAXSIZE=100;   //for maximum of 100 employees
     
     //decleration of variables
     int n;
     long int id[MAXSIZE];
     int hoursworked[MAXSIZE], overtimehours[MAXSIZE];
     int regularhours[MAXSIZE];
     float hourlyrate[MAXSIZE], regularpay[MAXSIZE],
     overtimepay[MAXSIZE], grosspay[MAXSIZE];
     float taxrate[MAXSIZE], taxamount[MAXSIZE], netpay[MAXSIZE];
     
     //function calls
     n=readalldata(id, hoursworked, hourlyrate, MAXSIZE); //get all data
     findovertimehours(hoursworked, overtimehours, n);
     findovertimepay(overtimehours, hourlyrate, overtimepay, n);
     findregularhours(hoursworked, regularhours, n);
     findregularpay(regularhours, regularpay, hourlyrate, n);
     findgrosspay(regularpay, overtimepay, grosspay, n);
     findtaxrate(grosspay, taxrate, n);
     findtaxamount(grosspay, taxamount, taxrate, n);
     findnetpay(grosspay, netpay, taxamount, n);
     printalldata(id, hoursworked, hourlyrate, overtimepay, 
                      grosspay, taxamount, netpay, n);
                      sort(netpay, n);
                      printalldata(id, hoursworked, hourlyrate, overtimepay, 
                      grosspay, taxamount, netpay, n);
                      return 0;
}//MAIN
//function definitions
int readalldata(long int id[], int hoursworked[], float hourlyrate[], int n){
    ifstream fin("employee.txt");
    n=0;
    
    while(fin>>id[n]>>hoursworked[n]>>hourlyrate[n]) n++;
    fin.close();
    return n;
}//READALLDATA

void findovertimehours(int hoursworked[], int overtimehours[], int n){
     for(int i=0; i<n; i++){
             if(hoursworked[i]>40) overtimehours[i]=hoursworked[i]-40;
             else overtimehours[i]=0;
     }//FOR
}//FINDOVERTIMEHOURS

void findovertimepay(int overtimehours[], float hourlyrate[],
     float overtimepay[], int n){
     for(int i=0; i<n; i++){
             overtimepay[i]=overtimehours[i]*hourlyrate[i]*1.5;
     }//FOR
}//FINDOVERTIMEPAY

void findregularhours(int hoursworked[], int regularhours[], int n){
     for(int i=0; i<n; i++){
             if(hoursworked[i]>40) regularhours[i]=40;
             else regularhours[i]=hoursworked[i];
     }//FOR
}//FINDREGULARHOURS

void findregularpay(int regularhours[], float regularpay[],
                        float hourlyrate[], int n){
      for(int i=0; i<n; i++){
      regularpay[i] = regularhours[i]*hourlyrate[i];
      }//FOR
}//FINDREGULARPAY
void findgrosspay(float regularpay[], float overtimepay[],
                        float grosspay[], int n){
                        for(int i=0; i<n; i++){
                        grosspay[i]=regularpay[i]+overtimepay[i];
        }//FOR
}//FINDGROSSPAY

void findtaxrate(float grosspay[], float taxrate[], int n){
     for(int i=0; i<n; i++){
             if(grosspay[i]>4000.00) taxrate[i]=0.40;
             else if(grosspay[i]>3000.00) taxrate[i]=0.30;
             else if(grosspay[i]>1000.00) taxrate[i]=0.20;
             else taxrate[i]=0.10;
     }//FOR
}//FINDTAXRATE

void findtaxamount(float grosspay[], float taxamount[],
                         float taxrate[], int n){
for(int i=0; i<n; i++){
        taxamount[i]=grosspay[i]*taxrate[i];
        }//FOR
}//FINDTAXAMOUNT

void findnetpay(float grosspay[], float netpay[], float taxamount[], int n){
     for(int i=0; i<n; i++){
          netpay[i]=grosspay[i]-taxamount[i];
     }//FOR
}//FINDNETPAY

void printalldata(long int id[], int hoursworked[], float hourlyrate[],
                       float overtimepay[], float grosspay[], float taxamount[],
                       float netpay[], int n){
                       float totalNetPay=0;
                       
                       int i=0;
                       cout<<"EMP ID"<<"\t"<<"HOURS"<<"\t"<<"RATE"<<"\t"
           <<"OVERPAY"<<"\t"<<"   GROSSPAY"<<"\t"<<"        TAX"<<"\t"
           <<"NETPAY"<<endl;
           
for(i; i<n; i++){
       totalNetPay +=netpay[i];
        cout<<""<<id[i]<<"\t"<<hoursworked[i]<<"\t"<<hourlyrate[i]
        <<"\t"<<overtimepay[i]<<"\t\t"<<grosspay[i]<<"\t\t"
        <<taxamount[i]<<"\t"<<netpay[i]<<endl;
        }//FOR
        cout<<"The net pay average of the employees are: "<<totalNetPay/i<<endl;
        system("PAUSE");
}//PRINTALLDATA

void sort(float table[], int n)
{
  int i, j;
  
for(i=0; i<n-1; i++){
    for(j=n-1; j>i; j--){
      if(table[j]<table[j-1]){
        float hold=table[j];
        table[j]=table[j-1];
        table[j-1]=hold;
      }// end if
    }// end j
  }// end i
}
//end source code   

Topic archived. No new replies allowed.