Concept: Adding and updating elements to a single array of structures

This is a discussion on Concept: Adding and updating elements to a single array of structures within the C Programming forums, part of the General Programming Boards category; A conceptually problem I am having in doing this problem... I have an array of structures like so: Code: [empty][empty][empty][...][empty] ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    84

    Post Concept: Adding and updating elements to a single array of structures

    A conceptually problem I am having in doing this problem...

    I have an array of structures like so:
    Code:
    [empty][empty][empty][...][empty]
    Initially all spots are empty. I read an line and take from it an ID and an element. The ID is what will serve as the distinguishing agent among these spots in the array of structures.

    Now, for the first read, I take the ID and element and assign it to the first spot.

    Next, take the next ID and element and put it into the second spot IF this ID doesn't match the first ID. If the ID matches the first ID, I will put the element under the first spot. If not, it will take place in the first empty spot. This is the case and thats what happens.

    The third read comes in, it is a different ID. I put it in spot 2 that has been empty along with its ID and the element that it came with.

    Thus, the array becomes...
    Code:
    [ID #5 - element 1, element 2][ID #1 - element 1][empty][...][empty]
    And so forth this will continue until all data has been read in.

    SO, to make this code. I must keep track of the ID's I am reading in. Thus, somehow be able to read an ID in and to see if it matches a previous ID that has been read in. If it matches, then to just update/add to that spot where the ID already exists. IF it doesn't have an match, fill in the next empty spot.

    My code design so far:

    Code:
    num_elements = 0;                                    // A counter to count number of elements read in
    
    array = calloc(1000, sizeof(data_type));      // Used calloc to initialize all array elements to 0
    
    // Note the structure is called data_type
    
    while(...) {                                  // Read through till the end
    
       flag = get_data(&id, &element)                // Gets a single ID and an element.
    
       // Rest of code focus on manipulating the array of structures... 
    
      ++num_elements;
    
       found_match = 0;
    
       for(i = 0; i < num_elements; ++i) {
    
           if(id == array[i].id) {
              found_match = 1;                              // A match was found, update the array only!
    
            /******************
            *     Update the        
            *     Array at this spot 
            ******************/
    
           }
    
       }
    
       if(found_match != 1){                              //  A match wasn't found, add the ID to the array!
          
           for(i = 0; i < num_elements; ++i) {
    
               if(array[i].id = 0)
                  data[i].id = id;
                  data[i].element1 = element;
                  break;                                                  
            // From my understanding, break will get us out of the current "for" loop and continue to  
            //  the next statement
    
               }
    
           }
    
       }
    
    }
    How does this plan sound?

    Comments?

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Not exactly sure how you are planning to use this but a few observations:

    1) What you are creating seems to be a sort of hash table of <elements>. If you are not familiar with a hash table data structure look it up, it may be of help.

    2) Once again not sure if this is the design you want, but it may help reduce memory use if instead of putting entire elements in your array, and thus duplicate this element chunks accross memory, to put pointers to elements.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    As the array gets larger, you'll have to do a lot of shuffling around of the contents, to make this work - and it's overly complicated imo.

    Why not take all the data records in, and then sort them all by ID number? Faster and simpler to build an index to those records, if needed, afterward.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    What kind of shuffling would be involved Could you elaborate?

    I feel, in my method, the FOR loops are used OKAY, in the sense, if it finds a match in the first one, it updates. IT should only find one match if it finds one... I believe?

    In the second FOR loop, since all values are initialized to 0, if a match is not found in the first, it then satisfies this new IF statement and goes to the FOR loop inside. The inside FOR loop is used to find the first 0 value in the ID spot and change it to the new ID and its new element.

    I do feel, however, its inefficient, in that it does a lot of cycling when you starting getting a lot of elements? Agree? Does that make sense?

    As far as sorting goes, if I take them all in, I feel I wont get the different elements under identical ID's under one ID (i.e. a situation that has different elements all under one ID). Instead I would have identical ID's appearing in the 1-D array of structures with only one different element under each like ID.

    Any more thoughts?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I guess it depends on what your definition of "under" is. Sort of like Bill Clinton with what the definition of "is" is. j/k

    From your description, it seemed like you could have this:

    1) the first record arrives, and takes the a[0] slot

    2) the second record arrives, doesn't match ID with the first, so it gets a[1]

    3) now the 3rd record arrives, and matches the first record. But "under" is taken, no? so you'd have to shuffle record in a[1], up to a[2], and put the newest record to arrive, into a[1], so it's "under" the right ID grouping.

    If that's the case, just dumping them in and sorting later by ID, would be preferable.

    If they have identical ID numbers, then when they're sorted with ID as the key, they'll all be "grouped" by ID number, and your program can handle them, as a group, by just referencing the ID number, whenever it needs to.

    I am having a bit of a time wrapping my head around your description, however.
    Last edited by Adak; 03-10-2010 at 04:47 PM.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Quote Originally Posted by Adak View Post
    I guess it depends on what your definition of "under" is. Sort of like Bill Clinton with what the definition of "is" is. j/k

    From your description, it seemed like you could have this:

    1) the first record arrives, and takes the a[0] slot

    2) the second record arrives, doesn't match ID with the first, so it gets a[1]

    3) now the 3rd record arrives, and matches the first record. But "under" is taken, no? so you'd have to shuffle record in a[1], up to a[2], and put the newest record to arrive, into a[1], so it's "under" the right ID grouping.

    If that's the case, just dumping them in and sorting later by ID, would be preferable.

    If they have identical ID numbers, then when they're sorted with ID as the key, they'll all be "grouped" by ID number, and your program can handle them, as a group, by just referencing the ID number, whenever it needs to.

    I am having a bit of a time wrapping my head around your description, however.
    Ahh, in my mind , I have the 3rd record arriving and matching the first record at a[0]. Since the ID is the same it just adds the new element under the a[0] spot. I guess that is what I am trying to have happen...

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I guess it all depends on the details of how many "unders" you have, "under your sleeve".

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    84

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Does not seem to be updating new elements...

    I.e. When the 3rd record arrives and matches the first record's ID at a[0]. Since the ID is the same, it should just add the new element under the a[0] spot. So a[0] should have:
    a[0]
    ID # 2
    element 1
    element 2

    Instead, it is only keeping the first element and not adding the new element under the proper ID. Ignoring it instead.

    Any idea why?

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Please also post the code where you declare the main variables of your program such as "array", "data",etc. It looks to me like you are writing very Java-ish code here assigning structs to each other, instead of using pointers to them to do this. This will not work in C. You can't say:

    Code:
    struct X{
      int y;
      char *z;
    
    };
    
    int main(int argc, char *argv[]){
      X str1,str2;
      /* initialize or read str1 str 2 from somewhere */
    
      str1 = str2; /* THIS WILL NOT WORK */
     return 0;
    }
    Instead what you want to do is something like:

    Code:
     X str1;
     X* str2;
     str2 = &str1;
     
     /*Now *str2 contains str1 */

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have a 1 dimension array of structs. There is no "under". If you want an "under" area for your array (head of each row), you need to make your array, a 2D array.

    Then you'll have an "under" area, for every number of your second dimension. If you call them "columns" instead of "under", it will help poor idjits like me, know wtf you mean.
    Last edited by Adak; 03-11-2010 at 12:03 PM.

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Adak View Post
    Then you'll have an "under" area, for every number of your second dimension. If you call them "columns" instead of "under", it will help poor idjits like me, know wtf you mean.
    LOL. Very true.

  13. #13
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Your right, I mean column instead of "under" when I use it ...

    Regarding earlier post:
    I have a pointer pointing to the array.. My code I posted is psuedo-code of its real life counterpart, made when I was developing a "plan".

    so I have:

    in the source file:
    Code:
    data_type     *array;
    
    array = calloc(1000, sizeof(data_type));
    In the header file:
    Code:
    typedef struct {
         ID
         element1
         element2
         ...
         element10
    } data_type
    Last edited by towed; 03-11-2010 at 05:57 PM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21