Thread: What data structure should I use?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    What data structure should I use?

    Class 1
    Max
    John
    Jessy
    Stephany


    Class 2
    Daniel
    Katie
    Tom


    Class 3
    Carlos
    Jess
    Aaron



    I want to store this data in the computer memory but I don't know how to connect John with Class 1, Katie with Class 2, etc. How can I do this? Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1
    You can make a structure of a class, and make 3 instances of that structure.

    Code:
    struct class
    {
         char *students[100]; 
         int seat;
         int class_num;
    };
    Code:
    class *class1;
    class1->students[1] = "God";
    You should be able to figure the rest out.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    char *students[100];
    // ...
    class *class1;
    class1->students[1] = "God";
    Maybe that last line was meant to be
    Code:
    strcpy(class1->students, "God");


    [edit]
    Oh, and by the way . . .
    Code:
    struct class {
    class is an invalid name. It's a keyword.
    [/edit]
    Last edited by dwks; 08-20-2005 at 07:48 PM.
    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.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    The problem is that I don't know how many classes I'll have and how many students each class will have. It has to be something dinamic

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could use an array of classes.
    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.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > class is an invalid name. It's a keyword
    Nope - it's a perfectly valid name in C

    > The problem is that I don't know how many classes I'll have
    So create a linked list of classes.

    Personally, I'd also have a linked list of students in each class.

    Do a search, there's plenty of prior discussion on linked lists.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    I would go something like this:
    Code:
    struct student{
      char *name;
      int seat;
      int class_num;  
    };
    
    struct class{
         struct student *students; 
    };
    This way your class can be as big or as small as you would like it to be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Include GTK+ just for a data structure?
    By Jesdisciple in forum C Programming
    Replies: 0
    Last Post: 04-12-2009, 07:19 PM
  2. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 12:42 PM
  3. Data structure implementation
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 07-31-2003, 07:44 AM
  4. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM