My program is below, however it keeps outputting 1 as the average rainfall (the last function) instead of the actual average. Any help?
#include <iostream>
using namespace std;
int GetLargest( int rainAmt[12] ); // Prototype.
int GetSmallest( int rainAmt[12] ); // Prototype.
int GetTotal( int rainAmt[12] ); // Prototype.
int GetAverage( int rainAmt[12] ); // Prototype.
int main()
{
int rainAmt[12];
for( int x=0; x<12; x++ )
{
cout << "Enter rainfall amount: ";
cin >> rainAmt[x];
}
cout << "Month with highest rainfall is " << GetLargest(rainAmt) << endl;
cout << "Month with lowest rainfall is " << GetSmallest(rainAmt) << endl;
cout << "The total rainfall is " << GetTotal(rainAmt) << endl;
cout << "The average rainfall is " << GetAverage(rainAmt) << endl;
return 0;
}
int GetLargest( int rainAmt[12] )
{
int largestAmt;
int largestMnth;
largestAmt = rainAmt[0];
largestMnth = 0;
// If statement to get the month with the largest amount of rainfall.
for(int x=1; x<12; x++)
if (rainAmt[x] > largestAmt)
{
largestAmt = rainAmt[x];
largestMnth = x;
}
return largestMnth; // Will return the largest month of rainfall.
}
int GetSmallest( int rainAmt[12] )
{
int smallestAmt;
int smallestMnth;
smallestAmt = rainAmt[0];
smallestMnth = 0;
// If statement to get the month with the least amount of rainfall.
for(int x=1; x<12; x++)
if (rainAmt[x] < smallestAmt)
{
smallestAmt = rainAmt[x];
smallestMnth = x;
}
return smallestMnth; // Will return the lowest month of rainfall.
}
int GetTotal( int rainAmt[12] ) // Gets total of all the rainfall.
{
int total = 0;
for(int num=1; num < 12; num++)
total += rainAmt[num];
return total;
}
int GetAverage( int rainAmt[12] ) // Gets average of all the rainfall.
{
int average;
for(int num=1; num < 12; num++)
average = rainAmt[num] / 12;
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).
you could try leaving average as an integer (meaning you would be able to keep your prototype and everything as it is) and divide by 12.0 instead of just 12 (this is just a blind guess as I don't have my IDE open right now).
int GetAverage( double rainAmt[] ) // Gets average of all the rainfall.
{
double average;
for(int num=1; num < 12; num++)
average = rainAmt[num];
return average/12.0; // Will return the average of the rainfall.
}
That's what I'm using with double as my prototype also and I'm getting the following errors.
error C2664: 'GetAverage' : cannot convert parameter 1 from 'int [12]' to 'double []'
warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data
sorry, I think you misunderstood me. I meant to do it like this:
1 2 3 4 5
double GetAverage(int rainAmt[]){
int average = 0 //I tested this code and saw that average could (and should) stay as an int
//code here
return average/12.0
}
Sounds like you have multiple cpp files, and each file declares a global var with the same name (probably 'SIZE' since that's the only global I see here).
EDIT: or each file has a main()
Remember that when making a new program you need to create a whole new project. You can't just make a new cpp file because a different cpp might still be part of the same program.