### 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:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147`` ``````#include #include//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; i40) 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; i40) regularhours[i]=40; else regularhours[i]=hoursworked[i]; }//FOR }//FINDREGULARHOURS void findregularpay(int regularhours[], float regularpay[], float hourlyrate[], int n){ for(int i=0; i4000.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; ii; j--){ if(table[j]

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.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include #include//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.

 ``12345678910111213141516171819`` ``````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; ii; j--) { if (payrec[j].grosspay < payrec[j-1].grosspay) { swap (j,j-1); }// end if }// end j }// end i } ``````

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:
 ``12`` ``````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:
 ``123456`` ``````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

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

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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163`` ``````#include #include//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; i40) 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; i40) regularhours[i]=40; else regularhours[i]=hoursworked[i]; }//FOR }//FINDREGULARHOURS void findregularpay(int regularhours[], float regularpay[], float hourlyrate[], int n){ for(int i=0; i4000.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; ii; j--){ if(table[j]
 ``12`` ``````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.
 ``123456`` `````` 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:
 ``123456`` ``````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?

 ``123456789101112131415`` ``````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. ``````

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150`` ``````#include #include//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; i40) 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; i40) regularhours[i]=40; else regularhours[i]=hoursworked[i]; }//FOR }//FINDREGULARHOURS void findregularpay(int regularhours[], float regularpay[], float hourlyrate[], int n){ for(int i=0; i4000.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; ii; j--){ if(table[j]

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!
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148`` ``````#include #include//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; i40) 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; i40) regularhours[i]=40; else regularhours[i]=hoursworked[i]; }//FOR }//FINDREGULARHOURS void findregularpay(int regularhours[], float regularpay[], float hourlyrate[], int n){ for(int i=0; i4000.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; ii; j--){ if(table[j]

Where would I place this sample program I made?

 ``123456789101112131415161718192021222324`` ``````#include 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*p[j+1]){ temp=p[j]; p[j]=p[j+1]; p[j+1]=temp; sortedflag=0; }//SWAP }//J }//I cout<

And than changes necessary to make it work

Topic archived. No new replies allowed.