Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

Random draw for a competition.

★☆☆

Competitions often include a first round where teams are drawn against each other randomly.

Cup draw

Make

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.

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the subprogram called input_teams that:

  1. Reads two team names from the user.
  2. Appends the two teams to a list of teams.
  3. Reads a yes/no choice for whether to enter two more teams.
  4. If the user inputs "y" the process of reading and appending teams is repeated until the user inputs "n".

Complete the subprogram called draw_teams that:

  1. Shuffles the teams.
  2. Outputs a message, The draw is:.
  3. Outputs who plays whom.

Complete the main program so that:

  1. An empty list called teams is declared.
  2. The input_teams procedure is called.
  3. The draw_teams procedure is called.

Typical inputs and outputs from the program would be:

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
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
// 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>