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

C language only (no C++) Thanks! CS210 - Machine Problem 4 Swiss Tournament Mana

ID: 3873632 • Letter: C

Question

C language only (no C++) Thanks! CS210 - Machine Problem 4 Swiss Tournament Manager 20 Points Assigned: September 26, 2017 Due: October 5, 2017 Tournaments are design to find out who is the best at a particular competition out of a group of partic- ipants. There are many different formats of tournaments with different properties. An interesting fornmat is known as a Swiss Tournament. In this type of tournament, there are no eliminations, so everyone gets to play every round. In each round, participants with the same win-loss (or as close as posible). This ensure record are paired against esch other attempts to ensure that most games are competitive and that the best players can be quickly determined. This program will run a Swiss Tournament with a given number of participants for a given number of rounds. It will identify the players by consecutive id numbers starting with 1001. After each round it wil display the current standings. The pairings for the next round will be determined by the ranks in that display (i.e. The first two players will play against each other in the next rounds, then 3 and 4, then 5 and 6, etc.) See the sample run for examples Program Design Again, this program has several tasks to be performed. In am going to describe the functions that you will most likely need to write. However, it is going to be up to you to design the parameter lists, local variables and algorithms for each function. Data The data for the tournament consist of the following: The number of participants The number of rounds An array of id numbens . An array of wins for each id: (Note: These arrays are known as paralel arrays, which means that the id in slot i, corresponds to the number of wins in slot i in the other array) Print Greeting Prints the greeting for the application Get Number of Participants This function is responsible for prompting the user for the number of participants in the tournament, vali- dating it, and getting the data back to the calling function

Explanation / Answer

#include<stdio.h>
//Function to return number of participants
int getNumberOfParticipants()
{
//To store number of participants
int no;
//Loops till valid number of participants
do
{
//Accepts number of participants
printf(" Enter number of Participants (must be even): ");
scanf("%d", &no);
//Checks if number of participants is less than 1 then display error
if(no < 1)
printf(" Must be at least 2 (Try again). ");
//Checks if number of participants is odd then display error
else if(no % 2 != 0)
printf(" Must be even (Try again). ");
//Checks if number of participants is more than 64 display error
else if(no > 64)
printf(" Must be less than 64 (Try again). ");
//Otherwise valid number of participants return the value
else
return no;
}while(1);//End of do - while
}//End of function
//Function to return number of rounds
int getnumberOfRounds()
{
int rounds;
//Loops till valid number of rounds
do
{
//Accept number of rounds
printf(" Enter number of rounds to play: ");
scanf("%d", &rounds);
//Checks if number of rounds is negative display error message
if(rounds <= 0)
printf(" Must play at least one round. Try again. ");
//Otherwise return number of rounds
else
return rounds;
}while(1);//End of do - while
}//End of function
//Function to initialize arrays
void initialize(int player, int id[], int win[], int loss[])
{
int x;
int c = 1001;
//Loops till number of players and assigns id to the array by incrementing one
for(x = 0; x < player; x++)
id[x] = c++;
//Loops till number of players and assigns zero to the array win and loss
for(x = 0; x < player; x++)
win[x] = loss[x] = 0;
}//End of function

//Function to print each round status
void printStanding(int player, int id[], int win[], int loss[])
{
int x;
//Displays the heading
printf(" Player ID Record");
printf(" _________ ________");
//Loops till number of players
for(x = 0; x < player; x++)
printf(" %d %d - %d", id[x], win[x], loss[x]);
}//End of function

//Function to display greeting message
void printGreetig()
{
printf(" Swiss Tournament Manager");
printf(" ______________________________");
}//End of function

//Function to record the win and loss value
void recordRound(int player, int id[], int win[], int loss[])
{
int x;
int res;
//Loops till number of players
for(x = 0; x < player; x+= 2)
{
//Validates user choice for win
do
{
printf(" Who won?");
//Accepts user choice for win
printf(" 1) %d or 2) %d : ", id[x], id[x+1]);
scanf("%d", &res);
//Checks if the res is 1 or 2 then valid
if(res == 1 || res == 2)
{
//If user choice is one
if(res == 1)
{
//Player 1 Win counter is increased by one
win[x]++;
//Player 2 loss counter is increased by one
loss[x+1]++;
}//End of if
//Otherwise
else
{
//Player 2 Win counter is increased by one
win[x+1]++;
//Player 1 loss counter is increased by one
loss[x]++;
}//End of else
break;
}//End of outer if
//For invalid choice
else
printf("Select 1 or 2. Try again");
}while(1);//End of do - while
}//End of for loop
}//End of function

