0

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;
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Tobi
  • 65
  • 5
  • 1
    "I have double checked all variables and debugged at the calculation". Okay, and what were the results of doing so? It sounds like you could have included _just_ the relevant calculation without tons of code that has nothing whatsoever to do with your question. – Nathan Pierson Jul 16 '22 at 06:12
  • 1
    `total/SIZE` is integer division. Try `1.0*total/SIZE`. – chux - Reinstate Monica Jul 16 '22 at 06:13
  • 1
    Anyway. `total` is an `int`. `SIZE` is a mystery, but is probably also an `int`. `total/SIZE` is an `int` divided by an `int` which is, in turn, an `int`. By the time you assign it to a `double`, the rounding has already happened. You'll need to cast one of those to a floating point value _before_ performing the division. – Nathan Pierson Jul 16 '22 at 06:13
  • What is `SIZE`? I presume it is an `int` of some kind. What happens when you divide an `int` by an `int`? – jkb Jul 16 '22 at 06:14
  • As already said you are doing integer devision (which truncates towards zero, it doesn't round up). Try this `average=(double)total/SIZE;` – john Jul 16 '22 at 06:32
  • Thank you for all the help. Problem come from integer/ integer. – Tobi Jul 16 '22 at 20:39

0 Answers0