.Net Programming - Financial independence

Collections.Generic in Visual Basic/C# – Exercise and Solution

Assignment

In this exercise we will create a small race winner grid in a Dictionary. The position will be associate with the player name.

1. Create a Dictionary containing Integer as keys and String values
2. Add the following values: (1, “Player 5”), (2, “Player 1”), (3, “Player 3”), (4, “Player 2”) and (5, “Player 4”). This will be our race final positions.
3. Display the number of players in the console.
4. Replace Player 5 at position 1 by Player 4 at position 5 so that Player 4 is now the winner and Player 5 is now the loser.
5. Display all the players with their respective position in the console.
6. Clear all the data in the Dictionary

You have the choice of your language for this exercise as the solution will be posted tomorrow in both programming languages!

Good luck!

Solution

Here is the solution for the exercise on Collections.Generic in Visual Basic and C#. You will see minor differences but both languages are essentially the same.

Visual Basic

Module Program
    Sub Main()
        '1. Create the Dictionary
        Dim Positions = New Dictionary(Of Integer, String)

        '2. Add values
        Positions.Add(1, "Player 5")
        Positions.Add(2, "Player 1")
        Positions.Add(3, "Player 3")
        Positions.Add(4, "Player 2")
        Positions.Add(5, "Player 4")

        '3. Display the number of racers
        Console.WriteLine("There are {0} positions in the grid.", Positions.Count)

        '4. Replace the player at position 5 with the one at position 1
        Positions.Remove(1)
        Positions.Remove(5)

        Positions.Add(1, "Player 4")
        Positions.Add(5, "Player 5")

        '5. Display all players
        For Index As Integer = 1 To Positions.Count Step 1
            Console.WriteLine("{0} arrived at position {1}.", Positions(Index), Index)
        Next

        '6. Clear the Dictionary
        Positions.Clear()
        Console.ReadKey()
    End Sub
End Module

C#

using System;
using System.Collections.Generic;

namespace Exercise4CSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            //1. Create the Dictionary
            Dictionary Positions = new Dictionary();

            //2. Add values
            Positions.Add(1, "Player 5");
            Positions.Add(2, "Player 1");
            Positions.Add(3, "Player 3");
            Positions.Add(4, "Player 2");
            Positions.Add(5, "Player 4");

            //3. Display the number of racers
            Console.WriteLine("There are {0} positions in the grid.", Positions.Count);

            //4. Replace the player at position 5 with the one at position 1
            Positions.Remove(1);
            Positions.Remove(5);

            Positions.Add(1, "Player 4");
            Positions.Add(5, "Player 5");

            //5. Display all players
            for (int Index = 1; Index <= Positions.Count; Index++)
                Console.WriteLine("{0} arrived at position {1}.", Positions[Index], Index);

            //6. Clear the Dictionary
            Positions.Clear();
            Console.ReadKey();
        }
    }
}

Remarks

For the number 4, I decided to remove the elements and insert them back. I could have Get both and then stored them in variables and finally reinsert them at their new position. Your call here, as long as the end result is the same!

I used a For loop for the number 5 of this exercise, I made this choice because I know how many loops I will have to make before all the elements are displayed, the Count parameter came handy.

Other than that I don’t think there are any other difficulties in this problem, if you wish to ask a question about this exercise, feel free to post it in the comments! Don’t give up!

YouTube

Next article Classes and Object Oriented Programming in Visual Basic/C#
Previous article The Stack Data Structure in Visual Basic/C#

Related posts

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Comment

Your data will be safe! Your e-mail address will not be published. Also other data will not be shared with third person. Required fields marked as *