Thread: 1-D array of structures with a need to update the elements in the struct and add to..

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

    Question 1-D array of structures with a need to update the elements in the struct and add to..

    Title (cont): or add to the array if a new structure with a new ID exists .


    Consider:

    I have an 1-D array of structures, each structure containing set possible elements.

    I pre-initialize the array and the structure elements with missing values.

    I have created a function to read in the data and assign them to a structure, with the "key" of each structure (to organize the array) to compare being the ID.

    I want to add and update the array as data is read in, in this pre-determined structure form.

    My notion:
    First ID is read in and given the first spot with its element. 2nd ID is read in with its element, with ID that is different from the first, and given the next spot. 3rd ID is read in and its the same as the first. I must now, add this element if existing or update the element already there in the first spot that has the corresponding same ID. So: Problem - I have to keep matches of the IDs to somehow update? AND if no match is found - then add it to the array of structs and then note that we now have a match if another ID gets read in and its already existing.

    Keep in mind: Space for the 1-D array of struct has been allocated and etc. I have assumed everything up to this point is working...

    So the framework. Pardon the code - I am just trying conceptually build this, pasting my code and functions would be too hard and I don't feel it is necessary. The idea is the where I am struggling..

    Code:
    Psuedo Code:
    typedef struct {
        e1;  // e for element
        e2;
        ...
        e9;
    }  data
    
    /* ---- */
    While(...) { // Read to the EOF
      flag = function(&passed_in->e) // Reads in the ID's and their elements - passing out the arguments assigned to struct to then be added to the array of structs.
      
      n_id = 0; // Counter to count number of ID's read in
      
      found_match = 0; //For this ID just given - mark that it has no match 
    
      // How would it keep track if we found a match? - Should it know?
    
      for(i = 0; i < n_id; i++) {
          found_match = 1;
          if(found_match = 0){
            found_match = 1;
            
               if(passed_in->e = tag_1) //tag is known before hand, lets use know if its e1 or e2 or ...
                  data[i].element1 =  passed_in->e1;
               else if(passed_in->e = tag_2) {
                  data[i].element1 =  passed_in->e2;
               }
    
          } else { // Means that found_match = 1, so have to update the preexisting id in the 1-d  
                           array of struct with the right element
              
               if(passed_in->e = tag_1) //tag is known before hand, lets use know if its e1 or e2 or ...
                  data[i].element1 =  passed_in->e1;
               else if(passed_in->e = tag_2) {
                  data[i].element1 =  passed_in->e2;
               }
    
    
          }
    
        //How does it keep track now that we will have a match if the same ID occurs? - Should it know?
       
       }
    Any ideas or comments - conceptually I am stumped in this problem.
    Last edited by towed; 02-24-2010 at 09:38 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Not sure I follow what you're talking about.
    Define the problem as clearly as possible.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Quote Originally Posted by towed View Post
    Title (cont): or add to the array if a new structure with a new ID exists .


    Consider:

    I have an 1-D array of structures, each structure containing set possible elements.

    I pre-initialize the array and the structure elements with missing values.

    I have created a function to read in the data and assign them to a structure, with the "key" of each structure (to organize the array) to compare being the ID.

    I want to add and update the array as data is read in, in this pre-determined structure form.

    My notion:
    First ID is read in and given the first spot with its element. 2nd ID is read in with its element, with ID that is different from the first, and given the next spot. 3rd ID is read in and its the same as the first. I must now, add this element if existing or update the element already there in the first spot that has the corresponding same ID. So: Problem - I have to keep matches of the IDs to somehow update? AND if no match is found - then add it to the array of structs and then note that we now have a match if another ID gets read in and its already existing.

    Keep in mind: Space for the 1-D array of struct has been allocated and etc. I have assumed everything up to this point is working...

    So the framework. Pardon the code - I am just trying conceptually build this, pasting my code and functions would be too hard and I don't feel it is necessary. The idea is the where I am struggling..

    Code:
    Psuedo Code:
    typedef struct {
        e1;  // e for element
        e2;
        ...
        e9;
    }  data
    
    /* ---- */
    While(...) { // Read to the EOF
      flag = function(&passed_in->e) // Reads in the ID's and their elements - passing out the arguments assigned to struct to then be added to the array of structs.
      
      n_id = 0; // Counter to count number of ID's read in
      
      found_match = 0; //For this ID just given - mark that it has no match 
    
      // How would it keep track if we found a match? - Should it know?
    
      for(i = 0; i < n_id; i++) {
          found_match = 1;
          if(found_match = 0){
            found_match = 1;
            
               if(passed_in->e = tag_1) //tag is known before hand, lets use know if its e1 or e2 or ...
                  data[i].element1 =  passed_in->e1;
               else if(passed_in->e = tag_2) {
                  data[i].element1 =  passed_in->e2;
               }
    
          } else { // Means that found_match = 1, so have to update the preexisting id in the 1-d  
                           array of struct with the right element
              
               if(passed_in->e = tag_1) //tag is known before hand, lets use know if its e1 or e2 or ...
                  data[i].element1 =  passed_in->e1;
               else if(passed_in->e = tag_2) {
                  data[i].element1 =  passed_in->e2;
               }
    
    
          }
    
        //How does it keep track now that we will have a match if the same ID occurs? - Should it know?
       
       }
    Any ideas or comments - conceptually I am stumped in this problem.
    I'll try.

    The problem. I am reading in elements that are part of a structure, each element includes an ID as it is read in. I am creating a 1-d array of structures to hold the elements for different ID's or stations - so the ID's are the common thing with these elements as their read. I have to add the structure and, thus, the ID to the array as it is read in AND if it has been added to the array already then to just update the array under the right structure that is holding the identical ID. Then, I think, I must keep track to see if we have an match of said ID already in the 1-d array of structures.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    26
    I am not able to understand your problem clearly with the given data.
    In the following code,
    Code:
    found_match = 0; //For this ID just given - mark that it has no match 
    
      // How would it keep track if we found a match? - Should it know?
    
      for(i = 0; i < n_id; i++) {
          found_match = 1;
          if(found_match = 0){
            found_match = 1;
            
               if(passed_in->e = tag_1) //tag is known before hand, lets use know if its e1 or e2 or ...
                  data[i].element1 =  passed_in->e1;
               else if(passed_in->e = tag_2) {
                  data[i].element1 =  passed_in->e2;
               }
    
          } else { // Means that found_match = 1, so have to update the preexisting id in the 1-d  
                           array of struct with the right element
              
               if(passed_in->e = tag_1) //tag is known before hand, lets use know if its e1 or e2 or ...
                  data[i].element1 =  passed_in->e1;
               else if(passed_in->e = tag_2) {
                  data[i].element1 =  passed_in->e2;
               }
    
    
          }
    before for loop you assigned the value zero to found_match.Then again inside the for loop you are assigning the value one again in if you are assigning zero .With this data I am not able to understand clearly.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    37
    Actually you misplaced the comparison operator with the assignment operator.
    if ( found_match ==0 ) like wise you need to test the condition instead of if ( found_match =0 )

  6. #6
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    According to this code, found_match will always = (equal) 1. Where is the code to check if something matches so found_match can be set to 1 or 0???
    The keyboard is the standard device used to cause computer errors!

  7. #7
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Also, real quick. You set n_id = 0; That means your 'for loop' will only execute once. Theres no point in hard coding a for loop to only execute once.
    The keyboard is the standard device used to cause computer errors!

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Quote Originally Posted by stumon View Post
    Also, real quick. You set n_id = 0; That means your 'for loop' will only execute once. Theres no point in hard coding a for loop to only execute once.
    Sorry forgot to add a line,

    I am actually incrementing n_id like so:

    ++n_id;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

Tags for this Thread