Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

c - 当我在C中运行循环时,程序停止响应(program stops responding when I run for loop in C)

Logic: I am trying to loop from 3 to 100 and put all prime numbers in that range in an array.

(逻辑:我试图从3到100循环,并将该范围内的所有素数放入数组中。)

I do this by first putting 2 manually into the array.

(我首先将2手动放入数组中。)

After that, I loop over all the numbers and if a number is not divisible by all the prime numbers I have added to the array, it is a prime.

(之后,我遍历所有数字,如果一个数字不能被我添加到数组中的所有质数所除,则它是质数。)

The logic itself is not really valid in my opinion but this is a homework problem and I need to do it the way the professor wants it to be done.

(在我看来,逻辑本身并不是真的有效,但这是一个作业问题,我需要按照教授希望的方式进行。)

So I try to loop and it gets to a point and the program just crashes.

(因此,我尝试循环执行,直至达到临界点,程序便崩溃了。)

What am I doing wrong here?

(我在这里做错了什么?)

    int primeNums_lessThan100[25] = {2}; //25 is the size here because there are only 25 prime numbers under 100.

    int primeNums_lessThan100_length = 1;
    for(int counter = 3 ; counter < 100 ; counter++)
    {
        printf("Entered here!
");

        for(int array_idx = 0;  array_idx < primeNums_lessThan100_length ; array_idx++)
        {
            if(counter % primeNums_lessThan100[array_idx] != 0)
            {
                printf("Entered here, TOO!
");
                primeNums_lessThan100[array_idx+1] = counter;
                primeNums_lessThan100_length++;
            }
            else
            {
                continue;
            }
        }
    }
  ask by new_learner translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have a basic error in your logic.

(您的逻辑中有一个基本错误。)

It's not enough to find out the counter value is coprime with some previously found prime to determine counter is prime itself.

(这是不够的,找出counter值是互质与一些以前发现的黄金,以确定counter是黄金本身。)

You need to test it is coprime with all primes found.

(您需要测试它是否与找到的所有素数都互质。)

Suppose counter == 9 and you test it against the first item of your 'prime' numbers, which is 2 .

(假设counter == 9 ,然后针对“素数”的第一项2 。)

The result of 9 % 2 is of course 1 , which is not equal zero and your program adds 9 to the array as a prime number.

(9 % 2的结果当然是1 ,它不等于零,并且您的程序9作为质数加到数组中。)

It happens even earlier for counter == 4 — first you find out it is divisible by 2 and reiterate the loop to find out in the next step 4 % 3 != 0 and add 4 to 'primes'.

(对于counter == 4它甚至更早发生了-首先您发现它可以被2整除,然后重复循环以在下一步中找到4 % 3 != 0 并将4加到'primes'。)

As a further result the array overflows (you put more numbers into it than you expected and declared; actually, you put every natural number to it), which means you eventually write past the end of the array, thus triggering an Undefined Behavior.

(进一步的结果是数组溢出(您在其中放入了比预期和声明更多的数字;实际上,您向其添加了每个自然数),这意味着您最终将写入数组的末尾,从而触发了未定义行为。)

That displays also a basic error in the design: you do not check your array_idx against the array size, which might allow you to handle the error in some civilized manner.

(这也显示了设计中的一个基本错误:您不对照数组大小检查array_idx ,这可能允许您以某种文明的方式处理错误。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...