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'

  • 5
    `_Bool compositePrimeNumbers[n];` is uninitialized for numbers which are not multiples of `initialPrimeIterator` (`i += initialPrimeIterator`). – KamilCuk Nov 15 '21 at 06: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. – Some programmer dude Nov 15 '21 at 06:48
  • 2
    A couple of other unrelated points: The `` header defines the type `bool`. And you should always check `argc` before using any element in `argv`. – Some programmer dude Nov 15 '21 at 06: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? – enhzflep Nov 15 '21 at 07:09
  • You might want to explain your first loop to a [rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging) or suitable substitute (roommate, friend, etc.). All it does is mark all odd indexes as `true`. – Some programmer dude Nov 15 '21 at 07:42
  • I have tried initializing all the booleans to false. It still resulted in non-deterministic output. Sorry for the line numbers. Yeah, I saw that stdbool.h has an alias for bool instead of _Bool, thanks. Not treasure hunt - used input "100" First loop is just the first case of Sieve of Erathosthenes - initially it will find all multiples of 2. Subsequently it will be further developed to continue marking the multiples of the next prime number which is found by the lowest-index 'false' boolean that is greater than the current prime number we are iterating on – Collin Quiason Rottinghaus Nov 15 '21 at 19:38
  • Could you add your updated code to the question? There must still be a problem with the initialization. – nielsen Nov 16 '21 at 14:19
  • I tried adding the initialization to false for the boolean array again and it seemed to solve the problem. I must have messed something up intitially – Collin Quiason Rottinghaus Nov 20 '21 at 23:40

2 Answers2

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.

nielsen
  • 5,641
  • 10
  • 27
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.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68