Storing cricketer information using STRUCTURE

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 %d\nPlayer Name\nTeam Name\nBatting Average\nin three different lines.\n\n”,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: %s\n”,tempteam);

    printf(“\tPlayer Name\tBatting average\n”);

    for(i=1;i<=n;i++)

    {

        if(strcmp(tempteam,player[i].teamname)!=0)

        {

            strcpy(tempteam,player[i].teamname);

            printf(“Team Name: %s\n”,tempteam);

            printf(“\tPlayer Name\tBatting average\n”);

        }

        printf(“\t%10s\t%15.2f\n”,player[i].playername,player[i].average);

    }

    return 0;

}

Share

6 thoughts on “Storing cricketer information using STRUCTURE

  1. I’ve read some good stuff here. Certainly worth bookmarking for revisiting. I wonder how a lot effort you place to make such a wonderful informative site.

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Lean Blog by Crimson Themes.