Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

编程练习5.2,有数组下标越界问题。 #9

Open
linfzl opened this issue Mar 30, 2024 · 0 comments
Open

编程练习5.2,有数组下标越界问题。 #9

linfzl opened this issue Mar 30, 2024 · 0 comments

Comments

@linfzl
Copy link

linfzl commented Mar 30, 2024

for 循环中 i 最大值为 100,而数组 factorials 大小就是 100 ,从 0 开始算,最大是 99,找不到factorials[100];
ar_size改为101就好了,原书的程序清单5.4也是,size是16,最后输出15的阶乘。

#include
using namespace std;
const int ArSize = 16;

int main() {
long long factorials[ArSize];
factorials[1] = factorials[0] = 1LL;
for (int i = 2; i < ArSize; i++)
{
factorials[i] = i * factorials[i - 1];
}
for (int i = 0; i < ArSize; i++)
{
cout << i << "! = " << factorials[i] << "\n";
}
return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant