I've written some code for a Uni assignment, and it worked perfectly. Basically the task was to declare a global array, then write 4 functions:

1) enter 10 values and store them in an array
2) display the populated array
3) find the min & max values in array and display them
4) accept a value from the keyboard and search for it in the array.

then call them from main().

As I said, what I wrote compiles, builds & runs perfectly, with no errors, and the code is below.

The next part of the assignment was to modify the above code so that the array is local to main(), then pass the array to each of the functions. I did this, and again it compiles and builds ok, with no errors, but it comes up with a runtime error "Run-Time Check Failure #2 - Stack around the variable 'myArray' was corrupted". However the program seems to actually run and terminate just fine if I click "Abort" to the error.

The error comes up immediately after I press return after entering a value to search for in the searchArray() function, so I presume the error lies in that function (although like I said, the search is performed and displayed just fine if I click "Abort" to the error message).

I tried moving the #define statement from inside main() to the top of the code to make it global, but that produces the same error.

First here is my code (working 100%) that has the array as global:

Code:
#include <iostream>
#include <iomanip>
using namespace std;

#define SIZE 9

int myArray[SIZE];

void takeValues()
{
	int a;

	for(a=0; a<=SIZE; a++)
	{
		cout << "Enter value " << a + 1 << ": ";
		cin >> myArray[a];
	}
}

void displayValues()
{
	int b;

	cout << endl << "Element" << setw(8) << "Value" << endl;

	for(b=0; b<=SIZE; b++)
	{
		cout << b << setw(12) << myArray[b] << endl;
	}
}

void findMinMax()
{
	int c, min=myArray[0], max=myArray[0];

	for(c=1; c<=SIZE; c++)
	{
		if(myArray[c] > max) max = myArray[c];
		else
			if(myArray[c] < min) min = myArray[c];
	}
	
	cout << endl << "Min is " << min << endl;
	cout << "Max is " << max << endl << endl;
}

void searchArray()
{
	int d, e, f;
	bool found = false;

	cout << "Enter a value to search for: ";
	cin >> d;

	{
		for(e=0; e<=SIZE; e++)
		{
			if(myArray[e] == d)
			{
				f = d;
				found = true;
				cout << "I found " << f << " at position " << e << " of the array." << endl <<endl;
				break;
			}
		}
	}

	if(!found) cout << d << " was not found in the array." << endl;
}
		

int main()
{
	takeValues();
	displayValues();
	findMinMax();
	searchArray();
}
Secondly, here is my code that has the array local to main() and produces the runtime error:

Code:
#include <iostream>
#include <iomanip>
using namespace std;

void takeValues(int myArray[], const int n)
{
	int a;

	for(a=0; a<=n; a++)
	{
		cout << "Enter value " << a + 1 << ": ";
		cin >> myArray[a];
	}
}

void displayValues(int myArray[], const int n)
{
	int b;

	cout << endl << "Element" << setw(8) << "Value" << endl;

	for(b=0; b<=n; b++)
	{
		cout << b << setw(12) << myArray[b] << endl;
	}
}

void findMinMax(int myArray[], const int n)
{
	int c, min=myArray[0], max=myArray[0];

	for(c=1; c<=n; c++)
	{
		if(myArray[c] > max) max = myArray[c];
		else
			if(myArray[c] < min) min = myArray[c];
	}
	
	cout << endl << "Min is " << min << endl;
	cout << "Max is " << max << endl << endl;
}

void searchArray(int myArray[], const int n)
{
	int d, e, f;
	bool found = false;

	cout << "Enter a value to search for: ";
	cin >> d;

	{
		for(e=0; e<=n; e++)
		{
			if(myArray[e] == d)
			{
				f = d;
				found = true;
				cout << "I found " << f << " at position " << e << " of the array." << endl <<endl;
				break;
			}
		}
	}

	if(!found) cout << d << " was not found in the array." << endl;
}
		

int main()
{
	#define SIZE 9
	int myArray[SIZE];

	takeValues(myArray, SIZE);
	displayValues(myArray, SIZE);
	findMinMax(myArray, SIZE);
	searchArray(myArray, SIZE);
}
From doing some research on the net I'm guessing the array is going out of bounds somewhere, but I can't figure out where.

Many thanks.