(1) Would you please use CODE BLOCKS when posting code. Shabbir is nicely doing that for you but please do it yourself. You have read the posting guidelines, haven't you? If not, do it now; they are described there.
(2) The code as presented doesn't compile so I'm not sure what you think you're running. average is not defined so
average = totalCharge/y;
throws an error.