I have a function that should read in data from a file and add them to and array then i pass that array to another that totals the data then i want to find the average of that array and pass the max value to a variable 'max'

with the code it runs with a runtime error that when i hit ignore it gives me a random value for the output

any ideas would be greatly apprecitated :)

int FindMax()
{
	inFile.close();
	inFile.open("test.txt");
	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=1; i<=lineCount; i++)
	{
		inFile >> monthInput[i];
		//adds the temperatures together and stores them in monthSum
		for(int j=1; j<=month; j++)
		{
			monthSum[j] = monthInput[i] + monthSum[j];
		}
	}
	//calculates the average temperature after all the temperatures are summed
	for(int i=1; i<=month; i++)
	{
		monthAverage[i] = monthSum[i] / month;
	}
	//compares the values to find the max average temperature
	for(int i=1; i<=month; i++)
	{
		monthAverage[i];
		if(monthAverage[i] > max)
		{
			max = monthAverage[i];
		}
	}

Recommended Answers

All 14 Replies

In the future, please post a complete program.

However.... Even though this program is incomplete, I can see a serious problem: In C++, arrays start with an index of 0. So when you write a loop such as

for(int i=1; i<=month; i++)	{
	monthAverage[i] = monthSum[i] / month;
}

that loop cannot possibly work: It ignores the first element (monthSum[0]) and tries to use a nonexistent element that is past the end of the array (monthSum[month]).

1 is consistent throughout the entire program in all the arrays would that still affect it?

1 is consistent throughout the entire program in all the arrays would that still affect it?

If you ever do monthAverage[0], monthSum[0], or monthInput[0] it will be garbage!
But your problem is with your for loop condition. In all of your for loop you use i <= someVariable. The problematic point is the <=. Using that, your for loop would loop through array[1] to array[constSize], while in C++, the valid range for arrays is
array[0] to array[constSize-1].

If you want to ignore element number 0 in every array, you can certainly do that. It's not good style, but it can be made to work.

But in order to make it work, you have to make every array at least one element bigger than you think you need. In other words:

int foo[10];
for (int i = 0; i <= 10; ++i)
    foo[i] = 0;

This example never does anything with foo[0]. By itself, there's no harm in doing that. However, it also attempts to access foo[10], and that's wrong. There is no such element as foo[10], and there's no telling what the program will do when run.

So if you want to use this technique, at the very least you have to define foo to have 11 elements, not 10.

I understand the points you are both saying but in the runtime i error recieve its telling me that the stacks around the variable monthSum and monthAverage have been corrupted which im curious to understand how that could cause it

monthSum and monthAverage have a length of some size 'month'

They start at monthSum[0] and go all the way to monthSum[month-1].

If you write something to monthSum[month], you will be writing off the end of the array (which lives on the stack) and over something else that is on the stack next to this array.

Put simply, monthSum[month] does not exist and by trying to write to it you are writing over something else on the stack.

Out of interest, how are you defining month? Is that a constant somewhere?

it's a constant of 12 defining the 12 months so i was hoping to go from 1-12 but i guess from what im hearing so far if i start my counter at 0 and switch month to 11 then i should be fine

That's correct. You make an array with 12 elements; you get array[0] to array[11], which is twelve elements in total.

As another aside, that's Andrew Koenig.

Andrew. Freaking. Koenig.

We're in a discussion thread with Andrew. Koenig. :)

Hasn't he been dead over a whole year lol?

And even with switching the counts to starting at 0 im still getting the corrupt values in the FindMax() function so posting with entire code.

Thanks for the continued help guys

/*
Alex McCracken
Jan 25, 2010
Homework 1
*/

#include <string>
#include <iostream>
#include <fstream>

using namespace std;

int highestArea();
int monthName();
int highestTemp();
int FindMax();
ifstream inFile;
const int month = 11;
int averageStore[month];
int yearAverage[month];
int lineCount, tempMonth;

int main()
{


	inFile.open("HW01.dat");

	if(inFile.fail())
	{
		cout << "Invalid file name";
		return 0;
	}

	//finds the number of rows subtracts row since count starts at 0
	inFile >> lineCount;
	lineCount = lineCount - 1;

	//calculates and stores of average of the rows
	for(int i=0; i<=lineCount; i++)
	{
		//pulls in a row and stores it
		for(int j=0; j<=month; j++)
		{
		inFile >> averageStore[j];
		}
		int yearSum = 0;
		//calculates the row total
		for(int k=0; k<=month; k++)
		{
			yearSum = yearSum + averageStore[k];
		}
		yearAverage[i] = yearSum/month;

	}


cout << "Alex McCracken" << endl << "CpSc 150 Section 01" << endl << "Assignment 1" << endl << endl;
cout << "The area with the highest average is area " << highestArea() << " with a temperature of " << highestTemp() << "." <<endl;
cout << "The month with the highest average temperature is " << monthName() << " with a temperature of " << FindMax() << endl;
return 0;
}

int highestTemp()
{
	int highTemp;
	highTemp = 0;
	//compares the values in the yearAverage array to find the highest temperature
	for(int i=0; i<=lineCount; i++)
	{
		if(yearAverage[i]>=highTemp)
		{
			highTemp = yearAverage[i];
		}
	}

	return highTemp;
}
int highestArea()
{
	int highTemp, count;
	highTemp = 0;
	count = 0;
	//compares the values in the yearAverage array to find the highest temperature
	for(int i=0; i<=lineCount; i++)
	{
		if(yearAverage[i]>=highTemp)
		{
			highTemp = yearAverage[i];
		}
		count=count+1;
	}

	return count;
}



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++)
	{
		monthSum[i];
		for(int j=1; j<=month; j++)
		{
			monthAverage[j] = monthSum[i] / month;
		}
	}
	//compares the values to find the max average temperature
	for(int i=0; i<=month; i++)
	{
		monthAverage[i];
		if(monthAverage[i] > max)
		{
			max = monthAverage[i];
		}
	}
	//returns max to the main() for output
	return max;
}

//unwritten function
int monthName()
{
	return 0;
}

Now you've set month to 11, which means that your arrays have only 11 elements; the last element of averageStore is averageStore[10], and so on.

The right way to do this is to set your upper bound (i.e. month) to the number of elements you want, so

const int month = 12;

and then to exclude equality from your loops, such as

for (int i = 0; i < month; ++i) { /* whatever */ }

Personally, I prefer to use != instead of <, because doing so lets me use the same style for forward iterators, which do not support <; but I realize that not everyone agrees with me.

By the way, I don't understand why you're evaluating some array elements and then throwing them asay, as you do on lines 124 and 133.

Hasn't he been dead over a whole year lol?

Rumors of my death are greatly exaggerated.

You're thinking of the Andrew Koenig who is the comedian who died last year, and who was the son of the original Star Trek actor Walter Koenig, who played Pavel Chekov.

No relation, so far as I know.

Thanks so much dude that did the trick :)

and as for lines 124 and 133 since im using two arrays there would i not need to have a separate for loop so they have there own count variables?

and as for lines 124 and 133 since im using two arrays there would i not need to have a separate for loop so they have there own count variables?

I'm sorry, but I don't understand the point you're trying to make.

You have written statements of the form

array[i];

Those statements don't actually do anything, so I don't understand why they are there.

oh they dont? didnt realize that in my mind they were counting through the array.

thanks for pointing that out dude

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.