Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C programming Consider the following struct definition, struct player { int age;

ID: 3743938 • Letter: C

Question

C programming

Consider the following struct definition,
struct player {
    int age;
    int wickets;
};
typedef struct player player_t;

In this task you are required to implement the functions given below by following the function descriptions.


/*
This function allocates memory for a player_t pointer variable.
Inputs:
  player_p_p - memory location of a player_t pointer variable
  nplayers - number of players that memory needs to be allocated for
Return:
  0 - success
  1 - failed to allocate memory
Post:
  After the function has been called, *player_p_p will point to freshly
  allocated memory if malloc was successful. Otherwise *player_p_p will point to
  NULL.
*/
int allocate_memory(player_t** player_p_p, long int nplayers)

/*
This function initialises each field of one playet_t struct.
Inputs:
  player_p - memory location of the player_t variable. player_p must have been
    allocated memory using allocate_memory before calling this function.
  nplayers - number of players that memory needs to be allocated for
Post:
  After the function has been called, the age field of *player_p will be the
  input age, and wickets field of *player_p will be the input wickets.
*/
void init_player(player_t* player_p, int age, int wickets)

/*
This function prints a player_t variable in the following format:
player - age:AA wickets:WWW
Inputs:
  player_p - variable to be printed
*/
void print_player(player_t player)

/*
This function compares two players. If *player1_p is older than the *player2_p,
swap them. In all other cases, do not swap them.
Inputs:
  player1_p - memory location of the first player
  player2_p - memory location of the second player
Post:
  After the function has been called, the age of *player1_p is always less than
  or equal to *player2_p age.
*/
void order_two_players(player_t* player1_p, player_t* player2_p)

/*
Sort an array of players in non-decreasing order of the age by implementing the
following algorithm:
1. Compare two adjacent players, if the first player is older than the second,
   swap the first and second players.
2. Keep comparing the next two adjacent players in the array, until the end of
   the array is reached.
3. Repeat the above steps for players_len times.
This simple algorithm is also known as bubble sort.
Inputs:
  players - player_t array (memory location of 0th element in the array)
  players_len - number of players in the array
*/
void order_all_players(player_t* players, int players_len)

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

struct player

{

int age;

int wickets;

};

typedef struct player player_t;

int allocate_memory(player_t *player_p_p, long int nplayers)

{

player_p_p = (player_t *)malloc(nplayers * sizeof(long int)); // allocate memory

if (player_p_p == NULL) // check if memory allocated

{

printf("Unable to allocate memory space program terminate ");

return 1;

}

else

return 0;

}

void init_player(player_t *player_p, int age, int wickets)

{

player_p->age = age;

player_p->wickets = wickets;

}

void print_player(player_t player)

{

printf("player - age: %d,wickets :- %d ", player.age, player.wickets);

}

void order_two_players(player_t *player1_p, player_t *player2_p)

{

player_t *temp;

if (player1_p->age > player2_p->age)

{

temp = player1_p;

player1_p = player2_p;

player2_p = temp;

}

}

void order_all_players(player_t *players[], int players_len)

{

int i, j;

player_t *temp;

for (i = 0; i < players_len - 1; i++)

{

for (j = 0; j < players_len - i - 1; j++)

{

if (players[j]->age > players[j + 1]->age)

{

temp->age = players[j]->age;

players[j]->age = players[j + 1]->age;

players[j + 1]->age = temp->age;

}

}

}

}

void main()

{

player_t player1, player2;

int x;

x = allocate_memory(&player, 2);

if (x == 0)

printf("Successfully memory allocated ");

else

return 1;

init_player(&player, 22, 5);

print_player(player);

init_player(&player1, 22, 5);

init_player(&player2, 20, 8);

order_two_players(&player1, &player2);

}

//Test this program as you wish and if you have any problem please commant