Sorting a Netpay Array with Pointers

So I know this question/issue has come up before on these forums. I've been through them researching and trying to debug my code for this for awhile now as well and think I need help at this point. In addition I have to write this program with arrays (I can't use struct as we have not covered it yet) plus the assignment is to do it with arrays and pointers.

The program I have written below works and runs without returning errors. However, it does not compute correctly. I have the same data plugged into a program that does not make use of pointers and it correctly returns the following data...
________________________________________________________________________________
NETPAY
405.00
450.00
486.00
720.00
756.00
832.00
880.00
900.00
1150.00
1400.00
==================================
The netpay average is: 797.90
________________________________________________________________________________
This program using pointers returns the data...(It should look exactly like the first output)
NETPAY
-107374176.00
405.00
450.00
486.00
720.00
756.00
832.00
880.00
900.00
1150.00
==================================
The netpay average is: -1.#j
________________________________________________________________________________
My Input file is:
1645 40 25.00
8932 40 20.00
7104 40 12.50
2816 40 26.00
5387 40 21.00
6780 40 13.50
5641 40 11.25

9000 60 25.00
9001 50 20.00
9002 55 23.00
________________________________________________________________________________
My question is how do I correct my program which makes use of pointers with so that -107374176.00 does not appear and instead displays the missing 1400 number. Plus how do I fix the netpay average so it displays correctly instead of showing -1.#j. I'm not quite sure but I think the issues are related. I'm quite new to programing and posting for help on forums so please feel free to offer advice on either as well.

Thanks

