Having trouble obtaining an average

Im trying to print out the average of some numbers that have been input. For some strange reason the average prints out the correct amount but with no decimal places. Can anyone tell me what is wrong with it. The whole program works except that part.

1
2
3
4
5
6
7
8
9
 sum = 0;
    for (int i = 0; i < max; i++)
    {   
        sum = sum + arr[i];
    }
    double average;
    average = sum / max;
    
    cout << "The average is " << average << "\n";
I'll make a guess and say sum is an integer? If sum is a double or a float it should work. (integer / integer is like the result of a division with remainder, for example the result of 5/3 is 1 instead of 1.6666, to make it work as expected just do this 5.0/3, or this 5/(double)3).
Last edited on
Thank you very much, it finally worked.
Topic archived. No new replies allowed.