### Converting bubble sort to use pointers

Below is a functioning payroll program. I am trying to convert the bubble sort portion of it to use pointers instead of what I've done. Can someone help me to understand how to do that? This is a past homework assignment (meaning I won't be getting any credit for changes I make to the program), but I want to know how to accomplish this conversion. Thanks in advance.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193`` ``````#include #include #include using namespace std; //function definitions int readdata(string fname[], string lname[], long int empid[], int tothrworked[], float hrrate[], int n) { ifstream fin("employee.txt"); n = 0; while (fin >> fname[n] >> lname[n] >> empid[n] >> tothrworked[n] >> hrrate[n]) n++; fin.close(); return n; }//read data function void calcothours(int tothrworked[], int othrworked[], int n) { for (int i = 0;i < n;i++) { if (tothrworked[i] > 40) othrworked[i] = tothrworked[i] - 40; else if (tothrworked[i] <= 40) othrworked[i] = 0; }//end of FOR }//calculating overtime hours void calcreghours(int reghrworked[], int tothrworked[], int n) { for (int i = 0;i < n;i++) { if (tothrworked[i] > 40) reghrworked[i] = 40; else if (tothrworked[i] <= 40) reghrworked[i] = tothrworked[i]; }//end FOR }//calculating regular hours void calcotpay(int othrworked[], float hrrate[], float otpay[], int n) { for (int i = 0;i < n;i++) { otpay[i] = (hrrate[i] * 1.5) * othrworked[i]; }//end of FOR }//calculating overtime pay void calcregpay(int reghrworked[], float hrrate[], float regpay[], int n) { for (int i = 0;i < n;i++) { regpay[i] = reghrworked[i] * hrrate[i]; }//end FOR }//calculating regular pay void calcgrosspay(float regpay[], float otpay[], float grosspay[], int n) { for (int i = 0;i < n;i++) { grosspay[i] = regpay[i] + otpay[i]; }//end FOR }//calculating gross pay void calctaxrate(float grosspay[], float taxrate[], int n) { for (int i = 0;i < n;i++) { if (grosspay[i] > 4000)taxrate[i] = 0.40; else if (grosspay[i] > 3000 && grosspay[i] <= 3999)taxrate[i] = 0.30; else if (grosspay[i] > 1000 && grosspay[i] <= 2999)taxrate[i] = 0.20; else taxrate[i] = 0.10; }//end FOR }//calculating tax rate void calctaxpaid(float grosspay[], float taxpaid[], float taxrate[], int n) { for (int i = 0;i < n;i++) { taxpaid[i] = grosspay[i] * taxrate[i]; }//end FOR }//calculating tax paid void calcnetpay(float grosspay[], float netpay[], float taxpaid[], int n) { for (int i = 0;i < n;i++) { netpay[i] = grosspay[i] - taxpaid[i]; }//end FOR }//calculating net pay void sumnet(float netpay[], float average, float sum, int n) { for (int i = 0;i < n;i++) { sum += netpay[i]; average = sum / n; } }//calculating sum of all netpays void printdata(string fname[], string lname[], long int empid[], int tothrworked[], float hrrate[], float otpay[], float grosspay[], float taxpaid[], float netpay[], float average, float sum, int n) { cout << "FIRST NAME" << "\t" << "LAST NAME" << "\t" << "EMP ID" << "\t" << "HOURS" << "\t" << "RATE" << "\t" << "OT PAY" << "\t\t" << "GROSSPAY" << "\t\t" << "TAX PAID" << "\t" << "NET PAY" << "\t" << endl; for (int i = 0;i < n;i++) { sum += netpay[i];//increment calculation for the sum of all netpays cout << fname[i] << "\t\t" << lname[i] << "\t\t" << empid[i] << "\t" << tothrworked[i] << "\t" << hrrate[i] << "\t" << otpay[i] << "\t\t" << grosspay[i] << "\t\t\t" << taxpaid[i] << "\t\t" << netpay[i] << endl << endl; }//end FOR }//print data void pointer_sort(float netpay[], int n) { int sortedflag = 1; float temp; for (int i = 0; i < n;i++) for (int i = 0; i < n; i++) { sortedflag = 1; for (int j = 0; j < n-1; j++) { if ((netpay[j+1]) < (netpay[j])) { temp = (netpay[j]); (netpay[j]) = (netpay[j+1]); (netpay[j+1]) = temp; sortedflag = 0; }//end of swap }//end of j loop }//end of i loop } void printdataaft(string fname[], string lname[], long int empid[], int tothrworked[], float hrrate[], float otpay[], float grosspay[], float taxpaid[], float netpay[], float average, float sum, int n) { cout << "NET PAY" << "\t" << endl; for (int i = 0;i < n;i++) { sum += netpay[i];//increment calculation for the sum of all netpays cout << netpay[i] << endl << endl; }//end FOR }//print data //prototyping all functions int readdata(string[], string[], long int[], int[], float[], const int); void calcothours(int[], int[], int); void calcreghours(int[], int[], int); void calcotpay(int[], float[], float[], int); void calcregpay(int[], float[], float[], int); void calcgrosspay(float[], float[], float[], int); void calctaxrate(float[], float[], int); void calctaxpaid(float[], float[], float[], int); void calcnetpay(float[], float[], float[], int); void sumnet(float[], float, float, int); void printdata(string[], string[], long int[], int[], float[], float[], float[], float[], float[], float, float, int); void pointer_sort(float[], int); void printdataaft(string[], string[], long int[], int[], float[], float[], float[], float[], float[], float, float, int); int main() { const int MAXSIZE = 100;//maximum number of employees set at 100 //declaration of local variables int n; string fname[MAXSIZE], lname[MAXSIZE]; long int empid[MAXSIZE]; int tothrworked[MAXSIZE], othrworked[MAXSIZE], reghrworked[MAXSIZE];//types of hours worked float hrrate[MAXSIZE];//pay rates float regpay[MAXSIZE], otpay[MAXSIZE], grosspay[MAXSIZE], netpay[MAXSIZE];//pay totals float taxrate[MAXSIZE], taxpaid[MAXSIZE];//taxes float average = 0, sum = 0;//sum and average of netpays for all employees; initialized these in the main function instead of as local variables //calling all functions n = readdata(fname, lname, empid, tothrworked, hrrate, MAXSIZE); calcothours(tothrworked, othrworked, n); calcreghours(reghrworked, tothrworked, n); calcotpay(othrworked, hrrate, otpay, n); calcregpay(reghrworked, hrrate, regpay, n); calcgrosspay(regpay, otpay, grosspay, n); calctaxrate(grosspay, taxrate, n); calctaxpaid(grosspay, taxpaid, taxrate, n); calcnetpay(grosspay, netpay, taxpaid, n); sumnet(netpay, average, sum, n); printdata(fname, lname, empid, tothrworked, hrrate, otpay, grosspay, taxpaid, netpay, average, sum, n); pointer_sort(netpay, n); printdataaft(fname, lname, empid, tothrworked, hrrate, otpay, grosspay, taxpaid, netpay, average, sum, n); }//end to MAIN ``````
Since this is for your own knowledge you would probably be better served to get rid of all those parallel arrays and use some kind of data structure to hold the employee data, then you wouldn't need to be passing all of those parameters into each of your functions.

