Sorting a table and saving a table help!!!

This is a discussion on Sorting a table and saving a table help!!! within the C Programming forums, part of the General Programming Boards category; Hi, im pretty new to C! and i urgently need help!!! i need to sort this league table i have ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    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;
    
    char showmenu();
    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 showmenu()
    {
         char c, returnc;
    
         printf("\nPlease choose an option:\n");
         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) {
                printf("Incorrect, please re-input teams\n");
                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. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

    Adak

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    ok mate thanks for the advise! ill get cracking with this bubble sort! thanx!

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    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. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You don't have to iterate through the array backwards to sort the array in decending order; you can just change the > to a <.

    [edit] Also, you could replace the goto label reinput with a while loop quite easily. [/edit]
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Saving to a file!
    By MrMe913 in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 09:54 AM
  2. Saving corrupts my database.
    By JamesM in forum C Programming
    Replies: 5
    Last Post: 09-29-2003, 03:53 AM
  3. saving mutliple variables.......
    By jverkoey in forum C++ Programming
    Replies: 10
    Last Post: 02-13-2003, 07:34 PM
  4. saving strings to a table
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-14-2002, 10:47 AM

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