Array of pointers - sort

Greetings,

This is a three part assignment where we have to modify our code to sort netpay. The code below does sort the netpay properly. However, part three of the assignment requires that I now sort the netpay with an array of pointers.

Me and my brother-in-law are looking at the textbook and a myriad of examples on the internet. However, we cannot determine how to approach this. Any help will be greatly appreciated!

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 <iomanip>
#include <fstream>

using namespace std;

    int id[100], i=0, n=0; //hours worked
    double hw[100], rp[100], gp[100], otp[100], TR[100], ta[100], np[100]; //regular pay, gross, overtime pay, taxes, tax rate, net pay
    double rh[100], oth[100], hr[100], otr[100]; //regular hours, overtime hours, hourly rate, overtime rate
    char fn[100][14], ln[100][15], st[100]; //first name/last name/status-married, single, head of household
    double tnp=0; // total net pay
    double rc=0; // record counter
    double avg=0; // average

	//functions
	void over(void){ //Overtime hours function
        for (i=0; i<n;i++)
        {
            if(hw[i] > 40)
            {
                oth[i] = hw[i] - 40;
                otr[i] = hr[i] * 1.5;
            }
            else
            {
                oth[i]=0, otr[i]=0;
            }
        }
    }// END Overtime hours function

    void pay(void){ // Pay function
        for (i=0; i<n;i++)
            {
            rp[i] = 40 * hr[i];
            if( oth[i] > 0)
            otp[i] = (hw[i] - 40) * hr[i] * 1.5;
            gp[i] = rp[i] + otp[i];
            } //  END Pay function
        }

    void tax(void){
        for (i=0; i<n;i++) // Tax function
            {
            if( gp[i] > 1000 ) TR[i] = 0.30;
            else if( gp[i] > 800) TR[i] = 0.20;
            else if( gp[i] > 500) TR[i] = 0.10;
            else TR[i] = 0.0;

            if (st[i]=='S') TR[i] = (TR[i] + .05);
            else if (st[i]=='M') TR[i] = (TR[i] * 1);
            else if (st[i]=='H' && gp[i] > 500) TR[i] = (TR[i] - .05);

            } // END Tax function
        }

    void net(void){
       for (i=0; i<n;i++) //Net pay function
            {
            ta[i] = gp[i] * TR[i];
            np[i] = gp[i] * (1.0- TR[i]);
            tnp+=np[i];
            rc++;
            avg=tnp/rc;
            } // END Net pay function
        }

    void swap(int x, int y)
    {
        int temp;
        temp=id[x];
        id[x]=id[y];
        id[y]=temp;

        int temp2;
        temp2=hw[x];
        hw[x]=hw[y];
        hw[y]=temp2;

        int temp3;
        temp3=hr[x];
        hr[x]=hr[y];
        hr[y]=temp3;

        int temp4;
        temp4=otp[x];
        otp[x]=otp[y];
        otp[y]=temp4;

        int temp5;
        temp5=gp[x];
        gp[x]=gp[y];
        gp[y]=temp5;

        int temp6;
        temp6=ta[x];
        ta[x]=ta[y];
        ta[y]=temp6;

        int temp7;
        temp7=np[x];
        np[x]=np[y];
        np[y]=temp7;

    }

    void npSort (void){ //np sort function
        bool flag=true;
        int i;
        while(flag==true)
        {
            flag=false;
            for(i=1;i<rc;i++)
            {
               if(np[i]<np[i-1])
               {
                   swap(i, i-1);
                   flag=true;
               }
            }
         }
    }

int main(){
    int over();
    int pay();
    int tax();
    int net();
    ifstream fin ("employee.in");

while(fin>>id[n]>>hw[n]>>hr[n])n++;

    over();
    pay();
    tax();
    net();

    cout<<endl<<endl<<"DR. EBRAHIMI'S PAYROLL INSTITUTE"<<endl<<endl;

    cout<<"SSN #"<<"\t"<<"Hours"<<"\t"<<"Rate"<<"\t"<<"OverTime"<<"\t"<<"GrossPay"<<"\t"<<
        "TaxRate"<<"\t"<<"NetPay"<<endl<<endl;

        for (i=0; i<n;i++){

            cout<<""<<id[i]<<"\t"<<hw[i]<<"\t"<<hr[i]<<"\t"<<otp[i]<<"\t\t"<<gp[i]<<"\t\t"
            <<ta[i]<<"\t"<<np[i]<<endl<<endl;
            }

    cout<<"Employee Salary Total"<<endl;
           cout<<tnp<<endl<<endl;

    cout<<"Employee Salary Average"<<endl;
           cout<<setprecision(5)<<avg<<endl<<endl;

    npSort();
    cout<<"Sorted Net Pay"<<endl;
        for (i=0; i<n;i++){
                cout<<""<<np[i]<<endl<<endl;}



return 0;
}//MAIN
To sort things from pointers, the comparison function must dereference the pointers.

