Solving C Programming Problem: Finding Primes to 1000

  • Thread starter Thread starter kbaumen
  • Start date Start date
AI Thread Summary
The discussion revolves around troubleshooting a C program intended to print all prime numbers up to 1000 using the sieve method. The original code resulted in a blank output, prompting the user to seek help. Key issues identified included a misplaced semicolon after a for loop, which caused the loop to terminate prematurely, preventing the intended execution of the print statement. Suggestions included adding printf statements for debugging and temporarily reducing the range of numbers to simplify testing. After modifying the loop from a for loop to a while loop and correcting the semicolon issue, the code worked as intended. The conversation also touched on the use of GDB (GNU Debugger) for debugging, though some participants emphasized the importance of resolving such issues independently before resorting to debugging tools.
kbaumen
Messages
190
Reaction score
0
I do encounter a lot of problems while learning C. For example the code below, I can't understand why doesn't it work. It's meant to print out all primes to 1000 by using the sieve method. However, I only get a blank line. And I've spent about two hours making small modifications and repeatedly rereading the code with no result. Can anyone tell me why doesn't it work?

Here's the code:
Code:
#include <stdio.h>
#include <stdlib.h>

#define PRIME 1
#define NONPRIME 0

int numbers[1000];

void mark_multiples(int num)
{
    int a, j;  
    a = 0;
    j = 2;  

    while (a < 1000)
    {
        a = num * j;

        if (a >= 1000)
        {
            break;
        }

        numbers[a] = NONPRIME;
        j++;
    }
}

int get_next_prime(int num)
{
    int answer;

    answer = num + 1;

    while (numbers[answer] == NONPRIME)
    {
        answer++;
        if (answer == 1000)
        {
            break;
        }
    }

    return answer;
}

int main(void)
{
    int i, next_prime;
    
    numbers[0] = NONPRIME;
    numbers[1] = NONPRIME;

    for (i = 2; i < 1000; i++)
    {
        numbers[i] = PRIME;
    }

    next_prime = 2;    
    
    do
    {
        mark_multiples(next_prime);
        next_prime = get_next_prime(next_prime);
    }
    while(next_prime < 1000);

    for (i = 1; i < 1000; i++);
    {

        if (numbers[i - 1] == PRIME)
        {
            printf("%i ", i);
        }
    }
    
    printf("\n");

    return 0;
}

Thank you in advance.
 
Last edited:
Technology news on Phys.org
Have you tried putting extra printf statements at key points in the code, to see what is actually happening to your variables? That often gives you clues as to what's wrong, faster than by just staring at the code. When doing this, it would probably help to reduce the upper limit temporarily from 1000 to perhaps 30, so the resulting output doesn't overwhelm you.

I haven't looked at your code in detail myself... I'm just offering a general suggestion.
 
Well, thanks for the suggestions. I tried reducing the 1000 to 100. But that idea of more printf's sounds quite good

...

That worked. I changed the last loop from FOR to WHILE like this:

Code:
#include <stdio.h>
#include <stdlib.h>

#define PRIME 1
#define NONPRIME 0

int numbers[1000];

void mark_multiples(int num)
{
    int a, j;  
    a = 0;
    j = 2;  

    while (a < 1000)
    {
        a = num * j;

        if (a >= 1000)
        {
            break;
        }

        numbers[a] = NONPRIME;
        j++;
    }
}

int get_next_prime(int num)
{
    int answer;

    answer = num + 1;

    while (numbers[answer] == NONPRIME)
    {
        answer++;
        if (answer == 1000)
        {
            break;
        }
    }
    return answer;
}

int main(void)
{
    int i, next_prime;
    
    numbers[0] = NONPRIME;
    numbers[1] = NONPRIME;

    for (i = 2; i < 1000; i++)
    {
        numbers[i] = PRIME;
    }

    next_prime = 2;    
    
    do
    {
        mark_multiples(next_prime);
        next_prime = get_next_prime(next_prime);
    }
    while(next_prime < 1000);

    i = 0;
    
    while (i < 1000)
    {
        if (numbers[i] == PRIME)
        {
            printf("%i  ", i);
        }
        
        i++;
    }
    
    printf("\n");

    return 0;
}

Now it works perfectly. Thanks for the help.

However, I still have no idea why the previous one didn't work. All I changed is FOR to WHILE in main routine.
 
You have a semi-colon after the for statement.
 
t!m said:
You have a semi-colon after the for statement.

Thanks. That really was the problem. Got to be more careful.
 
gdb?
 
Texpie said:
gdb?

I'm sorry, what do you mean by that? What's gdb?
 
kbaumen said:
I'm sorry, what do you mean by that? What's gdb?

GDB stands for GNU Debugger.

But I think it is not a good sugestion: before using any debugger, you should be able to correct a piece of code like this by yourself - alone.
 
Back
Top