# For Loops

• ### Question

• Hi People,

I'm trying to create a basic Tournament bracket and I need a way of checking which numericalUpDown has the highest number. However this must be done in pairs e.g. did player 1 score more than 2, did player 3 score more than 4 etc. any help?

Sorry also must be in C#

Regards

Dave

• Edited by Wednesday, June 26, 2013 9:51 AM
Wednesday, June 26, 2013 9:47 AM

• Use a good old buble sort algorithm.  Look at my indexes very carefully.  'j' starts at i + 1.  We don't need to compare each player twice and we don't need to compare each player with itself.  Also notice i stops at number of player - 2 and j stops at number of players - 1.

You can think of the comparison as a square table with rows and columns.  You only have to compare half the table and you don't have to compare the cells along the diagnol (comparing a player against itself).

```            List<int> players = new List<int>();
for (int i = 0; i < players.Count - 2; i++)
{
for (int j = i + 1; j < players.Count - 1; j++)
{
if (players[i] < players[j])
{
}
}
}```

jdweng

Wednesday, June 26, 2013 10:20 AM
• You haven't described your situation in much detail. I'm going to assume that you have a single elimination tournament and that you have arrays of TextBoxes to store the players names and arrays of NumericUpDown to store scores.  The following snippet assumes a tournament of 16 players, it simply declares 4 x 16 arrays of controls (four rounds with a maximum of 16 players in a round).  Presumably you have either defined the controls in the designer or you create them in your code.  When Button1 is clicked, the scores from the current round are processed and the names of the winners are copied into the TextBoxes for the next round.

```private TextBox[,] names = new TextBox[4, 16];
private NumericUpDown[,] scores = new NumericUpDown[4, 16];
private int round;

public void Button1_Click(object sender, EventArgs e)
{
int roundPlayers = Convert.ToInt32(Math.Pow(2, (4 - round)));
for (int i = 0; i < roundPlayers; i += 2)
{
int winner = (scores[round, i].Value > scores[round, i + 1].Value) ? i : i + 1;
names[round + 1, i / 2].Text = names[round, winner].Text;
}
round += 1;
}```

Wednesday, June 26, 2013 12:04 PM
• Hi ,from your description, i know that you have a set of score that need to sort in the  Tournament,so i would like to suggest you to use insert sort.

Thursday, June 27, 2013 7:16 AM

### All replies

• Use a good old buble sort algorithm.  Look at my indexes very carefully.  'j' starts at i + 1.  We don't need to compare each player twice and we don't need to compare each player with itself.  Also notice i stops at number of player - 2 and j stops at number of players - 1.

You can think of the comparison as a square table with rows and columns.  You only have to compare half the table and you don't have to compare the cells along the diagnol (comparing a player against itself).

```            List<int> players = new List<int>();
for (int i = 0; i < players.Count - 2; i++)
{
for (int j = i + 1; j < players.Count - 1; j++)
{
if (players[i] < players[j])
{
}
}
}```

jdweng

Wednesday, June 26, 2013 10:20 AM
• You haven't described your situation in much detail. I'm going to assume that you have a single elimination tournament and that you have arrays of TextBoxes to store the players names and arrays of NumericUpDown to store scores.  The following snippet assumes a tournament of 16 players, it simply declares 4 x 16 arrays of controls (four rounds with a maximum of 16 players in a round).  Presumably you have either defined the controls in the designer or you create them in your code.  When Button1 is clicked, the scores from the current round are processed and the names of the winners are copied into the TextBoxes for the next round.

```private TextBox[,] names = new TextBox[4, 16];
private NumericUpDown[,] scores = new NumericUpDown[4, 16];
private int round;

public void Button1_Click(object sender, EventArgs e)
{
int roundPlayers = Convert.ToInt32(Math.Pow(2, (4 - round)));
for (int i = 0; i < roundPlayers; i += 2)
{
int winner = (scores[round, i].Value > scores[round, i + 1].Value) ? i : i + 1;
names[round + 1, i / 2].Text = names[round, winner].Text;
}
round += 1;
}```

Wednesday, June 26, 2013 12:04 PM
• Hi ,from your description, i know that you have a set of score that need to sort in the  Tournament,so i would like to suggest you to use insert sort.

Thursday, June 27, 2013 7:16 AM