This week's book giveaway is in the Agile and Other Processes forum.We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!

# How to write an algorithm for an average of values?

Ashley Kin
Ranch Hand
Posts: 36
Hi, I'm trying to write a method that uses a sentinel loop that repeatedly asks a user for input until a negative number is entered, and then prints the average.
I wrote the following code so far, but I can't figure out how to write an algorithm for the average? Any help is appreciated, thanks!

Paul Clapham
Sheriff
Posts: 21572
33
But the average is just the sum of the values divided by the number of values, isn't it?

Ashley Kin
Ranch Hand
Posts: 36
Paul Clapham wrote:But the average is just the sum of the values divided by the number of values, isn't it?

Yes, but I don't get how to represent the number of values. Is the for loop approach correct? Thanks!

John C Stewart
Greenhorn
Posts: 15
There are a couple things I would do with this method. You have some redundant code going on which can be eliminated by enclosing your prompt in a loop. This can be done several ways, one of which would be to declare a new variable and use it as your check to see if the loop continues also I would probably use an if/else construct not a while loop but that may just be preference. I am new here so I am not going to post code as I do not think it is allowed but to try to answer your original question you simply need to store your inputs in an ArrayList. Once your sentinel is entered call all of the elements in your ArrayList and do the average calculation. You will want to use an arraylist because it is a dynamic collection, meaning it grows in size as you need it to. So you will have an algorithm that calls all of your arraylist elements, adds them together and then divides that sum by the size of the arraylist. This is a very abridged version of what needs done, if you have never worked with collections let me know and I will do my best to explain it in more detail. If you need help with the actual code I will do all I can without violating the rules of this community.

Ashley Kin
Ranch Hand
Posts: 36
Thank you so much for the detailed answer, but I'm in introductory Java in high school and we haven't learned array lists yet. Right now we're working with while loops, so I figured I should probably use those. I'm just having trouble figuring out how to write an algorithm to find the average. How can I set a value for the number of values entered by the user?
Thanks!

John C Stewart
Greenhorn
Posts: 15
If you mean to track how many responses have been entered before the sentinel and subsequent termination you would use a counter variable and increment it with each pass of the loop

Campbell Ritchie
Sheriff
Posts: 51407
87
• 1
You are not testing for negative entry. You need to look at the while loop very carefully.