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.
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.
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.
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.