Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 847639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:48:59+00:00 2026-05-15T06:48:59+00:00

I have been trying to execute this program on my MinGW, through Code::Blocks: #include

  • 0

I have been trying to execute this program on my MinGW, through Code::Blocks:

#include <string.h>
#include <math.h>
#include <stdio.h>
#define N 100
int p[N];
int pr[N];
int cnt;

void sieve()
{
   int i,j;
   for(i=0;i<N;i++) pr[i]=1;
   pr[0]=pr[1]=0;

   for(i=2;i<N;i++)
      if(pr[i])
        {
         p[cnt]=i; cnt++;
         for(j=i+i;j<=N;j+=i) pr[j]=0;
        }

  }

int main(){
    sieve();
    int i;
    for(i=0;i<cnt;i++)
       printf("%d ",p[i]);
    puts("");
   printf("Total number of prime numbers : %d",cnt);
  return 0;
}

On my system the output is:

7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Total number of prime numbers : 22

Which is completely insane, since I am completely sure about the implementation of my algorithm.

So I decided to try it in Ideone where it gives correct output. Can anybody point out the reason ?

I changed it to N, but the output doesn’t change.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:49:00+00:00Added an answer on May 15, 2026 at 6:49 am

    There are two important bugs in your code.

    One is that your p array is too small, so you are writing off the end of it. This is undefined behaviour, though on the platforms you are using it overwrites the start of the pr array. This has no effect on the output, since the location you are overwriting is before the location you are testing in the sieve.

    The other is that you are also writing off the end of your pr array:

            for(j=i+i;j<=N;j+=i) pr[j]=0;
    

    This loops sets pr[N] to zero, which is off the end of pr. In MinGW this is where cnt is stored, so each time i divides N, cnt is set to zero. As N is 100, this happens for i==2 and i==5, so you lose the primes before five from your result. IdeOne seems to put cnt somewhere else in relation to pr, so it does not get overwritten. This is why you get different output with the different compilers.

    Change the size of the array p to N, or use only one array for both sieve and output, and change <= in line 18 to < so you don’t write off the end of it.

    int p[N];
    int *pr = p; // reuse the array
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to test this out. I put in the code below
This might sound silly but I have been trying to execute a NUnit test
Have been trying to encrypt an xml file to a string so that I
I have been trying to execute a button event in html page that displays
I have been trying to get the following code to work(everything is defined in
I have to execute my program with this line: java -DImport=data.txt -Din=input.dat -Dout=output1.dat main.Main
I have been trying to write some code that asks the user for several
I have been trying to find a solution to this for some time. I
I have been trying to get this to work for most of the day
I've been trying to parallelize this piece of code for about two days and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.