newbie here:
the following program to generate all prime numbers under 100 using the "Sieve of Eratosthenes algorithm" works fine, but crashes after displaying the CORRECT output!
使用“Eratosthenes算法筛”生成100以下所有素数的以下程序工作正常,但在显示CORRECT输出后崩溃!
the error in windows: primenumber.exe has stopped working!
Windows中的错误:primenumber.exe已停止工作!
#include<stdio.h>
int main()
{
int P[100] = {0}, i, j;
for(i = 2; i < 100; ++i)
{
if(P[i] == 0)
printf("%d\n", i);
for(j = 1; i * j <= 100; ++j)
P[i * j] = 1;
}
return 0;
}
2 个解决方案
#1
3
i*j<=100
array index out of bound -- UB...(Undefined Behavior)
i * j <= 100数组索引超出范围 - UB ...(未定义行为)
It should be i*j<100
.
它应该是i * j <100。
#2
2
there is an array overflow occurring here
这里发生了数组溢出
i*j<=100 should be i*j<100
because range of your array is 0-99
因为你的数组范围是0-99
#1
3
i*j<=100
array index out of bound -- UB...(Undefined Behavior)
i * j <= 100数组索引超出范围 - UB ...(未定义行为)
It should be i*j<100
.
它应该是i * j <100。
#2
2
there is an array overflow occurring here
这里发生了数组溢出
i*j<=100 should be i*j<100
because range of your array is 0-99
因为你的数组范围是0-99