What data structure should I use?

This is a discussion on What data structure should I use? within the C Programming forums, part of the General Programming Boards category; Class 1 Max John Jessy Stephany Class 2 Daniel Katie Tom Class 3 Carlos Jess Aaron I want to store ...

  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,046
    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,046
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 11:42 AM
  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, 05:03 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21