Thread: group struct info

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    41

    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,

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    41
    Could any one give me an example, even if a simple one.

    Thanks,

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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 */
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. Binary trees search problem...
    By Umoniel in forum C Programming
    Replies: 2
    Last Post: 02-22-2004, 02:29 PM
  4. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM