0

This is an unfinished program I'm writing to learn C (only checks multiples of 2 currently...)

Ultimately, I want this to be an implementation of the Sieve of Eratosthenes (prime numbers)

The problem I'm having is that the output is non-deterministic: sometimes the output includes 11, other times it does not - This happens for a handful of numbers. I have experimented by changing a few things, such as actually initializing the array of booleans to false.

Any ideas why this might be happening?

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main(int argc, char *argv[]) {
        int n = atoi(argv[1]);
        int initialPrimeIterator = 2;
        _Bool compositePrimeNumbers[n];

        printf("Prime Numbers from 2 -> %d\n", n);
        for (int i = initialPrimeIterator; i < n; i += initialPrimeIterator) {
                compositePrimeNumbers[i-1] = true;
        }
        printf("Done...\n");
        
        printf("Printing prime numbers from 2-> %d\n", n);
        for (int i = 2; i < n; i++) {
                if (!compositePrimeNumbers[i]){
                        printf("%d\n", i + 1);
                }
        }
        
        return 0;
}

edit: haha. Just realized I have an array named 'compositePrime...' Should just be 'compositeNumbers'

8
  • 5
    _Bool compositePrimeNumbers[n]; is uninitialized for numbers which are not multiples of initialPrimeIterator (i += initialPrimeIterator). Commented Nov 15, 2021 at 6:46
  • 5
    Please don't include line-numbers in code. If you want to point out a specific line then mention it in the question itself and add a comment on that line. Commented Nov 15, 2021 at 6:48
  • 2
    A couple of other unrelated points: The <stdbool.h> header defines the type bool. And you should always check argc before using any element in argv. Commented Nov 15, 2021 at 6:50
  • 1
    Is this supposed to be a treasure-hunt, or do you think you could perhaps supply the inputs for which the outputs are a surprise? Commented Nov 15, 2021 at 7:09
  • You might want to explain your first loop to a rubber duck or suitable substitute (roommate, friend, etc.). All it does is mark all odd indexes as true. Commented Nov 15, 2021 at 7:42

2 Answers 2

3

Since I understand you are aiming at completing the program when overcoming this hurdle, I will not post a completed program, but only point out issues in your current version:

  1. As it has been noted, the array compositePrimeNumbers is uninitialized. Since it must be initialized with all values false which is represented by 0, the quickest way is this:
     memset(compositePrimeNumbers, 0, sizeof(compositePrimeNumbers));
  1. You should not mark the current initialPrimeIterator as a composite number, hence the for-loop should start with the next multiple. Also, n must be included:
    for (int i = 2 * initialPrimeIterator; i <= n; i += initialPrimeIterator) {

(actually, this can be optimized by replacing 2 * initialPrimeIterator with initialPrimeIterator * initialPrimeIterator).

With these changes, I believe you are well on the way to complete the program.

Sign up to request clarification or add additional context in comments.

Comments

2

In C, a local array is not initialized, probably for performance reasons.

One way to fix this is to loop over it to set each element to false.

2 Comments

I tried this and still got the same non-deterministic output
"probably for performance reasons": you can drop the word "probably".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.