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

Ask A Question

Stats

  • Questions 516k
  • Answers 516k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer My go-to for testing a value against multiple regexes at… May 16, 2026 at 6:50 pm
  • Editorial Team
    Editorial Team added an answer Well, that was easy! I set the UITabBarControllerDelegate and then… May 16, 2026 at 6:50 pm
  • Editorial Team
    Editorial Team added an answer Look at the Version.Minor property. It is 0 for Vista,… May 16, 2026 at 6:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

This might sound silly but I have been trying to execute a NUnit test
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 register 3 hotkeys. I followed this example (or this
I have been trying to define multiple combo boxes in R using the tcltk
I just started working on Python and have been trying to run an outside
I have been trying to research how solr works when documents like doc or
I have been trying to get the unique values in each column of a
I've been trying to get started with unit-testing while working on a little cli
I'm trying to copy a used repository by using svnsync (I'm doing it this
I am trying to do something similar to what the person in this question

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.