I have this array that should be returning a value in the 50's due to the data input i am giving it but it is returning 0 everytime

What the code should do is input to and array from the file then pass it to the array monthSum then it should be divided by 12 and put into the array monthAverage then i compare it to against the value "max" to find the largest one which is returned to the main function.

any ideas would be greatly appreciated

int FindMax()
{
	inFile.close();
	inFile.open("HW01.dat");
	int dummy, max;
	int monthInput[month];
	int monthSum[month];
	int monthAverage[month];
	max = 0;
	//stores lineCount variable since it was already pulled in and not needed again
	inFile >> dummy;

	//pulls in the temperatures for the array to hold the values
	for(int i=0; i<lineCount; i++)
	{
		inFile >> monthInput[i];
		//adds the temperatures together and stores them in monthSum
		for(int j=0; j<month; j++)
		{
			monthSum[j] = monthInput[i] + monthSum[j];
		}
	}
	//calculates the average temperature after all the temperatures are summed
	for(int i=0; i<month; i++)
	{
		for(int j=0; j<month; j++)
		{
			monthAverage[i] = monthSum[j] / month;
		}
	}
	//compares the values to find the max average temperature
	for(int i=0; i<month; i++)
	{
		if(monthAverage[i] > max)
		{
			max = monthAverage[i];
		}
	}
	//returns max to the main() for output
	return max;
}

Two possibilities:

  1. Garbage in, garbage out -- The calculation code is not getting the input you think it is, so it's giving bad results.
  2. Good data in, garbage data. The data being delivered to the calculation code is valid, so the calculation code is wrong.

Figure out which of these two scenarios applies. Do that by writing code that displays both the size and contents of the arrays after line 22 and confirming everything's right.

If it's wrong, look at the lines up to line 22. Otherwise, look later.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.