programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Junilu Lacar
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Rob Spoor
• Bear Bibeault
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Piet Souris
• Carey Brown
• Stephan van Hulst
Bartenders:
• Frits Walraven
• fred rosenberger
• salvin francis

# The Art & Science of Java Chapter 4 Exercise 5 AverageList.java

Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
Hi guys.

Im trying to figure out The AverageList exercise and I think I've got it but just missing that last final piece in the puzzle. I cant think of what to do from here..

I know the int average = total/value; is wrong but i just cant figure out how to get the average. I can add the exam scores together.....

Could anybody help me out with this one? Any input is much appreciated.

Bartender
Posts: 1051
5
• Number of slices to send:
Optional 'thank-you' note:
For an average, you need to keep a count of the number of exam scores entered.

M Ford
Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
sorry but as you can gather i am a complete novice at this... can you show me the syntax for that?

Ranch Hand
Posts: 479
• Number of slices to send:
Optional 'thank-you' note:

M Ford wrote:sorry but as you can gather i am a complete novice at this... can you show me the syntax for that?

You already have a loop that gathers the total of the numbers entered. The average of them is not the total divided by any of the values, but the total divided by the number of entries. In other words, if the user enters 5 values, it is the total divided by 5. So declare another integer variable outside the loop, set to 0. Each time through the loop, add one to it (count = count + 1). After the loop is done, divide the total by the number of entries to get your average. Or you can do the division after each of the entries, for the average "up to that point".

M Ford
Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
I understand how to get the average. I've tried different ways to add how many times an integer is entered so I can divide the total by that but I just cant seem to figure it out. Tried creating another integer outside the loop but which ever way I do it, instead of adding how many times the integers are being added, it just adds the value for them instead.

Ralph Cook
Ranch Hand
Posts: 479
• Number of slices to send:
Optional 'thank-you' note:

I moved the calculation of the average to outside the loop. This doesn't make much difference, but you may as well do things within the loop that depend on being in the loop, and other things outside it. Someone might wonder why you calculate the average every time through the loop, since you don't use it there.

Since your average value is an integer, you will only have an integer value for the average, and I think integer division will truncate (throw away any fractional portion) instead of rounding. If you wanted fractional values, you could use a float for average, or you could call a rounding function on your integer calculation if you wanted to do it that way.

M Ford
Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
Thank you very much for your help.. finally got it.