__________________________________________________________________________
1.#include <iostream>
2.#include <fstream>//file input output stream
4.#include <iomanip>
5.using namespace std;
6. //function prototypes
7. int readalldata(long int[], int[], float[], const int);
8. void findovertimehours(int[], int[], int);
9. void findovertimepay(int[], float[], float[], int);
10. void findregularhours(int[], int[], int);
11. void findregularpay(int[], float[], float[], int);
12. void findgrosspay(float[], float[], float[], int);
13. void findtaxrate(float[], float[], int);
14. void findtaxamount(float[], float[], float[], int);
15. void findnetpay(float[], float[], float[], int);
16. void printalldata(long int[], int[], float[], float[], float[], 17.float[], float[], int);
18. void pointer_array_sort(float[], int);
19.
20. int main(){
21. const int MAXSIZE = 50; //for maximum of 100 employees
22.
23. //decleration of variables
24. int n;
25. long int id[MAXSIZE];
26. int hoursworked[MAXSIZE], overtimehours[MAXSIZE];
27. int regularhours[MAXSIZE];
28. float hourlyrate[MAXSIZE], regularpay[MAXSIZE],
29. overtimepay[MAXSIZE], grosspay[MAXSIZE];
30. float taxrate[MAXSIZE], taxamount[MAXSIZE], netpay[MAXSIZE];
31. char name[MAXSIZE];
32.
33. //function calls
34. n = readalldata(id, hoursworked, hourlyrate, MAXSIZE); 35. //get all data
36. findovertimehours(hoursworked, overtimehours, n);
37. findovertimepay(overtimehours, hourlyrate, overtimepay, n);
38. findregularhours(hoursworked, regularhours, n);
39. findregularpay(regularhours, regularpay, hourlyrate, n);
40. findgrosspay(regularpay, overtimepay, grosspay, n);
41. findtaxrate(grosspay, taxrate, n);
42. findtaxamount(grosspay, taxamount, taxrate, n);
43. findnetpay(grosspay, netpay, taxamount, n);
44. printalldata(id, hoursworked, hourlyrate, overtimepay,
45. grosspay, taxamount, netpay, n);
46. pointer_array_sort(netpay, n);
47. printalldata(id, hoursworked, hourlyrate, overtimepay,
48. grosspay, taxamount, netpay, n);
49. return 0;
50.
51. }//MAIN
52. //function definitions
53. int readalldata(long int id[], int hoursworked[], float hourlyrate[], 54. int n){
55. ifstream fin("employee.txt");
56. n = 0;
57.
58. while (fin >> id[n] >> hoursworked[n] >>hourlyrate[n]) 59. n++;
60. fin.close();
61. return n;
62.
63. }//READALLDATA
64.
65. void findovertimehours(int hoursworked[], int overtimehours[], int n){
66. for (int i = 0; i<n; i++){
67. if (hoursworked[i]>40) overtimehours[i] = 68.hoursworked[i] - 40;
69. else overtimehours[i] = 0;
70.
71. }//FOR
72.
73.}//FINDOVERTIMEHOURS
74.
75. void findovertimepay(int overtimehours[], float hourlyrate[],
76. float overtimepay[], int n){
77. for (int i = 0; i<n; i++){
78. overtimepay[i] = overtimehours[i] * hourlyrate[i] * 1.5;
79.
80. }//FOR
81.
82. }//FINDOVERTIMEPAY
83.
84. void findregularhours(int hoursworked[], int regularhours[], int n){
85. for (int i = 0; i<n; i++){
86. if (hoursworked[i]>40) regularhours[i] = 40;
87. else regularhours[i] = hoursworked[i];
88.
89. }//FOR
90.
91. }//FINDREGULARHOURS
92.
93. void findregularpay(int regularhours[], float regularpay[],
94. float hourlyrate[], int n){
95. for (int i = 0; i<n; i++){
96. regularpay[i] = regularhours[i] * hourlyrate[i];
97.
98. }//FOR
99.
100. }//FINDREGULARPAY
101. void findgrosspay(float regularpay[], float overtimepay[],
102. float grosspay[], int n){
103. for (int i = 0; i<n; i++){
104. grosspay[i] = regularpay[i] + overtimepay[i];
105.
106. }//FOR
107.
108. }//FINDGROSSPAY
109.
110. void findtaxrate(float grosspay[], float taxrate[], int n){
111. for (int i = 0; i<n; i++){
112. if (grosspay[i]>4000.00) taxrate[i] = 0.40;
113. else if (grosspay[i]>3000.00) taxrate[i] = 0.30;
114. else if (grosspay[i]>1000.00) taxrate[i] = 0.20;
115. else taxrate[i] = 0.10;
116.
117. }//FOR
118.
119. }//FINDTAXRATE
120.
121. void findtaxamount(float grosspay[], float taxamount[],
122. float taxrate[], int n){
123. for (int i = 0; i<n; i++){
124. taxamount[i] = grosspay[i] * taxrate[i];
125.
126. }//FOR
127.
128. }//FINDTAXAMOUNT
129.
130. void findnetpay(float grosspay[], float netpay[], float taxamount[], 131. int n){
132. for (int i = 0; i<n; i++){
133. netpay[i] = grosspay[i] - taxamount[i];
134.
135. }//FOR
136.
137. }//FINDNETPAY
138. void printalldata(long int id[], int hoursworked[], float hourlyrate[],
139. float overtimepay[], float grosspay[], float taxamount[],
140. float netpay[], int n){
141. float totalNetPay = 0;
142. int i = 0;
143. cout << setw(48) << "PAYROLL INSTITUTE" << 144. endl;
145. cout << setw(40) << "106 EASY WAYS LANE" << endl;
146. cout << setw(43) << "PLEASANTVILLE N.Y. 11068" << endl;
147. cout << " " << endl << endl;
148. cout <<
149."============================================================ " << endl;
150. cout //<< "EMP ID" << setw(7)
151. //<< "HOURS" << setw(6)
152. //<< "RATE" << setw(10)
153. //<< "OVERPAY" << setw(11)
154. //<< "GROSSPAY" << setw(8)
155. //<< "TAX" << setw(11)
156. << "NETPAY" << endl;
157. cout << fixed << setprecision(2);
158. for (int i = 0; i<n; i++){
159. cout //<< "" << id[i] << setw(7)
160. //<< hoursworked[i] << setw(9)
161. //<< hourlyrate[i] << setw(7)
162. //<< overtimepay[i] << setw(12)
163. //<< grosspay[i] << setw(10)
164. //<< taxamount[i] << setw(10)
165. << netpay[i] << endl;
166.
167. }//FOR
168. cout <<
169."============================================================ " << endl;
170. cout << "The net pay average of the employees are: " <<
171.totalNetPay / i << endl;
172. system("pause");
173. cout << endl;
174. cout << endl;
175. }//PRINTALLDATA
176.
177. void pointer_array_sort(float netpay[], int n){
178. float tmp;
179. bool sortedflag = 0;
180. while (!sortedflag){
181. sortedflag = true;
182. for (int j = 0; j<n; j++){
183. if (netpay[j]>netpay[j + 1]){
184. tmp = netpay[j];
185. netpay[j] = netpay[j + 1];
186. netpay[j + 1] = tmp;
187. sortedflag = false;
188.
189. }//SWAP
190.
191. }//J
192.
193. }//I
194.
195.}
196. //end source code
closed account (D80DSL3A)
Your for loop on line 182 goes one too far:
182. for (int j = 0; j<n; j++){
It should be:
182. for (int j = 0; j<n-1; j++){
so that the j+1 index stays in range of 0 to n-1.
Thanks a ton that's something I never would have noticed. I thought I had something much more major wrong. Now the 1400 displays correctly in the second part instead of the -107374176.00.

The net pay average is still showing up as -1.#j though so I'll have to figure out why that's happening.

Still half way there and thanks again!
closed account (D80DSL3A)
The problem with that?
1
2
141. float totalNetPay = 0;
142. int i = 0;


Neither value is modified, so they are still both = 0 at line 171
171.totalNetPay / i << endl;// 0/0 is not good.
Note. The 'i' used on line 171 is not the 'i' from the for loop.
So, calculate the totalNetpay, then divide by n (not i).
Yeah I fooled around with the program a little bit after your help with the for (int j = 0; j<n-1; j++){ line and realized I was trying to divide 0 by 0 which would cause weird errors to happen.

What I've got to do now is as you've said calculate the totalNetpay so I can divide it by n as you've advised.

I've started to look at how to add up all the individual net pays to get the totalNetpay so I can divide them (mathematically this has always made sense to me). I'm just not sure how to program the sum off all my individual net pays off the top of my head.

I like to get things done early and I've got time on this yet so I'm calling it a night. Hopefully I will be thinking clearer tomorrow when I look up how to calculate the sum off all my individual net pays.

You've really been a great help so thanks again!

closed account (D80DSL3A)
You're welcome.
It looks like you left a space for the total to be found!
This at line 166 should do the trick:
totalNetPay += netpay[i];
Got this 100% complete now thanks to the help you've provided.

I also managed to have one of those doh moments as well. Before I got on here I was looking at the code from my previous working programs (the ones not using pointers) and I had that totalNetPay += netpay[i]; line in each of them but couldn't see that I was missing it in the pointer program. Which also explains why I had a blank line waiting for it to go into.

Also I have a tendency to think that when something goes wrong it must be something big that needs changing.

Anyway the next chapters look more organized then trying to put a pointer into an array (that just seemed like a mess) so hopefully they go better.
Topic archived. No new replies allowed.