Hi,

My assignment for class was to produce a program that runs the seive of eratosthenes to determine all the prime numbers between 2 and n, with n no larger than 100. I tryed to write the program with a boolian array that will turn all non-primes to false up to the max of 100, and then only output to the requested input of n.

Quite honestly, this confuses me and I am not even sure I have the process right, let alone the program. As is, I am getting an unresolved external error upon linking. Line 38 is what I am most unsure of, and for some reason think my error is there.

Any advice or help on getting this thing running would be much appriciated. How am I doing?

Thanks.--Will

Dani AI

Generated

began with the right idea (a boolean sieve up to 100) and later uploaded a revised attempt, while correctly spotted an off‑by‑one when printing primes (adding 1 to the index). The short, practical points below show a simple, standards‑compliant C implementation (C99) and a checklist for the reported "unresolved external" link error.

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

#define MAX_N 100

int main(void) {
    int n;
    bool is_prime[MAX_N + 1];
    for (int i = 0; i <= MAX_N; ++i) is_prime[i] = true;
    is_prime[0] = is_prime[1] = false;

    if (scanf("%d", &n) != 1) return 1;
    if (n > MAX_N) n = MAX_N;

    for (int p = 2; p * p <= n; ++p) {
        if (is_prime[p]) {
            for (int m = p * p; m <= n; m += p) is_prime[m] = false;
        }
    }

    for (int i = 2; i <= n; ++i)
        if (is_prime[i]) printf("%d\n", i);

    return 0;
}

Checklist and clarifications:

  • Array size must allow index n (use MAX_N + 1 for 0..100). Initialize is_prime[0] and is_prime[1] to false.
  • Mark multiples starting at p * p to avoid redundant work. Use p * p <= n instead of sqrt(n) (this avoids needing the math library).
  • The printing bug noted by is an off‑by‑one: print the index (i) itself, not i + 1.
  • "Unresolved external" is a linker message that names the missing symbol. Common causes: a missing main/function definition, calling a function from a library that hasn't been linked (e.g., sqrt without -lm), or mixing object files compiled with incompatible language/linkage. The exact linker text identifies which symbol is missing and points to the appropriate fix.
  • For compilers that lack <stdbool.h>, replace bool with int (0/1) or compile as C99.

Recommended Answers

All 2 Replies

OK, I got a bit further and think I have the basic idea down, however, I am still getting the unresolved externals message. Attatched is my improved code. Thanks. Will

Hi,

My assignment for class was to produce a program that runs the seive of eratosthenes to determine all the prime numbers between 2 and n, with n no larger than 100. I tryed to write the program with a boolian array that will turn all non-primes to false up to the max of 100, and then only output to the requested input of n.

Quite honestly, this confuses me and I am not even sure I have the process right, let alone the program. As is, I am getting an unresolved external error upon linking. Line 38 is what I am most unsure of, and for some reason think my error is there.

Any advice or help on getting this thing running would be much appriciated. How am I doing?

Thanks.--Will

Member Avatar for Member #27895

What errors you are getting? It compiles and runs just fine for me except every prime is 1 larger than it should be because you add 1 to m when printing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.