I'm new to C++. I try to get the average of rainfall using total/12 months. However, for some reason the average always round-up. I have double checked all variables and debugged at the calculation to see what happen. Seen like when the total/12 it gives me back a round up number. Please give me some advice where did I did wrong?
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
using namespace std;
void assignMonths(string monthsName[SIZE], int rainFall[SIZE]);
void calRainfall(int rainFall[SIZE], int&, double&);
void displayValues(string monthsName[SIZE], int rainFall[SIZE], int total, double average);
int main()
{
string monthsName[SIZE];
int rainFall[SIZE];
int total = 0;
double average = 0;
assignMonths(monthsName, rainFall);
calRainfall(rainFall, total, average);
displayValues(monthsName, rainFall, total, average);
return 0;
}
void assignMonths(string months[SIZE], int rain[SIZE])
{
const string MONTHS[SIZE] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
srand(time(0));
for (int i = 0; i < SIZE; i++) {
months[i] = MONTHS[i];
rain[i] = rand() % 100;
}
return;
}
void calRainfall(int rain[SIZE], int& total, double& average)
{
for (int i = 0; i < SIZE; i++) {
total = total + rain[i];
}
average = total / SIZE;
return;
}
void displayValues(string months[SIZE], int rain[SIZE], int total, double average)
{
cout << "Rainfall per Month (in inches)" << endl;
cout << endl;
//Display all the values
for (int i = 0; i < SIZE; i++) {
cout << setw(30) << left << months[i] << right << rain[i] << endl;
}
cout << endl
<< setw(30) << left << "Total Rainfall: " << right << total << endl;
cout << fixed << showpoint << setprecision(4) << setw(30) << left << "Average Rainfall:" << right << average << endl;
return;
}