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

#include <stdio.h> #include <stdlib.h> #include <string.h> void order_two_player

ID: 3742292 • Letter: #

Question

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void order_two_players(player_t* player1_p, player_t* player2_p)

{

/*

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_all_players(player_t* players, int players_len)

{

/*

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

*/

}

Explanation / Answer

#include #include #include typedef struct player { int age; }player_t; void order_two_players(player_t* player1_p, player_t* player2_p) { /* 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. */ int age; if(player2_p->age age) { age = player1_p->age; player1_p->age = player2_p->age; player2_p->age = age; } } void order_all_players(player_t* players, int players_len) { /* 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 */ int i, j, age; for(i = 0; i