//Function to do selection sort
void selectionSort(int player, int id[], int win[], int loss[])
{
int max, loc, temp, x, y;
//printStanding(player, id, win, loss);
for(x = 0; x <= player - 1; x++)
{
//Stores the x index position value of win as the maximum
max = win[x];
//Stores x position as the maximum location
loc = x;
//Loops till end of players
for(y = x + 1; y <= player - 1; y++)
{
//checks if the win y index position value is greater than or equals to max value
if(win[y] >= max)
{
//Stores the win y index position value as maximum
max = win[y];
//Stores the y value as the index position for the maximum
loc = y;
}//End of if
}//End of for loop
//Checks if location of the maximum is not equal to x index position
if(loc != x)
{
//Swapping process for win
temp = win[x];
win[x] = win[loc];
win[loc] = temp;
//Swapping process for id
temp = id[x];
id[x] = id[loc];
id[loc] = temp;
//Swapping process for loss
temp = loss[x];
loss[x] = loss[loc];
loss[loc] = temp;
}//End of if
}//End of for loop
//Loops till number of players
for(x = 0; x <= player - 1; x++)
{
//Checks if win value of x index position player and next position player is same and x index position id is less than next position id then swap
if(win[x] == win[x+1] && id[x] < id[x+1])
{
//Swapping process for win
temp = win[x];
win[x] = win[x+1];
win[x+1] = temp;
//Swapping process for id
temp = id[x];
id[x] = id[x+1];
id[x+1] = temp;
//Swapping process for loss
temp = loss[x];
loss[x] = loss[x+1];
loss[x+1] = temp;
}//End of if condition
}//End of for loop
}//End of function
//Main function definition
int main()
{
int numberOfParticipants;
int numberOfRounds;
int id[65];
int win[65];
int loss[65];
int x;
//Calls the function to print greeting
printGreetig();
//Calls the function to store number of players
numberOfParticipants = getNumberOfParticipants();
//Calls the function to store number of rounds
numberOfRounds = getnumberOfRounds();
//Calls the function to initialize the arrays
initialize(numberOfParticipants, id, win, loss);
//Loops till number of rounds
for(x = 0; x < numberOfRounds; x++)
{
printf(" Enter the result from round %d: ", (x + 1));
recordRound(numberOfParticipants, id, win, loss);
selectionSort(numberOfParticipants, id, win, loss);
printf(" Standings after round %d: ", (x + 1));
printStanding(numberOfParticipants, id, win, loss);
}//End of for loop

}//End of function

Sample Run 1:


Swiss Tournament Manager
______________________________
Enter number of Participants (must be even): 6

Enter number of rounds to play: 3

Enter the result from round 1:
Who won?
1) 1001 or 2) 1002 : 1

Who won?
1) 1003 or 2) 1004 : 2

Who won?
1) 1005 or 2) 1006 : 1

Standings after round 1:
Player ID Record
_________ ________
1005 1 - 0
1004 1 - 0
1001 1 - 0
1006 0 - 1
1003 0 - 1
1002 0 - 1
Enter the result from round 2:
Who won?
1) 1005 or 2) 1004 : 1

Who won?
1) 1001 or 2) 1006 : 1

Who won?
1) 1003 or 2) 1002 : 2

Standings after round 2:
Player ID Record
_________ ________
1005 2 - 0
1001 2 - 0
1004 1 - 1
1002 1 - 1
1006 0 - 2
1003 0 - 2
Enter the result from round 3:
Who won?
1) 1005 or 2) 1001 : 2

Who won?
1) 1004 or 2) 1002 : 2

Who won?
1) 1006 or 2) 1003 : 2

Standings after round 3:
Player ID Record
_________ ________
1001 3 - 0
1005 2 - 1
1002 2 - 1
1004 1 - 2
1003 1 - 2
1006 0 - 3