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;
}

Recommended Answers

All 2 Replies

Post your proposed code for the swap function. We are not going to give you the solution for this without some effort on your part. :-(

There are few things about your style of coding that could be improved:

/* use Global const (in C use #define) instead of 'magic numbers' */
#define MAX_LEN 31 /* pick numbers that give multiplies of 4 (or 8) bytes */ 

typedef struct
{
    char name[MAX_LEN+1], /* recall need space for terminal 0 */
         surname[MAX_LEN+1];
    int number;

} Player ; /* Use Cap then small case here */


typedef struct
{
    char nameofteam[MAX_LEN+1];
    int numberofplayers;
    Player* players;

} Team ;

/* 
Note: usually it is better to pass in data records by passing the address.
 */
void pr_replaces_numout( Team* tm, const Player* pr, int numout )
{
    int i;
    for( i = 0; i < tm->numberofplayers; ++ i )
    {
        if( tm->players[i].number == numout )
        {
            strcpy( tm->players[i].name, pr->name );
            strcpy( tm->players[i].surname, pr->surname );
            tm->players[i].number = pr->number;
            break;
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.