Thread: How to get index of structure elements?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    45

    How to get index of structure elements?

    Hi
    I want to get the starting index of structure elements, whoz id are 0,1,2,3
    Like in below code
    col_data[0] (starting id=0)
    col_data[3] (starting id=1)
    col_data[5] (starting id=2)
    col_data[8] (starting id=3)

    Code:
    
    
    Code:
    typedef struct gg{ int id; int value; } gg; gg col_data[10]={{0,44},{0,46},{0,39},{1,25},{1,10},{2,46},{2,78},{2,55},{3,64},{3,79}}; int ptr[4]; for (i = 0; i < 4; i++){ for (j = 0; j < 10; j++){ if (col_data[j].id==i){ ptr[i]=j; // If i use break it will break after first match and will not go again to the loop. break; } } }

    output should be
    ptr[0]=0
    ptr[1]=3
    ptr[2]=5
    ptr[3]=8

    How can i skip remaining loop iterations when it get that index and will go back to loop again for getting next element index?
    Last edited by gevni; 04-09-2013 at 03:59 AM.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    117
    Code:
    for (i =0, j = 0; i < 4 && j < 10; ++j)
    {
        if (col_data[j].id == i)
        {
            ptr[i++] = j;
        }
    }

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    45
    It won't work.. continue looping and the result was wrong too


    Quote Originally Posted by DRK View Post
    Code:
    for (i =0, j = 0; i < 4 && j < 10; ++j)
    {
        if (col_data[j].id == i)
        {
            ptr[i++] = j;
        }
    }

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    45
    I think it is because i was trying this with 0,2,1,3
    Code:
    // What is the efficient way of doing this in this case?
    
    gg col_data[10]={{0,44},{0,46},{0,39},{2,25},{2,10},{1,46},{1,78},{1,55},{3,64},{3,79}};

  5. #5
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    Quote Originally Posted by gevni View Post
    I think it is because i was trying this with 0,2,1,3
    Sorting col_data by id would be cool.
    Otherwise, searching for indexes in a meshed up col_data will lead to two nested loops.

    //Edit

    Thinking again, would you mind giving us more info about the order of elements ?
    Are ids distinguished and contiguous ? If so, then loop once over the table and store each unique reference to a helper table.
    Last edited by ch4; 04-09-2013 at 07:31 AM.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    117
    Quote Originally Posted by gevni View Post
    It won't work.. continue looping and the result was wrong too
    No, it won't continue. i < 4 condition terminates loop when all 4 indexes are found.

    Here's a new solution for you:
    Code:
    for (i = 0; i < 4; ++i)
    {
        ptr[i] = -1; // index not yet found
    }
    
    for (i = 0, j = 0; i < 4 && j < 10; ++j)
    {
        int id = col_data[j].id;
        if (id < 4 && ptr[id] == -1)
        {
            ptr[id] = j;
            ++i;
        }
    }

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    45
    Thanks for your help. It work fine if col_data[10] is in below order

    Code:
    gg col_data[10]={{0,44},{0,46},{0,39},{1,25},{1,10},{2,46},{2,78},{2,55},{3,64},{3,79}};
    I am sorry i provide the data into wrong order it should be like that

    Code:
    gg col_data[10]={{3,44},{3,46},{2,39},{2,25},{2,10},{1,46},{1,78},{0,55},{0,64},{0,79}};
    In that case if i run the above code it shows me output as
    Code:
    ptr[0]:7
    ptr[1]:0
    ptr[2]:0
    ptr[3]:0
    As it find the first element in the last of array. How can i copup with it?

    Quote Originally Posted by DRK View Post
    No, it won't continue. i < 4 condition terminates loop when all 4 indexes are found.

    Here's a new solution for you:
    Code:
    for (i = 0; i < 4; ++i)
    {
        ptr[i] = -1; // index not yet found
    }
    
    for (i = 0, j = 0; i < 4 && j < 10; ++j)
    {
        int id = col_data[j].id;
        if (id < 4 && ptr[id] == -1)
        {
            ptr[id] = j;
            ++i;
        }
    }
    Last edited by gevni; 04-09-2013 at 12:41 PM.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    45
    Thank you for your help.. It works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. structure with array elements
    By l_l in forum C Programming
    Replies: 7
    Last Post: 09-07-2012, 07:55 PM
  2. Structure Passing, Index Problem
    By ChipS in forum C Programming
    Replies: 11
    Last Post: 10-16-2011, 12:47 AM
  3. Random index structure based file managment in C
    By infantheartlyje in forum C Programming
    Replies: 1
    Last Post: 10-07-2011, 01:45 AM
  4. accessing user entered array elements by index?
    By richdb in forum C Programming
    Replies: 10
    Last Post: 04-08-2006, 11:10 AM
  5. How to compare structure elements
    By khpuce in forum C Programming
    Replies: 6
    Last Post: 04-10-2005, 11:40 AM
Website Security Test