Question: Define a structure called “Cricket” that will describe the following information: player name, team name and batting average. Using the structure, declare an array player with 50 elements and write a program to read the information about all the 50 players and print a team-wise list containing names of players with their batting average.
For video explanation, stay tuned…. it will come soon.
Source Code:
#include <bits/stdc++.h>
struct Cricket{
char playername[100];
char teamname[25];
float average;
}player[51];
int main()
{
int i,j,n,p;
char tempteam[25],tempplayer[100];
float tempaverage;
printf(“How many informations of players you want to give as input? “);
scanf(“%d”,&n);
printf(“Enter %dnPlayer NamenTeam NamenBatting Averagenin three different lines.nn”,n);
for(i=1;i<=n;i++)
{
scanf(“n”,&p);
gets(player[i].playername);
gets(player[i].teamname);
scanf(“%f”,&player[i].average);
}
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-i;j++)
{
if(strcmp(player[j].teamname,player[j+1].teamname)>0)
{
strcpy(tempteam,player[j].teamname);
strcpy(player[j].teamname,player[j+1].teamname);
strcpy(player[j+1].teamname,tempteam);
strcpy(tempplayer,player[j].playername);
strcpy(player[j].playername,player[j+1].playername);
strcpy(player[j+1].playername,tempplayer);
tempaverage=player[j].average;
player[j].average=player[j+1].average;
player[j+1].average=tempaverage;
}
}
}
strcpy(tempteam,player[1].teamname);
printf(“Team Name: %sn”,tempteam);
printf(“tPlayer NametBatting averagen”);
for(i=1;i<=n;i++)
{
if(strcmp(tempteam,player[i].teamname)!=0)
{
strcpy(tempteam,player[i].teamname);
printf(“Team Name: %sn”,tempteam);
printf(“tPlayer NametBatting averagen”);
}
printf(“t%10st%15.2fn”,player[i].playername,player[i].average);
}
return 0;
}
