Thread: why this is happening in for loop

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    113

    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

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    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");
    }
    Where i am going wrong?
    You were letting the for loops go all the way through the array, instead of breaking out when the number was found. Note that the logic here will only find the first instance of the N number, not all of them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How come this is happening?
    By Angermanaged in forum C++ Programming
    Replies: 3
    Last Post: 03-21-2009, 07:35 PM
  2. What is happening here?
    By kermit in forum C Programming
    Replies: 4
    Last Post: 02-01-2009, 11:59 AM
  3. why is this happening?!
    By MK27 in forum C Programming
    Replies: 3
    Last Post: 01-13-2009, 12:40 PM
  4. What is happening here PLZ help me
    By jayee_spicyguy in forum C Programming
    Replies: 1
    Last Post: 11-15-2008, 06:47 AM
  5. Why is this happening?
    By caduardo21 in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2005, 01:09 AM

Tags for this Thread