Write a C Program to Sort Structures Elements

Sort Structures Elements


Write a C Program to Sort Structures Elements. Here’s simple C Program to Sort Structures Elements in C Programming Language.


Below is the source code for C Program to Sort Structures Elements which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program to Sort Structures Elements */

#include<stdio.h>
#include<string.h>

struct cricket
{
   char pname[20];
   char tname[20];
   int avg;
} player[10], temp;

int main()
{
   int i, j, n;

    for (i = 0; i < 10; i++)
    {
      printf("\nEnter Player Name : ");
      scanf("%s", player[i].pname);
      printf("\nEnter Team Name : ");
      scanf("%s", player[i].tname);
      printf("\nEnter Average : ");
      scanf("%d", &player[i].avg);
      printf("\n");
   }
   n = 10;

    for (i = 1; i < n; i++)
        for (j = 0; j < n - i; j++)
        {
            if (strcmp(player[j].tname, player[j + 1].tname) > 0)
            {
                temp = player[j];
                player[j] = player[j + 1];
                player[j + 1] = temp;
            }
        }

    for (i = 0; i < n; i++)
    {
      printf("\n%s\t%s\t%d",player[i].pname,player[i].tname,player[i].avg);
    }

   return 0;
}

Output : 


/* C Program to Sort Structures Elements  */

Enter Player Name : CodezClub

Enter Team Name : Programming

Enter Average : 100


Enter Player Name : John

Enter Team Name : C

Enter Average : 80


Enter Player Name : Max

Enter Team Name : Java

Enter Average : 90


Enter Player Name : Ramsey

Enter Team Name : C++

Enter Average : 78


Enter Player Name : Watson

Enter Team Name : PHP

Enter Average : 99


Enter Player Name : Payne

Enter Team Name : C

Enter Average : 98


Enter Player Name : Rambo

Enter Team Name : C++

Enter Average : 97


Enter Player Name : A-kay

Enter Team Name : 45

Enter Average : 78


Enter Player Name : Walt

Enter Team Name : Django

Enter Average : 88


Enter Player Name : Bolt

Enter Team Name : 99

Enter Average : 99


A-kay   45      78
Bolt    99      99
John    C       80
Payne   C       98
Ramsey  C++     78
Rambo   C++     97
Walt    Django  88
Max     Java    90
Watson  PHP     99
CodezClub       Programming     100

Process returned 0

Above is the source code for C Program to Sort Structures Elements which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

Leave a Reply