-1

Below is the code I wrote.

Please tell me why my code didn't work even after a long time after execution.

I did this under the condition of num = 4. I used the latest version of Xcode, M2 MacBook Air.

#include <stdio.h>

int main() {
    int num = 0;
    printf("Enter the number of prime numbers you want to find : ");
    scanf("%d", &num);
    int prime[num], store = 0, load = 0, dividend = 1, signal = 1, i;
    prime[0] = 2;
    while (store < num) {
        for (load = 0; load < store; load++) {
            if (dividend % prime[load] == 0) {
                signal = signal * 0;
            }
        }
        if (signal == 1) { // if 'dividend' value is a prime number, store dividend in the array of 'prime'.
           prime[store] = dividend;
           store++;
        }
        signal = 1;
        dividend++;
    }
    for (i = 0; i < num; i++) {
        printf("%d \n", prime[i]);
    }
    return 0;
}

/*
 signal == 1 : prime
 signal == 0 : not prime
 
 1. prime[load] == NALL
 2. for any value in the array 'prime', 'dividend % prime[load] != 0' //dividend is prime number
    signal * 0 
*/

Jest a second ago, I asked chatGPT for this problem. It told me "This method is outdated. you can use another way like Sieve of Eratosthenes.". But At least, if my code doesn't have problem, it must operate.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
23 3
  • 23
  • 3
  • 2
    This seems like a very good time to learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your programs. For example by using a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through your code line by line while monitoring variables and their values. – Some programmer dude Mar 09 '23 at 11:30
  • And please don't use bots like ChatGPT. All their answers are totally made up, it's basically guessing and puzzling together things from all over the Internet. While it might have mentioned a possibly better method to generate primes, it tend to have answers that *look* good but are otherwise unusable. Especially when it comes to anything factual and precise, like programming. There's a reason ChatGTP (or similar) answers have been forbidden here. – Some programmer dude Mar 09 '23 at 11:31
  • 5
    I love the 'outdated' when Eratosthenes lived 2 milliennia ago. – Weather Vane Mar 09 '23 at 11:38
  • I'm a non-English speaker, so I trusted the translator, but I failed. :) – 23 3 Mar 09 '23 at 12:50

3 Answers3

1

There are multiple problems:

  • store should be initialized to 1 as you set the initial value prime[0] = 2 and start with dividend = 3. You could start at 2 and remove the initialization of prime[0].
  • signal is not a good name for the prime indicator: composite is more explicit. Breaking from the loop as soon as a factor has been found is also recommended.
  • making load and composite local to the loop and initialized at the beginning would be less error prone.

Here is a modified version:

#include <stdio.h>

int main() {
    int num;

    printf("Enter the number of prime numbers you want to find : ");
    if (scanf("%d", &num) != 1 || num < 1)
        return 1;

    int prime[num];
    int store = 0;
    int dividend = 2;

    while (store < num) {
        int composite = 0;
        for (int load = 0; load < store; load++) {
            if (dividend % prime[load] == 0) {
                composite = 1;
                break;
            }
        }
        if (composite == 0) {
            // 'dividend' value is a prime number, store it in the array.
            prime[store] = dividend;
            store++;
        }
        dividend++;
    }
    for (int i = 0; i < num; i++) {
        printf("%d\n", prime[i]);
    }
    return 0;
}

Note that testing all prime factors is not necessary. The inner loop test could be changed to:

    for (int load = 0; dividend / prime[load] >= prime[load]; load++) {
        if (dividend % prime[load] == 0) {
            composite = 1;
            break;
        }
    }
chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

You should declare store = 1 because there is a number stored in the prime array. I think the reason that is causing an infinite loop is that by declaring dividend = 1 will cause 1 to enter in the prime array, and every number is divisible by 1, causing the algorithm to detect no more primes. You should declare dividend = 3 (omiting 2 because is already in the array).

#include <stdio.h>
int main() {
    int num = 0;
    printf("Enter the number of prime numbers you want to find : ");
    scanf("%d", &num);
    int prime[num], store = 1, load = 0, dividend = 3, signal = 1, i;
    prime[0] = 2;
    while (store < num) {
        for (load = 0; load < store; load++) {
            if (dividend % prime[load] == 0) {
                signal = 0;
            }
        }
        if (signal == 1) { // if 'dividend' value is a prime number, store dividend in the array of 'prime'.
           prime[store] = dividend;
           store++;
        }
        signal = 1;
        dividend++;
    }
    for (i = 0; i < num; i++) {
        printf("%d \n", prime[i]);
    }
    return 0;
}

One thing more: signal = signal * 0 is equal to signal = 0

xLui
  • 3
  • 2
0

Your code has some more than one issue. For example int prime[num] ; you should create dynamic memory allocation or make it int prime[constant value]. Better to try debug it :) Here is simple code about finding prime numbers examle.

programiz.com/c-programming/examples/prime-number

Ukarahan
  • 39
  • 7