# Thread: Making A League Table

1. ## 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. 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. 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. 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?

Originally Posted by master5001
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. Originally Posted by apple_ranger
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. 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;
}```

7. @master5001: String literals can be glued together by the compiler you know

8. 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. > 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).

10. Thanks Salem. I am glad you pointed that out. I rushed too much for my own good.

11. 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 ??