hey…i am getting Run time error…plzz…help…though using Sieve
my code is
#include
#include<math.h>
using namespace std;
int main()
{
int a,b,c,i,j,k,d,u;
float g;
cin>>d;
for(k=0;k<d;k++)
{
cin>>u;
cin>>a;
g=a;
bool m[a];
for(i=2;i<=a;i++)
{
m[i]=true;
}float z;int q;
z=sqrt(g);
for(i=2;i<=z;i++)
{
if(m[i]==true)
{
for(j=0;;j++)
{
q=(ii)+(ji);
if(q<=a)
m[q]=false;
else
break;
}
}
}
for(i=2;i<=a;i++)
{
if((m[i]==true)&&(i>=u))
cout<<i<<"\n";
}
cout<<"\n";
}
return 0;
}
@wonder : Are you sure that this is the complete code . It doesn’t even compile . For example there is no variable declaration of ii and ji .
Runtime error could be due to the fact that you are declaring m[a] and running a loop till <= a . Since arrays are indexed starting from zero , this could lead to going out of bounds .
Hope that helps . If it doesn’t then please post a link to your submission or give your complete code.
2 Likes
My opinion is that it fails because of big memory allocation, see here - qO0lB0 - Online C++ Compiler & Debugging Tool - Ideone.com test case