Write a function for swapping players in a team. New player (which is read) enters team and replacing the player which is also read.
Prototype of function is void swap(TEAM *, PLAYER, int);
First argument of a function is the pointer to team, second argument is the player that enters in team, and the third argument is the jersey number of a player that leaves the team.
Could someone show how to implement the function for swapping players in the following code:
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
char name[25],surname[25];int number;
}PLAYER;
typedef struct
{
char nameofteam[25];int numberofplayers;PLAYER *players;
}TEAM;
void readplayer(PLAYER *pi)
{
printf("name:");scanf("%s",pi->name);
printf("surname:");scanf("%s",pi->surname);
printf("number of player:");scanf("%d",&pi->number);
}
void readteam(TEAM *pt)
{
int i;
printf("name of team:");scanf("%s",pt->nameofteam);
printf("number of players in team:");scanf("%d",&pt->numberofplayers);
pt->players = calloc(pt->numberofplayers, sizeof(*pt->players));
for(i=0;i<pt->numberofplayers;i++)
{
printf("%d. player:\n",i+1);
readplayer(pt->players+i);
}
}
void print(TEAM *p)
{
int i;
printf("TEAM:%s\n",p->nameofteam);
printf("PLAYERS:\n");
printf("ON. SURNAME NAME NUM.\n");
printf("--- --------------- --------------- ----\n");
for(i=0;i<p->numberofplayers;i++)
{
printf("%2d.",i+1);
printf("%-15s %-15s %4d",
p->players[i].surname,p->players[i].name,p->players[i].number);
printf("\n");
}
printf("--- --------------- --------------- ----");
}
void swap(TEAM *p,PLAYER newplayer,int num)
{
//...//
}
void erase1(TEAM *pt)
{
free(pt->players);
}
int main()
{
int n,num;
TEAM *p;
PLAYER newplayer;
do
{
printf("n=");scanf("%d",&n);
}
while(n!=1);
p=(TEAM *)malloc(n * sizeof(TEAM));
printf("data about team:\n");
readteam(p);
//replace(p,newplayer,num);
print(p);
free(p);
return 0;
}