Packiging Cupcakes (MUFFINS3) Runtime error?

Why runtime error is coming in this code. Its working properly on my PC.
#include< iostream>
using namespace std;

int main()

{

int c,i,r[100],t,h,big,j;

cin>>t;

for(j=0; j< t;j++)

{
cin>>c;

  h=0;

 for(i=0;i<100;i++)
  {
   r[i]=1;
  }

 
 for(i=1;i<=c;i++)
 {
  if(c%i>0)
  {
	 r[h]=i;
	 h++;		 
  }

 }


 big=c%r[0];



  for (i=0; i<h; i++) 
{
    if(c%r[i]>=big) 
    {
        big=r[i];
    }
}  

   if(big==0)
     cout<<"1\n";
   else
  cout<<big<<"\n";	

}

 return 0;

}

1 Like

I din’t look into your approach. However this error occurs when you use an undeclared memory. You are writing r[h++]=i. And the size of r declared is 100. What if h becomes more than 100 in any test case. Then this memory will become undeclared. So you are getting SIGSEGV.

1 Like

I found that I was doing a very silly thing. This is what i have coded now. Its working.
#include

using namespace std;

int main()
{

int t,i,a;
	cin>>t;
    if(t>=1&&t<=1000)
	{
		
	for(i=0;i<t;i++)
	{
		cin>>a;
		if(a>=2&&a<=100000000)
			cout<<(a/2)+1<<"\n";
	}
	}

 
 return 0;

}

1 Like

That why i din’t look into your code properly! :stuck_out_tongue: I knew this straightforward formula. I thought you might be trying something different.