2016-02-09 102 views
0

我正在使用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; 
} 
+0

您可以将该代码缩减至当前大小的1/4并仅隔离相关问题吗?正如你所看到的是一个Wall O'Code,它为任何想要帮助的人提供了一个巨大的进入门槛,可能大部分与手边的问题无关。 (作为奖励,你甚至可能在过程中自己发现问题) –

+0

'for(int i = 0; i <= POINTS; i ++)'是错误的。 myArray [POINTS]超出界限。 – drescherjm

回答

2
double myArray[POINTS]; 

myArray是的20个双阵列 - myArray[0]通过myArray[19]

for (i = 0; i <= POINTS; i++) 
{ 
    myArray[i] = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x); 

这套myArray[0]myArray[20]。访问myArray[20]是不允许的,因为那是20元素数组的第21个元素。

请注意,编译器并不总是足够好,可以为您检测到这个问题。 Visual C++在这里通过导致程序崩溃而信任你。

+0

非常感谢 – Lucky

相关问题