Thread: how to print scores in descending order

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    99

    how to print scores in descending order

    I have always wanted to know this. 16 teams play each other in this program. the user enters those teams and then the teams play each other and the user enters the teams's scores.WHAT I WANT TO DO IS GET A WAY OF PRINTING THE SCORES OF EACH TEAM ACCORDING TO WHO HAS THE MOST. for example chelsea vs liverpool. chelsea:2 liverpool 1. next game say chelsea vs arsenal. chelsea:4 arsenal:3
    now chelsea have scored 6 in total. arsenal have scored 3 in total and liverpool have score 1 in total. BUT I WONT KNOW THE SCORES. so is there a way i can print the scores in order according to has the most. for example now: chelsea have scored 6 in total
    arsenal have scored 3 in total
    liverpool have scored 1 in total
    here's my program. no need to worry it's simple and works perfectly but i just want to know what i have stated. cheers in advance.



    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define MAXTEAMS 16
    typedef struct team
    {
        char name[50];
        int points;
        int scored;
    }teams;
    
    
    
    
    teams team1[MAXTEAMS] = {{"a"},{"b"},{"c"},{"d"},{"e"},{"f"},{"g"},{"h"},{"i"},{"j"},{"k"},{"l"},{"m"},{"n"},{"o"},{"p"}};
    
    
    int main()
     {
         team1[0].scored = 0;
         team1[1].scored = 0;
         team1[2].scored = 0;
         team1[3].scored = 0;
         team1[4].scored = 0;
         team1[5].scored = 0;
         team1[6].scored = 0;
         team1[7].scored = 0;
         team1[8].scored = 0;
         team1[9].scored = 0;
         team1[10].scored = 0;
         team1[11].scored = 0;
         team1[12].scored = 0;
         team1[13].scored = 0;
         team1[14].scored = 0;
         team1[15].scored = 0;
        int i;
    
    
                for(i=0;i<MAXTEAMS;i++)
                {
                    printf("enter team:\n");
                    scanf("%s",team1[i].name);
                }
                printf("welcome to the champions league\n\n");
    
    
    
    
                         for(i=1;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[0].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[0].name);
                            scanf("%d",&team1[0].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[0].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                                 for(i=2;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[1].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[1].name);
                            scanf("%d",&team1[1].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[1].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=3;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[2].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[2].name);
                            scanf("%d",&team1[2].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[2].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=4;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[3].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[3].name);
                            scanf("%d",&team1[3].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[3].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=5;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[4].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[4].name);
                            scanf("%d",&team1[4].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[4].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=6;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[5].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[5].name);
                            scanf("%d",&team1[5].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[5].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=7;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[6].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[6].name);
                            scanf("%d",&team1[6].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[6].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=8;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[7].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[7].name);
                            scanf("%d",&team1[7].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[7].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=9;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[8].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[8].name);
                            scanf("%d",&team1[8].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[8].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=10;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[9].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[9].name);
                            scanf("%d",&team1[9].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[9].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=11;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[10].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[10].name);
                            scanf("%d",&team1[10].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[10].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=12;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[11].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[11].name);
                            scanf("%d",&team1[11].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[11].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=13;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[12].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[12].name);
                            scanf("%d",&team1[12].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[12].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=14;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[13].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[13].name);
                            scanf("%d",&team1[13].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[13].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                            printf("%s vs %s\n",team1[14].name,team1[15].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[14].name);
                            scanf("%d",&team1[14].scored);
                            printf("%s's score:",team1[15].name);
                            scanf("%d",&team1[15].scored);
                            team1[14].scored += team1[0].scored;
                            team1[15].scored += team1[i].scored;
    
    
    
    
    return(0);
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,730
    Jeez, you know you can put a for loop inside another, don't you?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    i know but i wanted to get this one right instead of confusing myself because im new to structs but thanks for the help?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,730
    All these repeated code is actually much more confusing. Anyway, about your problem, you'll need a sorting algorithm, I suggest Insertion Sort or Selection Sort because both are easy to do. After your array is sorted, a simple iteration and printing of names and scores will suffise.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    ok thanks for the help only saw your comment now . cheers

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    instead of writing your own sort, you can use the standard library function qsort

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-04-2011, 11:31 PM
  2. three numbers in descending order
    By jackson6612 in forum C++ Programming
    Replies: 3
    Last Post: 05-21-2011, 08:05 AM
  3. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  4. ascending and descending order
    By ssk in forum C Programming
    Replies: 3
    Last Post: 03-25-2011, 08:03 PM
  5. Sorting in descending order
    By michael- in forum C Programming
    Replies: 3
    Last Post: 12-12-2005, 01:07 PM