Sorting First and Last Name with Netpay

So my ongoing work sorting my net pay program continues. At this point I've got it working to the point where it sorts the net pay and only the net pay. I've been told I can comment out the other variables (hours worked, hourly rate, etc...) with the exception of First Name, and Last Name which I need to sort along with Net Pay.

I need to sort First Name and Last Name along with Net Pay so that while Net Pay is being sorted it still corresponds to the same individual.

Right now as I'm only sorting Net Pay and individuals end up with different Net Pays as the sorting occurs. This is what I need to correct. However, I'm not exactly sure how to do this.
_____________________________________________________________________________

This is the programs current output (Pre Sorting)

John Doe 900.00
Jane Smith 720.00
Chris Davis 450.00
Sara White 832.00
Alice Baker 756.00
Laura Jackson 486.00
Jack Benson 405.00
Kate Russle 1400.00
Matt Jones 880.00
Fred Shultz 1150.00
______________________________________________________________________________

This is the programs current output (Post Sorting)

John Doe 405.00
Jane Smith 450.00
Chris Davis 486.00
Sara White 720.00
Alice Baker 756.00
Laura Jackson 832.00
Jack Benson 880.00
Kate Russle 900.00
Matt Jones 1150.00
Fred Shultz 1400.00

As you can see the correct people are no longer making the correct Net Pay!
_______________________________________________________________________________

Arranging the data outside programing logically shows it should look like this!

Jack Benson 405.00
Laura Jackson 486.00
Jane Smith 720.00
Alice Baker 756.00
Sara White 832.00
Matt Jones 880.00
John Doe 900.00
Fred Shultz 1150.00
Kate Russle 1400.00
_______________________________________________________________________________

Is there a way for me to sort my employee's First Name, Last Name, and Net Pay
so that everyone is recieving the correct Net Pay? If so how do I accomplish this?

_______________________________________________________________________________

Below is my current program...
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
#include <iostream>
#include <iomanip>
#include<fstream>
#include <string>
using namespace std;

//function prototypes
int readalldata(long int id[], string fname[], string lname[], int hoursworked[], float hourlyrate[], int n);
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 id[], string fname[], string lname[], int hoursworked[], float hourlyrate[],
	float overtimepay[], float grosspay[], float taxamount[],
	float netpay[], int n);
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];
	string fname[MAXSIZE], lname[MAXSIZE];

	//function calls
	n = readalldata(id, fname, lname, 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, fname, lname, hoursworked, hourlyrate, overtimepay,
		grosspay, taxamount, netpay, n);
	sort(netpay, n);
	printalldata(id, fname, lname, hoursworked, hourlyrate, overtimepay,
		grosspay, taxamount, netpay, n);
	sort(netpay, n);
	return 0;
}//MAIN
//fUNCTION DEFINITIONS
int readalldata(long int id[], string fname[], string lname[], int hoursworked[], float hourlyrate[], int n){
	ifstream fin("employee.txt");
	n = 0;

	while (fin >> id[n] >> fname[n] >> lname[n] >> hoursworked[n] >> hourlyrate[n])
		n++;

	fin.close();
	return n;
}//READALLDATA

void findovertimehours(int hoursworked[], int overtimehours[], int n){
	for (int i = 0; i<n; i++){
		if (hoursworked[i]>40) 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; i<n; i++){
		overtimepay[i] = overtimehours[i] * hourlyrate[i] * 1.5;
	}//FOR
}//FINDOVERTIMEPAY

void findregularhours(int hoursworked[], int regularhours[], int n){
	for (int i = 0; i<n; i++){
		if (hoursworked[i]>40) regularhours[i] = 40;
		else regularhours[i] = hoursworked[i];
	}//FOR
}//FINDREGULARHOURS

void findregularpay(int regularhours[], float regularpay[],
	float hourlyrate[], int n){
	for (int i = 0; i<n; i++){
		regularpay[i] = regularhours[i] * hourlyrate[i];
	}//FOR
}//FINDREGULARPAY
void findgrosspay(float regularpay[], float overtimepay[],
	float grosspay[], int n){
	for (int i = 0; i<n; i++){
		grosspay[i] = regularpay[i] + overtimepay[i];
	}//FOR
}//FINDGROSSPAY

