user11085376
user11085376

Reputation:

How to combine values of two dictionaries without LINQ?

I am creating an express poll program.

I have been instructed to make it work without any LINQ operators.

These are my two dictionaries:

Dictionary<int, string> ballots = new Dictionary<int, string>();
Dictionary<int, int> votes = new Dictionary<int, int>();

This is how I ask the votes:

//Print the ballots as list
Console.WriteLine("Voting Ballots:");
foreach (KeyValuePair<int, string> ballot in ballots)
{
    Console.WriteLine("{0} - {1}", ballot.Key, ballot.Value);
}
//Voting process
Console.WriteLine("\nVote for one of the ballots");
Console.WriteLine("---------------------------------");
Console.WriteLine("Write the number of the ballot you want to vote for: ");
int nVote; int.TryParse(Console.ReadLine(), out nVote);
int val;
string nBallot;
//Verify if the ballot exists
if (planillas.TryGetValue(nVote, out nBallot)){
    Console.WriteLine("You've voted for the ballot {0}", nBallot);
    if (votes.TryGetValue(nVote, out val)) {
        votes[nVote] += 1;
    }
}else{
      Console.WriteLine("The ballot #{0} doesn't exist. \nPlease try again.", nVote);
}

I need to output the results as follows:

BALLOT ID --------- BALLOT NAME ------------ NUMBER OF VOTES

   1      ------  BALLOT NAME   ---------       5

   2      ------  BALLOT NAME   ---------       15

   3      ------  BALLOT NAME   ---------       25

The Ballot ID is a given number, the name as well.

I'm printing the results as follows:

Console.Clear();
Console.WriteLine("VOTING RESULTS: ");
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine("BALLOT ID --------- BALLOT NAME ------------ NUMBER OF VOTES");
List<int> nVotes = votes .Keys.ToList<int>();
foreach (KeyValuePair<int, int> ballot in votes )
{
    Console.Write("{0} ----- ", ballot.Key);
     // I need to print the ballot's name here
    Console.Write("----- {0}", ballot.Value);
}

I tried doing this:

foreach (KeyValuePair<int, int> planilla in votosPlanillas)
{
    Console.Write("\n ---------- {0} ---------- ", planilla.Key);
    if (planillas.TryGetValue(planilla.Key, out nPlanilla)){
        Console.Write("{0} ", nPlanilla);
    }
    foreach (KeyValuePair<int, string> namePlanilla in planillas) {
     Console.Write(" ---------- {0}", planilla.Value);
    }
}

But the result is:

 ----- BALLOT ID ----- BALLOT NAME ----- NUMBER OF VOTES
                                                                                                                                 
 ---------- 1 ---------- a  ---------- 1 ---------- 1 ---------- 1 ---------- 1                                                  
 ---------- 2 ---------- b  ---------- 1 ---------- 1 ---------- 1 ---------- 1                                                  
 ---------- 3 ---------- c  ---------- 3 ---------- 3 ---------- 3 ---------- 3                                                  
 ---------- 4 ---------- d  ---------- 1 ---------- 1 ---------- 1 ---------- 1  

In that result I voted three times for ballot number three. It's working, however... it's printing the dashes (----------) and the number of votes accordingly to the number of ballots-1.

How can I get my desired result?

Upvotes: 3

Views: 108

Answers (1)

Foggzie
Foggzie

Reputation: 9821

Before anything, I'd suggest updating your code to have a Dictionary<int, Ballot> where Ballot is a class with a name and count so you don't need to manage two dictionaries. Regardless...

Your current issue is a foreach loop inside of another foreach loop. If there's guaranteed to be the same keys in both dictionaries, you could just remove the second loop:

foreach (KeyValuePair<int, int> planilla in votosPlanillas)
{
    Console.Write("\n ---------- {0} ---------- ", planilla.Key);
    if (planillas.TryGetValue(planilla.Key, out nPlanilla)){
        Console.Write("{0} ", nPlanilla);
    }

    Console.Write(" ---------- {0}", planilla.Value);
}

Keep in mind that if you don't keep your dictionaries properly synchronized, the if statement will fail and you'll get a report entry with an ID and number of votes but no ballot name.

Upvotes: 3

Related Questions