Solving C Programming Problem: Finding Primes to 1000

  • Thread starter Thread starter kbaumen
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a C programming problem related to finding prime numbers up to 1000 using the sieve method. Participants explore issues with the provided code, suggest debugging techniques, and share modifications that lead to successful execution.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant describes their struggle with a C program intended to print prime numbers up to 1000, noting that it only produces a blank line.
  • Another participant suggests using additional printf statements to debug the code and recommends temporarily reducing the upper limit from 1000 to 30 for easier output management.
  • A participant reports success after changing a for loop to a while loop, although they express confusion about why the original code did not work.
  • Multiple participants identify a semicolon after the for statement as the source of the original issue, indicating it caused the loop to terminate prematurely.
  • There is a mention of using gdb (GNU Debugger) as a debugging tool, but this suggestion is met with skepticism regarding the necessity of a debugger for such a straightforward problem.

Areas of Agreement / Disagreement

Participants generally agree that the semicolon after the for statement was the main issue. However, there is no consensus on the necessity of using a debugger like gdb, with differing opinions on debugging approaches.

Contextual Notes

The discussion highlights the importance of careful attention to syntax in programming and the utility of debugging techniques, but it does not resolve all uncertainties regarding debugging methodologies.

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.
 

Similar threads

  • · Replies 28 ·
Replies
28
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 9 ·
Replies
9
Views
4K
Replies
7
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K