if (*(foo[i]) < *(foo[i-1]))

By the way, you are doing this all a very hard way.

You should have a struct holding all the information of a specific kind.

1
2
3
4
5
6
7
8
9
struct employee
{
  char   first_name[15];
  char   last_name[15];
  char   status;
  int    hours_worked;
  double regular_pay, gross_pay, overtime_pay, taxes, ...;
  etc.
};

Now you can have an array of employees:

 
employee employees[100];

To sort the array, you need a function to compare two employees. You can create as many functions as you like, depending on how you wish to sort them.

1
2
3
4
bool compare_hours_worked( const employee& a, const employee& b )
{
  return a.hours_worked < b.hours_worked;
}

You can now sort using the standard library (which works much better than that Bubble sort you are using):

1
2
  //sort employees by hours worked, least to most:
  std::sort( std::begin(employees), std::end(employees), compare_hours_worked );

Hope this helps.
Last edited on
Thank you for your quick reply. Unfortunately, structs and classes begin in the next module of the class and I think my professor wants us to do it the hard way before learning easier tools. For example, in the next module I will have to create classes for both hourly wage and salaried employees.
Sounds good.

So again, to sort, you'll need a comparison function that compares the dereferenced values of your pointers, instead of comparing the pointer's values directly.
I am confused as to how to implement this concept in my original code to sort the netpay with an array of pointers.
to sort the netpay with an array of pointers

What does that mean?

The "regular" case has an array of real objects and you swap those real values, until they are in some order.

"Array of pointers" sounds like having two arrays. One, that has the real values, and another that has pointers to those real values. The "sort" does not touch the array of real values, but swaps the pointers until they are in some order.

For example, the real array has { 7, 42, 3 }. The "pointers" are { A, B, C }. If we dereference the pointers, we get 7, 42 and 3. If we rearrange the pointer array to { B, A, C }, then dereferencing the array produces 42, 7 and 3.

When is such indirection useful? When the real values are expensive to swap. Furtheremore, you may want to keep the real data unchanged and merely create temporary "views" with pointer arrays that you sort in arbritrary ways.


You do have slightly "expensive" values. One value has components in multiple arrays. However, you cannot have one pointer to point to more than one address, so without the struct proposed by Duoas I'm unsure what your array of pointers would actually do.


You could have an array of indices. Integers that are used to dereference the "real values". That is still indirection. Kind of pointers, even though not according to language's syntax.
I began working on the next module in lieu of this one while I seek answers to the pointer issue. The instructions from my professor are:

Program 3 - 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 emailed him yesterday but have yet to receive a response.
For now, display only the net pays before sorting and after sorting.

"Only", so have an array of pointers to the elements in the "net pays" array and ignore all the other data.
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
#include <algorithm>
#include <iostream>
using namespace std;

bool cmp( int* a, int* b )
{
  return *a < *b;
}

int main()
{
  int  xs[7] = { 5, 3, 4, 1, 7, 2, 6 };
  int* ps[7];
  
  for (int n = 0; n < 7; n++)
    ps[n] = &xs[n];
    
  cout << "ps[] before sorting:\n";
  for (int* p : ps)
    cout << "  " << p << " (points to " << *p << ")\n";
    
  sort( begin(ps), end(ps), cmp );
  
  cout << "\nps[] after sorting:\n";
  for (int* p : ps)
    cout << "  " << p << " (points to " << *p << ")\n";
  
  cout << "\noriginal, unsorted xs[]:\n";
  for (int& x : xs)
    cout << "  " << x << " (at address " << &x << ")\n";
}

Good luck!
Thank you for your help. I am still confused by the whole concept and am having a hard time with these examples and with my text book as to how exactly to implement the code.
Line 7 is the key.

Instead of

return a < b

you must follow the pointer to get the data you want to compare.

int* --> return *a < *b;

employee* --> return a->hours_worked < b->hours_worked;

