Random draw for a competition.
★☆☆Competitions often include a first round where teams are drawn against each other randomly.
Write a program that allows the user to enter any number of team names. The computer shuffles the teams and then outputs all the team names two at a time. If you want an odd number of teams, enter “bye” as one of the teams so that one team gets an automatic place in the second round. You only need to simulate the draw for round one.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
input_teams that:"y" the process of reading and appending teams is repeated until the user inputs "n".draw_teams that:The draw is:.teams is declared.input_teams procedure is called.draw_teams procedure is called.Enter the name of a team:
England
Enter the name of a team:
Germany
Add two more teams? y/n :
y
Enter the name of a team:
Spain
Enter the name of a team:
Italy
Add two more teams? y/n :
n
The draw is:
Spain v Germany
England v Italy
// Cup draw program
using System;
using System.Collections.Generic;
class Submission
{
---
// -------------------------
// Globals
// -------------------------
static Random random_generator = new Random();
// -------------------------
// Subprograms
// -------------------------
// Add teams to a list in pairs
static void input_teams(List teams)
{
---
string answer = "y";
while (answer != "n")
{
---
Console.WriteLine("Enter the name of a team:");
string team1 = Console.ReadLine();
teams.Add(team1);
Console.WriteLine("Enter the name of a team:");
string team2 = Console.ReadLine();
teams.Add(team2);
Console.WriteLine("Add two more teams? y/n :");
answer = Console.ReadLine();
---
}
---
}
---
// Shuffle (Fisher-Yates)
static void shuffle(List list)
{
---
int n = list.Count;
while (n > 1)
{
---
n--;
int i = random_generator.Next(n + 1);
string value = list[i];
list[i] = list[n];
list[n] = value;
---
}
---
}
---
// Draw teams to play each other randomly
static void draw_teams(List teams)
{
---
shuffle(teams);
Console.WriteLine();
Console.WriteLine("The draw is:");
for (int i = 0; i + 1 < teams.Count; i += 2)
{
---
Console.WriteLine($"{teams[i]} v {teams[i + 1]}");
---
}
---
}
---
// -------------------------
// Main program
// -------------------------
public static void Main(string[] args)
{
---
List teams = new List();
input_teams(teams);
draw_teams(teams);
---
}
}
</pre>
</dw-parsons-puzzle>
</details>