I have written the code and logic but why my cod is not working

#include
#include<bits/stdc++.h>
using namespace std;

void prime(int p[10000000],int n){

for(int i=3;i<=5000000;i+=2){

 p[i] = 1;

}

for(int i=3;i<=5000000;i+=2){

 if(p[i]==1){

	 for(int j=1*i;i<=5000000;j+=i){

		 if(p[j]==1){
			 p[j] = 0;
		 }
	 }
 }

}

p[0] = p[1] = 0 ;

p[2] = 1;

int csum[5000000] = {0};

for(int i=1;i<=5000000;i++){

 csum[i] = csum[i-1] +p[i];

}

for(int i=1;i<=5000000;i++){

 if(csum[i]==n){

	 cout<<i<<endl;

	 break;
 }

}

return;

}

int main(){

int n;

cin>>n;

int arr[10000000] = {0};

prime(arr,n);

return 0;

}

Hi @manish99
refer this https://ide.codingblocks.com/s/637385

why below given code on compiling gives

/bin/run.sh: line 4: 18 Segmentation fault (core dumped) ./exe

ok so u are getting error bcoz u have created p array of size 1000005 but at other places u are trying to access till 10000005 of p array which is more than the size of p array declared…

still, I am getting the same error in the code below:

/bin/run.sh: line 4: 18 Segmentation fault (core dumped) ./exe

Hi, just declare this int p[10000005] = {0}; globally it will work fine… declaring such a big array inside sometimes stack memory overflows and it gives error… u can see my code…

I have reduced the array size to 100 ,but still showing same error in the below code:

/bin/run.sh: line 4: 18 Segmentation fault (core dumped) ./exe

could you please @ varun goyal give me some reason behind that so that no more question comes in my thought and i could move forward towards the course.

Thanks

Hi @manish99
ur code is working fine…


i dont see any segmentation error now…

i hope its clear now… shall i mark it as resolved??

Yes vaibhav you must mark it resolved

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Vaibhav , I have some questions which are difficult to ask in this mode.so how can I clear this doubt

the only option to ask to doubt is through here only…in whichever ques u are having doubt ask doubt in that… we will try our best that ur doubt gets resolved…