Sorting Netpay Using Array of Pointers?

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 and then sort them with a regular sort! Now I need to sort the netpay with an array of pointers without changing the original data in the original array. I made a sample program that utilizes this technique 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
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 


My questions are:

1.)How would I incorporate this into my program? I begin to get confused when I begin adding this idea into my functions of the program

2.) Am I replacing my original sort I made with pointers?

My buddy gave me some tips and said to possibly incorporate this into my program. Can anybody get me started and lead me in the right direction on where to begin? I'm just learning about pointers and it's hard for me to grasp the concept right now

int *np,tmp;
for (i=0,i<n; i++) np=&netpay[i]; //associating the netpay to np;
temp=np[i];//storing the pointer to a temp

I'm just learning about pointers and it's hard for me to grasp the concept right now
Last edited on
You really need to learn about using structs.

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

struct payrecord
{  long id;
   int hoursworked;
   int overtimehours;
   int regularhours;
   float hourlyrate;
   float regularpay;
   float overtimepay; 
   float grosspay;
   float taxrate; 
   float taxamount; 
   float netpay;
};
const int MAXSIZE=100;   
struct payrecord payrec[MAXSIZE];   // Declare the array of structs

void findovertimehours (struct payrecord * pr);
void findovertimepay (struct payrecord * pr);

void Calculate (struct payrecord * pr)  // Point to one pay record
{   findovertimehours (pr);
    findovertimepay (pr);
	// etc
}

int readalldata ()
{	int n = 0;
    ifstream fin("employee.txt");
    // create a pointer to the first pay record
    struct payrecord * pr = &payrec[n];
    
    while(fin>>pr->id>>pr->hoursworked>>pr->hourlyrate)
    {   Calculate (pr);
        pr++;   // Point to next pay record
        n++;    // Increment the count
    }
    fin.close();
    return n;
}//READALLDATA   


I don't see where you call your original sort routine, so I don't know what you're sorting on. However, it's going to reorder one of the arrays of floats and leave every other array intact, which will leave you with scrambled data. I think that is why your friend suggested using pointers.

By using structures as I've suggested, you can reorder the entire array of pay records, so the associated data stays together.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void swap (int i, int j)  // swap two pay records
{  struct payrecord temp;
    temp = payrec[i];
    payrec[i] = payrec[j];
    payrec[j] = temp;
}

//  Assuming you want to sort on gross pay
void sort(float table[], int n)
{ int i, j;
  
  for (i=0; i<n-1; i++)
 { for (j=n-1; j>i; j--)
   { if (payrec[j].grosspay < payrec[j-1].grosspay)
     {  swap (j,j-1);
      }// end if
    }// end j
  }// end i
} 


From here it's a small jump to using classes and vectors.




A completely unnecessary use of pointers, the array is already a pointer.

Anyway, the easiest way to sort a bunch of parallel arrays, or arrays that you want to keep untouched, is to create an index array:
1
2
int numbers[5] = {3,4,2,4,1};
int index[5] = {0,1,2,3,4};


When you do the sort, don't swap the values in numbers[5], swap the values in index[5]. After sorting, index[5] = {4,2,0,1,3}.

Thus, you can do this:
1
2
3
4
5
6
for (int i = 0; i < 5; i++)
{
  cout << numbers[index[i]] << endl;
  // You could put any array and it would be in the proper order
  // ex. cout << words[index[i]] << endl;
}


The sort function should reset the index array to {0,1,2,3,...} before trying to swap values.
Last edited on
Thanks for the feedback guys. I'm going to attempt this tonight when I get home from work
Here's my "employee.txt" as well for everybody to see if they are curious:

8572 42 12.00
6423 40 15.00
7465 45 10.00
2477 40 16.00
5996 44 18.00
I made some changes but I don't know where to add:

1
2
void findovertimehours (struct payrecord * pr);
void findovertimepay (struct payrecord * pr);

