-
group struct info
Hello All,
I have some data into an struct and I would like to group those information. My struct looks like this:
Code:
struct MyStruct data[MAX];
struct MyStruct {
char date[20];
char code[5];
};
void PrintStruct() {
int x;
for(x=0; x<MAX; x++) {
printf("\t\t** data[%d].date: [%s] **\n",x,data[x].date);
printf("\t\t** data[%d].cc_code: [%s] **\n",x,data[x].code);
}
}
I would like to count how many records of an certain code I have and group them. For example:
[1]=10
[-1]=99
[8]=12
And so on. Any help ?
Another thing that I'm trying to do is count how many records my stryct has. I've define MAX to 100000 but I don't want to loop all the 10000 records if I only have 20 records on that moment, for example.
Any help ?
Thanks a lot,
-
Keep a count of the total number of elements used, that way you don't need to loop through MAX entries.
Counting and grouping is going to be a looping and shuffling exercise, I'm afraid. Unless you want to change data structure, and use a list of some sort.
-
Could any one give me an example, even if a simple one.
Thanks,
-
Just do something like this:
Code:
#define MAX 1000000
while(there_are_more_structs) {
read_in_a_struct();
struct_count ++;
}
for(...; count < struct_count; ...) {
/* classify the struct */
}