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.

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include<iostream>
#include<fstream>
#include<string>

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.

Maybe start with something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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/

and finally here is your answer:

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

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;
	
}


to working with Array in C Remember:
Array Name is a Pionter to First Array's Element.
Topic archived. No new replies allowed.