我正在使用Visual Studios 2015,并且遇到了问题。运行时检查失败#2 - 变量“myArray”周围的堆栈已损坏。我不确定程序中哪些地方会导致我的阵列出现某种腐败。但是当我做了涉及操作数组的计算时,几个数字变成0.0000而不是原来的数字。有人可以帮忙吗?阵列周围的堆栈已损坏
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double xmin, xmax;
const int POINTS = 20;
const double PI = 3.1416;
double increments;
double range, mean;
double total = 0;
double myArray[POINTS];
double number = myArray[0];
double mode = number;
int count = 1;
int countMode = 1;
cout << "Enter in a value for the minimum x value: ";
cin >> xmin;
cout << "Enter in a value for the maximum x value: ";
cin >> xmax;
if (xmin < 0)
increments = (abs(xmin) + xmax)/POINTS;
else
increments = (xmax - xmin)/POINTS;
int i = 0;
double x = xmin + increments * i;
double minimum = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
double maximum = myArray[19];
cout << setw(15) << "x |" << setw(15) << "f(x)" << endl;
cout << setw(32) << setfill('-') << " " << endl;
cout << setfill(' ');
for (i = 0; i <= POINTS; i++)
{
myArray[i] = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
x = xmin + increments * i;
cout << fixed << showpos << setw(15) << setprecision(2) << x << setw(15) << setprecision(4) << myArray[i] << endl;
if (myArray[i] <= minimum)
minimum = myArray[i];
if (myArray[i] > maximum)
maximum = myArray[i];
}
cout << endl;
range = maximum - minimum;
for (int count = 0; count <= POINTS; count++)
{
total += myArray[count];
}
mean = total/POINTS;
int temp;
bool swap;
do
{
swap = false;
for (int i = 0; i < POINTS - 1; i++)
{
if (myArray[i] > myArray[i + 1])
{
temp = myArray[i];
myArray[i] = myArray[i + 1];
myArray[i + 1] = temp;
swap = true;
}
}
} while (swap);
for (int i = 0; i <= POINTS; i++)
{
if (myArray[i] == number)
{
count++;
}
else
{
if (count > countMode)
{
countMode = count;
mode = number;
}
count = 1;
number = myArray[i];
}
}
cout << "The maximum value is: " << maximum << endl;
cout << "The minimum value is: " << minimum << endl;
cout << "The range is: " << range << endl;
cout << "The mean value is: " << mean << endl;
cout << "The median value is: " << (myArray[9] + myArray[10])/2 << endl;
cout << "The mode value is: " << mode << endl;
for (i = 0; i <= POINTS; i++)
cout << myArray[i] << endl;
system("pause");
return 0;
}
您可以将该代码缩减至当前大小的1/4并仅隔离相关问题吗?正如你所看到的是一个Wall O'Code,它为任何想要帮助的人提供了一个巨大的进入门槛,可能大部分与手边的问题无关。 (作为奖励,你甚至可能在过程中自己发现问题) –
'for(int i = 0; i <= POINTS; i ++)'是错误的。 myArray [POINTS]超出界限。 – drescherjm