and
1
2
3
4
void Calculate (struct payrecord * pr)  // Point to one pay record
{   findovertimehours (pr);
    findovertimepay (pr);
	// etc 


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

struct payrecord
{  long id;
   int hoursworked;
   int overtimehours;
   int regularhours;
   float hourlyrate;
   float regularpay;
   float overtimepay; 
   float grosspay;
   float taxrate; 
   float taxamount; 
   float netpay;
};

int main(){
     const int MAXSIZE=100;   //for maximum of 100 employees
     struct payrecord payrec[MAXSIZE];//Declare the array of structs
     
void findovertimehours (struct payrecord * pr);
void findovertimepay (struct payrecord * pr);
     
     //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){
    int n=0;
    ifstream fin("employee.txt");
    
    while(fin>>pr->id>>hoursworked>>pr->hourlyrate)
    {    Calculate (pr);
         pr++; //Point to next pay record
         n++;//Increment the count
    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 swap (int i, int j)  // swap two pay records
{  struct payrecord temp;
    temp = payrec[i];
    payrec[i] = payrec[j];
    payrec[j] = temp;
}


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   
1
2
void findovertimehours (struct payrecord * pr);
void findovertimepay (struct payrecord * pr);

should go before main.

Calculate() can either go before main(), no forward delcaration needed, or it can go after main, in which case you will need a forward delaration for it.

The findovertimehours and findovertimepay I provided were meant as samples to replace you existing findovertimehours and findovertimepay functions. I also intended for you to extend the idea I provided to your other calculation routines.

My sort function was also meant to replace yours. I wasn't sure what field you wanted to sort on, so I arbitrarily chose grosspay.

The following declarations go away because the are replaced by the struct array.
1
2
3
4
5
6
    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];

You will need to change each of your variable[n] references, to refer to either payrec[n].variable, or use a pointer to a specific payrec instance as I did in the readalldata function.
For example, findovertimehours becomes:
1
2
3
4
5
6
void findovertimehours (struct payrecord * pr)
{   if(pr->hoursworked > 40) 
       pr->overtimehours = pr->hoursworked-40;
     else 
       pr->overtimehours=0;
}//FINDOVERTIMEHOURS 

Note that this eliminates the for loop, since it is now being called to process one record at a time from the read loop via the call to Calculate().





Last edited on
This is what I'm trying to do to learn example of pointers. This is why I'm suppose to do it this way, but I'm lost......Any helpers?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int MAXSIZE = 50;
int NetPay[MAXSIZE];
// We are skipping ahead to the NetPay array already filled with Values. 

int* ArrayPointer = &NetPay; //This is how you would create your pointer to your array. 

int SortedNetPay = Pointer_Array_Sort( ArrayPointer );

int Pointer_Array_Sort( int* Values )
{
// Do your sorting in here

}

Don't forget to Prototype your function. 



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
#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);
void pointer_array_sort(int*values);

int main(){
     const int MAXSIZE=50
     int netpay[MAXSIZE];   //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
int*arraypointer=&netpay;
int sortednetpay=pointer_array_sort(arraypointer);
void pointer_array_sort(int*Values)
{
  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   


Errors:

1.)C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp In function `int main()':
2.)21 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp expected `,' or `;' before "int"
3.)21 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp At global scope:
4.)134 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp `netpay' was not declared in this scope
5.)135 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp void value not ignored as it ought to be
6.) C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp In function `void pointer_array_sort(int*)':
7.)140 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp `n' undeclared (first use this function)
8.) (Each undeclared identifier is reported only once for each function it appears in.)
9.)142 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp `table' undeclared (first use this function)
Last edited on
2) You're missing the ; on line 20.
4) netpay is inside main. arraypointer is global scope. You can't take the address of an array inside main.
5) pointerarraysory is prototyped at line 17 as a void function. You can't assign the result of a void function to an int.
7) n is declared inside main. It can't be seen from inside pointerarraysort.
9) Where is table?

2.)Ok I fixed line 20
4.) Do I just delte int netpay[MAXSIZE]; inside the main?
5.) Line 17 do I just delete the void?
7.) Do I just delete the n in main or move it outside of the main?
9.) The table is this right:
Last edited on
4) That depends. What are you trying to do with it? The only use I see of it is at line 134 which initializing a global pointer (arraypointer). The use of arraypointer is also problematic. Since it is at global scope, pointerarraysort will get called at program initialization. Not what I think you want.

5) pointerarraysort (line 136) does not return anything, so the void is correct.

7) You should pass n as an argument.

9) I'm confused. In your previous post, you stated you had to do the problem a certain way, which is why I didn't make any further reference to the stuct based solution in my previous post. I don't see any indication of use of the struct in your code, yet you're asking about the struct based sort and swap routines.

We can make the sort use an index array with the array of structs, if that would meet the requirements of your assignment. Has your instructor introduced the use of structs yet? I may be suggesting something you haven't gotten to yet.
Well as I said I actually am finished totally with this payroll program once I

Sort the netpay by the pointers
Sort the net pays (salary) using an array of pointers (do not change the data in the original array). For now, display only the net pays before sorting Sort the net pays (salary) using an array of pointers (do not change the data in the original array). For now, display only the net pays before sorting and after sorting.


I have been working on this program for a month now and am finally on the last part. I just need to do what the bold print asks and I am finally done! Haha. So I don't believe I will be utilizing struct. I think I should eliminate the example program I made my bad!
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
#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);
void pointer_array_sort(int*values);

int main(){
     const int MAXSIZE=50;  //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);
                      pointer_array_sort(netpay);
                      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 pointer_array_sort(int*Values)
{
  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   


Where would I place this sample program I made?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream.h>
int main(){
     const int n=5;
     int item[n]={2,5,3,1,8};
     int *p[n];
int i,j;
     int*temp;
     int sortedflag=0;
     for(i=0;i<n;i++) p[i]=item+i;//INITIALIZING POINTER ARRAY
     for(i=0;i<n;i++)cout<<*p[i]<<" ";
     while(!sortedflag){
                        sortedflag=1;
                        for(j=0;j<n-1;j++){
                                           if(*p[j]>*p[j+1]){
                                                             temp=p[j];
                                                             p[j]=p[j+1];
                                                             p[j+1]=temp;
                                                             sortedflag=0;      }//SWAP
                                                             }//J
                                                        }//I
     cout<<endl<<"SORTED ARRAY: ";
     for(i=0;i<n;i++)cout<<*p[i]<<" ";
     system("PAUSE");
     }//MAIN 


And than changes necessary to make it work

Topic archived. No new replies allowed.