Thread: Making A League Table

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    10

    Question Making A League Table

    hi, can someone give me some pointers on how i can create a league table for a football game where each team have to play each other team in their home ground and the away ground. IF a team wins (they get 2 points), if they draw (they get 1 point) and if they lose (they get 0 points).

    currently i'm having troubles on how to display the number of points each team win , draw or lose , so that i can calculate the total points a team wins and lose or draws against ALL the other teams.

    i was thinking of using arrays , but i'm getting confused with how to arrange the arrays O.O" , can someone help ???

    --------------------------------------------------------------------------------

  2. #2
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    One approach would be to make an array of structures. Each struct would be named the 'team name' and would include wins, losses, ties, and points. There may be other ways, so see if there are other suggestions.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    #include <stdio.h>
    
    struct dog
    {
      char name[20];
      char breed[20];
      int length;
      int weight;
    };
    
    int main(void)
    {
      size_t i;
      struct dog table[] =
      {
        {"Feefee", "poodle", 25, 7},
        {"Rocko", "pitbull", 37, 18},
        {"Killer", "sheppard", 51, 62}
      };
    
      printf("|       name         |          breed      | len.| wght|\n|____________________|_____________________|_____|_____|\n");
      for(i = 0; i < sizeof(table)/sizeof(*table); ++i)
        printf(" &#37;20s | %20s | %3d | %3d\n", table[i].name, table[i].breed,
          table[i].length, table[i].weight);
    
      return 0;
    }
    No I didn't compile this to make sure the alignment is off. If it is sue me, its just a sample.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    ok i get that , but how do i initialise the array if i wasnt' given the number of teams or the team names and the user of the program is meant to input that?


    Quote Originally Posted by master5001 View Post
    Example:
    Code:
    #include <stdio.h>
    
    struct dog
    {
      char name[20];
      char breed[20];
      int length;
      int weight;
    };
    
    int main(void)
    {
      size_t i;
      struct dog table[] =
      {
        {"Feefee", "poodle", 25, 7},
        {"Rocko", "pitbull", 37, 18},
        {"Killer", "sheppard", 51, 62}
      };
    
      printf("|       name         |          breed      | len.| wght|\n|____________________|_____________________|_____|_____|\n");
      for(i = 0; i < sizeof(table)/sizeof(*table); ++i)
        printf(" %20s | %20s | %3d | %3d\n", table[i].name, table[i].breed,
          table[i].length, table[i].weight);
    
      return 0;
    }
    No I didn't compile this to make sure the alignment is off. If it is sue me, its just a sample.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by apple_ranger View Post
    ok i get that , but how do i initialise the array if i wasnt' given the number of teams or the team names and the user of the program is meant to input that?
    Then you don't. You can't create the array until you know how many teams there are. If that requires user input, then you're going to have to create the "array" dynamically during runtime. See "malloc" for more details.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Expanded Example:
    Code:
    #include <stdio.h>
    
    struct dog
    {
      char name[20];
      char breed[20];
      int length;
      int weight;
    };
    
    void die(void *x)
    {
      if(x)
        free(x);
    
      fputs("Out of memory.", stderr);
      abort();
    }
    
    int main(void)
    {
      size_t i, x;
      struct dog *table, *tmp;
      char line[256];
    
      x = 50;
      i = 0;
    
      table = malloc(x * sizeof(*table));
      if(!table)
        die(0);
    
      while(1)
      {
        fputs("Describe your dog: [name] [breed] [length] [weight] ", stdout);
    
        if(!fgets(line, sizeof(line), stdin))
          break;
    
        sscanf(line, "[&#37;20s ] [%20s ] [%d] [%d]", table[i].name, table[i].breed, 
          &table[i].length, &table[i].weight);
    
        if(++i == x)
        {
           tmp = realloc(table, (x += 50) * sizeof(*table));
           if(!tmp)
              die(table);
           table = tmp;
        }
      }
    
      x = i;
    
      printf("|       name         |          breed      | len.| wght|\n|____________________|_____________________|_____|_____|\n");
      for(i = 0; i < x; ++i)
        printf(" %20s | %20s | %3d | %3d\n", table[i].name, table[i].breed,
          table[i].length, table[i].weight);
    
      return 0;
    }
    Last edited by master5001; 10-17-2008 at 01:17 PM.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    @master5001: String literals can be glued together by the compiler you know

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I know. Its also easier for me to tap the keyboard and count in one continuous line since I was lazily writing code directly into the Quick Reply box.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > sscanf(line, "[%20s] [%20s] [%d] [%d]",
    %s will read ] as well, so unless your names are always and exactly 20 chars, this won't work.
    And if they are, then that's a buffer overflow.

    > tmp = realloc(table, (x += 50));
    You forgot the "scale" factor, for the size of each element of the array (like you managed in the malloc).
    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.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Thanks Salem. I am glad you pointed that out. I rushed too much for my own good.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    can i ask you what scale factor you forgot in the C code. i try running the code you made on gcc but it had errors. can you please tell me what is wrong ??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-07-2008, 07:48 AM
  2. Help with code reading table please
    By cjohnman in forum C Programming
    Replies: 0
    Last Post: 04-28-2008, 08:17 AM
  3. ASP.NET question (looping inside a table)
    By Rainbowinblack in forum C# Programming
    Replies: 0
    Last Post: 03-06-2008, 03:10 PM
  4. Lookup table with different spacing between entries
    By Boksha in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2008, 02:40 PM
  5. Why isn't it working.
    By silhoutte75 in forum C Programming
    Replies: 9
    Last Post: 01-28-2008, 11:11 AM