Here is my code:when my input for input for array is 10001 .then also it is entering in else block for input[1]=0,where as i have put condition in outer if that if(input[j]==1).can any body tell me why it is happening?
#include<stdio.h>
int main()
{
unsigned int tcase=0,build=0,i=0,j=0,k=0,count=0;
unsigned int input[1000];
scanf("%d",&tcase);
while(tcase--)
{
scanf("%d",&build);
for(i=0;i<build;i++)
scanf("%d",&input[i]);
for(j=0;j<build;j++)
{
if(input[j]==1)
{
if(j==0)
{ input[j+1]=1;
printf("fddf");
}
else if(j==(build-1))
{
input[j-1]=1;
printf("Gfgf");
}
else
{
input[j+1]=1;
input[j-1]=1;
printf("awGfgf");
}
}
}
for(k=0;k<build;k++)
{
if(input[k]==0)
++count;
}
printf("%d\n",count);
}
return 0;
}
I will answer your question based your comment to Eric J’s answer:
The gist of the answer is that the first input you are entering as 1 which causes the condition to succeed first time. Later on in each iteration, you keep changing the values of your input array which causes subsequent iterations to enter into the condition.
As you mentioned your input is
Now, see the behavior in the below code: