Next time use [/CODE][CODE] tags. It just makes things easier to read.
The reason it is giving you 1 is because you are not adding all of your rainfall numbers together; you are only overwriting average every time the computer runs through the loop.
The way you have it written the computer is seeing:
average = 1/12 = 0 (because it's integer division)
average = 2/12 = 0 (because it's integer division)
... (and so on until the last ones)
average = 11/12 = 0 (because it's integer division)
average = 12/12 = 1
Since 12 is the last element in the array, average stays 1 and that is what is returned.
I suggest doing something to the effect of:
1 2 3 4 5 6
|
int GetAverage(int rainAmt[])//note you don't need to put 12 in the brackets
{
double average;
for(int num=0; num<12;num++){
average += rainAmt[num];
}
|
Divide by 12 outside of your for loop so that the entire sum is divided by 12 and not just the individual elements. I also suggest changing your average variable to a double so that you can get the decimal value (unless you have to use an integer).
Hopefully this wasn't too confusing lol