# why this is happening in for loop

• 08-14-2011
suryak
why this is happening in for loop
Actually, give an input.. at random .(no zero's)
the printf() which I highlighted is showing out of range array location..
why this is happening ..

Code:

```# include <stdio.h> int main() {   int n,i,j,flag;   int arr[3][3];     printf("Enter arr\n");   for(i=0;i<3;i++)   {     for(j=0;j<3;j++)     {        scanf("%d",&arr[i][j]);     }   }         for (i=0;i<3;i++)   {       for(j=0;j<3;j++)       {              flag = 0;           if (arr[i][j]==0)           {              printf("[%d,%d]",i,j);             break;           }           else if (arr[i][j]!=0)           {             flag = 1;             printf("<%d, %d>",i,j);           }       }   }         if (flag==1)         printf("/%d, %d/\n",i,j); }```
To be specific..
what I am looking for is that.. :
let us assume a number N and a array Arr[][]
now, if the array doesn't have the value N. it should print (say, not present)

if did this way.

Code:

```for (i=0;i<3;i++) {      for (j=0;j<3;j++)     {         flag = 0;         if ( arr[i][j] == N)               flag =1;     } } if (flag ==1) {   printf("not present");   printf("Last loc (%d %d)",i,j);  //its showing out of range .. ie., showing 3 instead of  //2 if no number is N }```
where i am going wrong
• 08-14-2011
```for (i=0, flag=0;i<3;i++) {      for (j=0;j<3;j++)     {         // not here flag = 0;         if ( arr[i][j] == N) {               flag =1;               break;         }     } } if (flag ==1) {   printf("N is present in the array");   printf("Last loc (%d %d)",i,j);  //OK now } else {   printf("N is not in the array\n"); }```