#include <iostream>
using namespace std;
int main()
{
int size = 0;
int* myArray = new int [size + 1];
cout << "Enter the exponent of the first term: ";
cin >> size;
cout << endl;
for (int i = size; i >= 0; --i)
{
cout << "Enter the coefficient of the term with exponent "
<< i << ": ";
cin >> myArray[i];
}
for (int i = size; i >= 0; --i)
{
cout << i << endl;
}
return 0;
}
为什么我在输入大于2时出现断言错误?这是多项式程序的前兆,其中阵列的下标是每项的幂,阵列[下标]处的元素是系数。为什么我得到断言错误?
代码试图去做什么?你将myArray初始化为只有1个int的数组,然后读取大小,并尝试写入数组中的任意位置? – 2009-12-10 06:10:29
谢谢大家!最初我刚刚收到一个编译错误,并将初始化的大小设置为0,但没有考虑清除错误。但是这造成了运行时错误。我通过不初始化大小并在输入大小后分配myArray来修复它。再次感谢你们! – Brandon 2009-12-10 06:15:16
@Brandon:还有一件事,你的for循环是从'int i = size;'开始的,这将是一个无效索引。它应该从'size-1'开始 – Naveen 2009-12-10 06:17:14