Why I'm getting SIGSEGV error for this programme ?

http://www.codechef.com/viewsolution/6616570

I think you have misunderstood the problem statement. It says you have to repeatedly input an integer and print it if it’s not equal to 42. If the number is 42 , then stop taking input. See the following pseudo code.

int a
input a
while(a!=42)
     print a
     input a

First of all, your approach itself is wrong. I will start of by explaining your code. Here’s a small fragment of it

int i=1,n=2,a[10];
while(i<n)
{

	if(a[i-1]==42)
	{
		// Nothing
	}
	else
	{			n++;
	}
	scanf("%d",&a[i]);
	i++;
}

Now, when the program starts, i has the value 1, n has 2, and a[0] to a[9] have some garbage value.

Now the program goes into the while loop, and i is less than n, so it continues forward, and then the condition

if( a[i-1] == 42 )

a[ i - 1 ] means a[ 0 ] , and you check if a[ 0 ] is equal to 42. Now let me ask you this, what is the value stored in a[ 0 ] ? Probably don’t know do you. Well, it is uninitialized and will have some garbage value. There is no saying that it cannot have the value 42, so that will be upto your luck. That’s not good.

Now, I’ll explain what the program is expected to do.

You just to keep on accepting input until 42 is obtained and output them ( it doesn’t matter when you output it, you can output it right after you take input, but the output should be of the format as specified in the example of the problem page ). You don’t have to go through the trouble to taking all the input, then printing them all next, for such a simple program, you just have to take input, and if it is not 42, output it and continue. You can reuse the variable ( you can actually do this program with just 1 variable, you don’t even need an array ).

So, the simplest way to make this program is

int n;
scanf ( "%d" , &n );
while ( n != 42 )
  {
     printf ( "%d\n" , n );     // don't forget the newline
     scanf ( "%d" , &n );
  }

This is the simplest thing ( the simplest I could think of anyway )

Hope you now understand this.

Happy Coding…