# Thread: Sorting a table and saving a table help!!!

1. ## Sorting a table and saving a table help!!!

Hi, im pretty new to C! and i urgently need help!!! i need to sort this league table i have created! so whoever has the most points comes top, etc...! i also need to save the table to a "user-specified" file! ur help will be gr8ly (and i mean gr8ly!) appreciated!

heres my code!

Code:
```#include <stdio.h>

typedef struct {
char name[12];
int p,w,d,l,f,a,t;
} team;

int teamentry();
int showtable(int);
void inputmatchresults(int);

team side[12];

int main(void)
{
char c, returnc;
int n = -1;

while ((c = showmenu()) != 'd') {
switch(c) {
case 'a':
printf("enter teams\n");
n = teamentry();
break;
case 'b':
if (n == -1) {
printf("You need to enter the teams first\n");
} else {
printf("display the current table\n");
showtable(n);
}
break;
case 'c':
if (n == -1) {
printf("You need to enter the teams first\n");
} else {
printf("enter match results\n");
inputmatchresults(n);
}
break;
default:
printf("make sure you have typed a valid option\n");
break;
}
}

printf("Exit\n");

return 0;
}

{
char c, returnc;

printf("\t a - input teams, b - show table, c - input results\n\t d - Exit\$
printf("enter the option: ");
scanf("%c%c", &c, &returnc);

return c;
}

int teamentry()
{
char returnc;
int n,i;

printf("number of teams: ");
scanf("%d%c", &n,&returnc);
for (i =0; i<n; i++) {
printf("Team%d\n",(i+1));
printf("\tName: ");
scanf("%s%c", &side[i].name, &returnc);
side[i].p = 0;
side[i].w = 0;
side[i].d = 0;
side[i].l = 0;
side[i].f = 0;
side[i].a = 0;
side[i].t = 0;
}

return n;
}

int showtable(int n)
{
int i;
printf("%-12s %3s %3s %3s %3s %3s %3s %3s \n","","P","W","D","L","F","A","T\$
for (i = 0; i<n; i++) {
printf("%-12s %3d %3d %3d %3d %3d %3d %3d\n",side[i].name, side[i].p,si\$
}

return 0;
}

void inputmatchresults(int n)
{
int t1,t2,f1,f2;
char returnc;
reinput: //this uses a goto lable for the re-input
printf("the teams you wish to enter the results for are: ");
scanf("%d%d%c", &t1, &t2, &returnc);
//correct t1 and t2 for array
t1--; t2--;
if (t1 > n || t2 > n || t1 == t2 || t1 < 0 || t2 < 0) {
goto reinput;
} else {
printf("goals scored for %s:", side[t1].name);
scanf("%d%c", &f1, &returnc);
printf("goals scored for %s:", side[t2].name);
scanf("%d%c", &f2, &returnc);
//check result
if (f1 > f2) {
//team1 wins
printf("%s wins\n", side[t1].name);
//update team1 winner
side[t1].p++;
side[t1].a += f2;
side[t1].f += f1;
side[t1].w++;
side[t1].t += 3;
//update team2 loser
side[t2].p++;
side[t2].a += f1;
side[t2].f += f2;
side[t2].l++;
} else if (f2 > f1) {
//team2 wins
printf("%s wins\n", side[t2].name);
//update team2 winner
side[t2].p++;
side[t2].a += f1;
side[t2].f += f2;
side[t2].w++;
side[t2].t += 3;
//update team1 loser
side[t1].p++;
side[t1].a += f2;
side[t1].f += f1;
side[t1].l++;
} else {
//draw
printf("Draw\n");
//update team1 draw
side[t1].p++;
side[t1].a += f2;
side[t1].f += f1;
side[t1].d++;
side[t1].t += 1;
//update team2 draw
side[t2].p++;
side[t2].a += f1;
side[t2].f += f2;
side[t2].d++;
side[t2].t += 1;
}
}
}```

2. OK, so you have 12 items (structs) in your team array. You want to sort them according to their points in descending order.

Bubble sort should be easy to do, except for moving the string field. You'll need to use a while loop to go through the letters until you reach the end of string marker char '\0'.

The work is straight forward otherwise. You show no work for a sorting function, that I can see. You need to make at least an attempt, then post up if and when you have some difficulties making it work.

3. ok mate thanks for the advise! ill get cracking with this bubble sort! thanx!

4. A kind of bubble sort algorithm, it may be helpful

Code:
```	for(j = size-1 ; j >= 0; j--){
for(i = 1 ; i <= j ; i++){
if(x[i-1] > x[i]){
temp = x[i-1];
x[i-1] = x[i+1];
x[i] = temp;
}
}
}```

5. You don't have to iterate through the array backwards to sort the array in decending order; you can just change the > to a <.

 Also, you could replace the goto label reinput with a while loop quite easily. [/edit]