void findtaxrate(float grosspay[], float taxrate[], int n){
	for (int i = 0; i<n; i++){
		if (grosspay[i]>4000.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<n; i++){
		taxamount[i] = grosspay[i] * taxrate[i];
	}//FOR
}//FINDTAXAMOUNT

void findnetpay(float grosspay[], float netpay[], float taxamount[], int n){
	for (int i = 0; i<n; i++){
		netpay[i] = grosspay[i] - taxamount[i];
	}//FOR
}//FINDNETPAY

void printalldata(long int id[], string fname[], string lname[], int hoursworked[], float hourlyrate[],
	float overtimepay[], float grosspay[], float taxamount[],
	float netpay[], int n){
	float totalNetPay = 0;

	int i = 0;
	cout << setw(62) << "PAYROLL INSTITUTE" << endl;
	cout << setw(55) << "106 EASY WAYS LANE" << endl;
	cout << setw(58) << "PLEASANTVILLE N.Y. 11068" << endl;
	cout << " " << endl << endl;
	cout << "==================================================================== " << endl;
	cout << fixed << setprecision(2);
	cout << "EMP ID" << setw(12)
		<< "FIRST NAME" << setw(12)
		<< "LAST NAME" << setw(7)
		<< "HOURS" << setw(6)
		<< "RATE" << setw(10)
		<< "OVERPAY" << setw(11)
		<< "GROSSPAY" << setw(8)
		<< "TAX" << setw(11)
		<< "NETPAY" << endl;

	for (i; i<n; i++){
		totalNetPay += netpay[i];
		cout << "" << id[i] << setw(11)
			<< fname[i] << setw(13)
			<< lname[i] << setw(7)
			<< hoursworked[i] << setw(9)
			<< hourlyrate[i] << setw(7)
			<< overtimepay[i] << setw(12)
			<< grosspay[i] << setw(10)
			<< taxamount[i] << setw(10)
			<< netpay[i] << endl;
	}//FOR

	cout << "==================================================================== " << endl;
	cout << "AVERAGE NETPAY FOR ALL EMPLOYEES IS:" << setw(7) << totalNetPay / i << endl;
	cout << endl;
	system("PAUSE");
	cout << endl;
	cout << endl;
}//PRINTALLDATA

void sort(float table[], int n)
{
	int i, j;

	for (i = 0; i<n - 1; i++){
		for (j = n - 1; j>i; j--){
			if (table[j]<table[j - 1]){
				float hold = table[j];
				table[j] = table[j - 1];
				table[j - 1] = hold;
			}// end if
		}// end j
	}// end i
}
//end source code
}
The problem is that you are sorting netpay which will sort by value, but the name arrays will remain in the same positions so after sort they wont match up (index positions).

Are you allowed to use structures for this, or must it be separate arrays? - creating a structure stored in a vector would be so much easier for you.
I'm not allowed to use structures. The advice I've been given since posting last is to use swap to move the employee names around. I've experimented with doing this but I'm just starting out at programing, it's not something I'm very good at, and I haven't been successful with it so far.

Any help would very much be appreciated!

Edit: Structs is the next thing we're working on and I can't wait to stop having to cobble together code. I haven't looked at it much but it seems much easier to work with.
Last edited on
You have three arrays where corresponding items are related.

If you change only one array, then the relationships between corresponding items is broken.

Since you aren't allowed to do anything intelligent with this project, I suggest you cheat and add another array:

1
2
3
4
5
6
7
8
9
int indices[MAXSIZE];
  // Indices into all the other arrays of an employee. To access an employee, 
  // you only need to dereference this array. For example:
  //
  //    float hourly_pay = hoursworked[indices[0]] * hourlyrate[indices[0]]
  //
  // The consequence is that it does not matter how employees are 
  // organized in the other arrays. But you can sort this array to your 
  // heart's content. 
1
2
// Don't forget to initialize your indices
for (int n = 0; n < MAXSIZE; n++) indices[n] = n;

Your sort function will take an interesting twist too:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void sort_by_float( 
	int indices[],  // sorts me!
	int n,          // number of employees/indices
	float floats[]  // the items we are comparing
	){
	// Evil bubble sort!
	int i, j;
	for (i = 0; i<n - 1; i++){
		for (j = n - 1; j>i; j--){
			if (floats[indices[j]]<floats[indices[j - 1]]{
				int index = indices[j];
				indices[j] = indices[j - 1];
				indices[j - 1] = index;
			}
		}
	}
}

Notice that it sorts the indices into all the employees -- it does not sort the individual employee tables -- maintaining the corresponding one-to-one relationship between the tables.


You could, of course, just pass all the other (corresponding) tables into your sort function as well:

1
2
3
4
5
6
7
8
9
10
11
12
13
void sort_by_net_pay( string fname[], string lname[], float netpay[], int n )
{
	...
				if (netpay[j]<netpay[j - 1]){
					//swap fname[j] and fname[j-1]
					...
					//swap lname[j] and lname[j-1]
					...
					//swap netpay[j] and netpay[j-1]
					...
				}
	...
}

Hope this helps.
Thanks I just saw your post and I'm going to try out some of the things you've mentioned. I agree though that the project is an exercise in doing things the unintelligent way.

Edit: I think I've got what I need now thanks for all the help. I also appreciated the humor in your response to trying to solve the problem in a silly programing manner.
Last edited on
:O)

The worry is whether or not your professor will agree...
Topic archived. No new replies allowed.