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.