etc.
I was working on the next two modules as I am still confused by all of this. I removed any code which referred to sorting the net pay. I am studying these examples, though I am unsure as to how they are implemented to sort the net pay and where it fits in with my above code. I am studying the examples here and in my text book and got this, but I get errors at the end:

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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

    int id[100], i=0, n=0; //hours worked
    double hw[100], rp[100], gp[100], otp[100], TR[100], ta[100];  //regular pay, gross, overtime pay, taxes, tax rate
    int np[100]; // net pay
    double rh[100], oth[100], hr[100], otr[100]; //regular hours, overtime hours, hourly rate, overtime rate
    char fn[100][14], ln[100][15], st[100]; //first name/last name/status-married, single, head of household
    double tnp=0; // total net pay
    double rc=0; // record counter
    double avg=0; // average


	//functions
	void over(void){ //Overtime hours function
        for (i=0; i<n;i++)
        {
            if(hw[i] > 40)
            {
                oth[i] = hw[i] - 40;
                otr[i] = hr[i] * 1.5;
            }
            else
            {
                oth[i]=0, otr[i]=0;
            }
        }
    }// END Overtime hours function

    void pay(void){ // Pay function
        for (i=0; i<n;i++)
            {
            rp[i] = 40 * hr[i];
            if( oth[i] > 0)
            otp[i] = (hw[i] - 40) * hr[i] * 1.5;
            gp[i] = rp[i] + otp[i];
            } //  END Pay function
        }

    void tax(void){
        for (i=0; i<n;i++) // Tax function
            {
            if( gp[i] > 1000 ) TR[i] = 0.30;
            else if( gp[i] > 800) TR[i] = 0.20;
            else if( gp[i] > 500) TR[i] = 0.10;
            else TR[i] = 0.0;

            if (st[i]=='S') TR[i] = (TR[i] + .05);
            else if (st[i]=='M') TR[i] = (TR[i] * 1);
            else if (st[i]=='H' && gp[i] > 500) TR[i] = (TR[i] - .05);

            } // END Tax function
        }

    void net(void){
       for (i=0; i<n;i++) //Net pay function
            {
            ta[i] = gp[i] * TR[i];
            np[i] = gp[i] * (1.0- TR[i]);
            tnp+=np[i];
            rc++;
            avg=tnp/rc;
            } // END Net pay function
        }

    void swap(int x, int y)
    {
        int temp;
        temp=id[x];
        id[x]=id[y];
        id[y]=temp;

        int temp2;
        temp2=hw[x];
        hw[x]=hw[y];
        hw[y]=temp2;

        int temp3;
        temp3=hr[x];
        hr[x]=hr[y];
        hr[y]=temp3;

        int temp4;
        temp4=otp[x];
        otp[x]=otp[y];
        otp[y]=temp4;

        int temp5;
        temp5=gp[x];
        gp[x]=gp[y];
        gp[y]=temp5;

        int temp6;
        temp6=ta[x];
        ta[x]=ta[y];
        ta[y]=temp6;

        int temp7;
        temp7=np[x];
        np[x]=np[y];
        np[y]=temp7;

    }

   int main(){
    int *p[n];
    int over();
    int pay();
    int tax();
    int net();
    ifstream fin ("3employee.in");

while(fin>>id[n]>>hw[n]>>hr[n])n++;

    over();
    pay();
    tax();
    net();

    cout<<endl<<endl<<"DR. EBRAHIMI'S PAYROLL INSTITUTE"<<endl<<endl;

    cout<<"SSN #"<<"\t"<<"Hours"<<"\t"<<"Rate"<<"\t"<<"OverTime"<<"\t"<<"GrossPay"<<"\t"<<
        "TaxRate"<<"\t"<<"NetPay"<<endl<<endl;

        for (i=0; i<n;i++){

            cout<<""<<id[i]<<"\t"<<hw[i]<<"\t"<<hr[i]<<"\t"<<otp[i]<<"\t\t"<<gp[i]<<"\t\t"
            <<ta[i]<<"\t"<<np[i]<<endl<<endl;
            }

    cout<<"Employee Salary Total"<<endl;
           cout<<tnp<<endl<<endl;

    cout<<"Employee Salary Average"<<endl;
           cout<<setprecision(5)<<avg<<endl<<endl;


 void pointer_array_sort(float*values);

  int i, j;
  float*temp;
     int sortedflag=0;
     for(i=0;i<n;i++) p[i]=np+i;//INITIALIZING POINTER ARRAY
     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 Net Pay:";
    for(i=0;i<n;i++)cout<<*p(i)<<"";

return 0;
}//MAIN 
Topic archived. No new replies allowed.