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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129`` ``````#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); 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; 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; i

SAMPLE SORT PROGRAM:

 ``123456789101112131415161718192021`` ``````#include main(){ int table[]={5, 1, 3, 99, 8, 12, 7, 8, 14}; int i, j; int n=9; for(i=0; ii; j--){ if(table[j]

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:
 ``12345678910111213`` ``````void sort(int table[], int n) { int i, j; for(i=0; ii; j--){ if(table[j]

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:

 ``12345678910111213`` ``````void sort(int table[], int n) { int i, j; for(i=0; ii; j--){ if(table[j]

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
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145`` ``````#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); 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]

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:

 ``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]

Topic archived. No new replies allowed.