Tuesday, March 22, 2011

Structures and Unions

Define a structure called cricket that will describe the following information:
Player name
team name
batting average
Using cricket, 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.

#include <stdio.h>
struct cricket
{
char name[20];
char team[25];
int batting;
};
void main()
{
int i=1,j,temp;
char dummy[25];
struct cricket player[60];
clrscr();
printf("This program is designed for collect cricket player information\n");
while(i<=50)
{
printf("Player Name:");
scanf("%s",player[i].name);
printf("Team Name:");
scanf("%s",player[i].team);
printf("Batting Average:");
scanf("%d",&player[i].batting);
i++;
}
clrscr();
for(i=1;i<50;i++)
for(j=i+1;j<=50;j++)
{
if(strcmp(player[i].team,player[j].team)>0)
{
strcpy(dummy,player[i].team);
strcpy(player[i].team,player[j].team);
strcpy(player[j].team,dummy);
strcpy(dummy,player[i].name);
strcpy(player[i].name,player[j].name);
strcpy(player[j].name,dummy);
temp=player[i].batting;
player[i].batting=player[j].batting;
player[j].batting=temp;
}
}
printf("Team Name       Player Name     Batting Average\n");
for(i=1;i<=50;i++)
printf("%-15s %-15s %10d\n",player[i].team,player[i].name,player[i].batting);
getch();
}

No comments:

Post a Comment