Why I am getting a runtime error SIGSEGV in the problem? Where I am going wrong?
This is an error caused by an invalid memory reference or segmentation fault. The most common causes are accessing an array element out of bounds, or using too much memory.
Some things for you to try:
Make sure you aren’t using variables that haven’t been initialised. These may be set to 0 on your computer, but aren’t guaranteed to be on the judge.
Check every single occurrence of accessing an array element and see if it could possibly be out of bounds.
Make sure you aren’t declaring too much memory. 64 MB is guaranteed, but having an array of size [10000][10000] will never work.
Make sure you aren’t declaring too much stack memory. Any large arrays should be declared globally, outside of any functions - putting an array of 100000 ints inside a function probably won’t work.
//why i was getting SIGSEGV??
//for this question http://www.codechef.com/COQU2014/problems/DCQ1402
//please help n explain
for this solution http://www.codechef.com/viewsolution/5277974
because you are initializing array a of size 5 , whereas n can be very large , so you are trying to access invalid memory hence SIGSEGV
why di i get reutime error for given problem
#include<stdio.h>
#include<stdlib.h>
#define size 65535
int main()
{
long n,k,j,t[size],i=0,count=0;
scanf("%ld %ld",&n,&k);
while(i<n)
{
scanf("%ld",&t[i]);
printf("\n");
if(t[i]%k==0)
count++;
i++;
}
printf("%ld\n",count);
}
why do i get SIGSEVG
You have assumed that size of the array is 100 while it is given in the question that size can be up to 10^6. So in your for loop you will be accessing array out of bounds.
You are creating character array of length of 50 to store the given string whose length can be equal to 50. Each character array used to store string("%s" format specifier) automatically adds null character(’\0’) in the end. So you actually need length+1 memory to store.
can anyone help me…why i am getting SIGSEVG error in this http://ideone.com/PeQwg9
thanks in advance
can anyone help me…why i am getting SIGSEVG error in this http://ideone.com/PeQwg9
thanks in advance
#include<stdio.h>
main()
{
long long t=0,i=0,j=0,temp=0,a[10000];
scanf("%lld",&t);
for(i=0;i<t;i++)
scanf("%lld",&a[i]);
for(i=0;i<t-1;i++)
{
for(j=0;j<t-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<t;i++)
printf("%lld\n",a[i]);
}
How do u declare array of unknown size?.
so after taking the input, u have to declare .i.e int q[n].
below is my accepted solution with just change of one line in your code