【发布时间】:2016-03-10 01:56:26
【问题描述】:
我编写了一个程序来接受一个数组中的 15 个整数值,然后将该数组传递给一个函数,该函数将每个偶数索引值乘以 4。
当前程序显示初始数组,但似乎在显示修改后的数组之前就挂了。
请帮助我了解为什么程序会卡在这里!
int main(){
const int SIZE = 15;
int quad[SIZE] = {};
void quadruple(int[], const int);
cout << "Enter 15 integer values into an array." << endl;
for (int i = 0; i < SIZE; i++) // Accept 15 int values
{
cout << i << ": ";
cin >> quad[i];
}
cout << "Before quadruple function is called: " << endl;
for (int i = 0; i < SIZE; i++)
{
cout << quad[i] << " ";
}
cout << endl;
quadruple(quad, SIZE);
cout << "After even index value multiplication: " << endl;
for (int i = 0; i < SIZE; i++)
{
cout << quad[i] << " ";
}
cout << endl;
return 0;
}
void quadruple(int values[], const int SZ){
for (int i = 0; i < SZ; i + 2) // Multiply even values by 4
{
if ((i % 2) == 0)
{
values[i] = values[i] * 4;
}
else // Keep odd values the same
{
values[i] = values[i] * 1;
}
}
}
【问题讨论】:
-
i + 2橡皮鸭会说什么? -
有人会要求您使用调试器并进行橡皮鸭调试。为什么不是我。
标签: c++ arrays function for-loop