M Ford

Greenhorn

Posts: 10

posted 5 years ago

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.

[HENRY: Added code tags]

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.

[HENRY: Added code tags]

M Ford

Greenhorn

Posts: 10

Ralph Cook

Ranch Hand

Posts: 479

posted 5 years ago

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

posted 5 years ago

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

posted 5 years ago

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.

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.

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |