Question

closed account (LEUk4iN6)
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;

return average;
}
Last edited on
closed account (LEUk4iN6)
Were supposed to put 1,2,3,4,5,6,7,8,9,10,11,12 as test data.
Last edited on
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
Last edited on
closed account (LEUk4iN6)
Would I have to change the prototype at the beginning to double also then? If I try that I get an error of the following...

error C2664: 'GetAverage' : cannot convert parameter 1 from 'int [12]' to 'double []'

warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data

and

if I leave it at int I get the following

warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data
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).

See if that helps.
also, you should initialize average

int average = 0

Your compiler may yell at you otherwise (mine did).

EDIT:

When I gave my advice above (about changing 12 to 12.0), you're going to have to change your return value on your function to double

i.e.
1
2
3
4
double function(parameters){
//code here
return average/12.0
}
Last edited on
closed account (LEUk4iN6)
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
}


Last edited on
closed account (LEUk4iN6)
Well no errors thanks, it just still outputs 1 I'm not sure where I'm going wrong.

double GetAverage( int rainAmt[] ) // Gets average of all the rainfall.
{
double average = 0;
for(int num=1; num < 12; num++)

average = rainAmt[num];

return average/12.0; // Will return the average of the rainfall.
}
it's because you didn't change

average = rainAmt[num];

to

average += rainAmt[num];
closed account (LEUk4iN6)
Ahh thanks a million + 1, I switched it earlier and it was saying unitialized variable, but that must've been because I had int and double backwards?
You're welcome. I got the "uninitialized variable" error too but it was because average wasn't set equal to zero.

Glad I could help =D
closed account (LEUk4iN6)
Working on my next problem, not done yet, but it should still run. I'm getting the following error..

fatal error LNK1169: one or more multiply defined symbols found

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

// Allows you to just change the number in 1 spot.
const int SIZE=7;

int main()
{
	int empID[SIZE];
	int hours[SIZE];
	double payRate[SIZE];
	double wages[SIZE];

	for(int x=0; x<SIZE; x++)
	{
		cout << "Enter ID: ";
		cin >> empID[x];
		cout << "Enter hours: ";
		cin >> hours[x];
		cout << "Enter payRate: ";
		cin >> payRate[x];
		wages[x] = hours[x] * payRate[x];
	}
	return 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.
Last edited on
Topic archived. No new replies allowed.