This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

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.

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

Joined: Mar 06, 2012
Posts: 10

posted

0

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

Joined: May 29, 2005
Posts: 479

posted

0

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

Joined: Mar 06, 2012
Posts: 10

posted

0

Thank you very much for your help.. finally got it.