And since this is C++ I'd recommend using std::vector instead of the arrays.

To change the sort to use pointers I would start by moving the "swap" into a function to keep things simpler.

 ``12345678910111213141516`` ``````void swap_values(double *a, double *b) { } void pointer_sort(double netpay[], int n) { for(int i = 0; i < n; i++) for(int i = 0; i < n; i++) { for(int j = 0; j < n - 1; j++) { swap_values(&netpay[j + 1], &netpay[j]); } } }``````

You may also want to revisit your documentation for your bubble sort, I removed the sortedflag variable because it wasn't being used. Also wouldn't a bool be a better type for this variable?

Or you could just use std::sort instead: `std::sort(netpay, netpay + n);`

first of all set your IDE # charachters in each line to fewer for better view in Post.

for better version and more on Bubble Sort view my post
http://www.cplusplus.com/forum/beginner/216609/

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` `````` void bubble_sort(long list[], long n) { long c, d, t; for (c = 0 ; c < ( n - 1 ); c++) { for (d = 0 ; d < n - c - 1; d++) { if (list[d] > list[d+1]) { /* Swapping */ t = list[d]; list[d] = list[d+1]; list[d+1] = t; } } } } void pointer_bubble_sort(long * list, long n) { long c, d, t; for (c = 0 ; c < ( n - 1 ); c++) { for (d = 0 ; d < n - c - 1; d++) { if ( * (list + d ) > *(list+d+1)) { /* Swapping */ t = * (list + d ); * (list + d ) = * (list + d + 1 ); * (list + d + 1) = t; } } } } int main() { float list[]={1,5,9,6,-1,7,-4,3,8}; //bubble_sort(list,9); //~ for (int i=0;i<9;i++){ //~ printf("%f\n",list[i]); //~ } pointer_bubble_sort(list,9); for (int i=0;i<9;i++){ printf("%f\n",list[i]); } return 0; }``````