Reputation: 31
I'm working with an Eurovision Voting Contest Simulator.
26 countries have to randomly vote other 10 countries (without duplicates nor itself).
So I do a for (Countries.Length) and inside a for (PossibleVotes.Length) For assigning the votes.
Even handling a counter for highest voted (12points) to show 'TheBest' winners.
This is already done.
The code:
//struct array country[26]
//country[].sName = Spain, Italy, France...
//country.iVotes = Total votes
//country.iTwelves = Counter for total12 recieved
//country.iZeros = Counter for total 0 recieved
// iPossibleVotes[10] {12 , 10 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 }
for (int i = 0 ; i < country.Length ; i++){
Console.WriteLine("\n___Country " + (i+1) + " " + country[i].sName + "___");
Console.WriteLine("\nVotes:");
int[] iVotedCountry = Utils.RandomArrayWitoutDuplicates(i);
//Function to make an array of 10 random integers without duplicates nor itself (i value)
//Executes 10 times
for (int j = 0 ; j < iVotedCountry.Length ; j++){
Console.WriteLine("-" + country[iVotedCountry[j]].sName + " with " + iPossibleVotes[j] + " points. ");
//If j = 0 equals to iPossibleVotes[] =12, sum iTwelves counter
if (j == 0){
country[iVotedCountry[j]].iTwelves++;
}
} // END FOR (iVotedCountry.Length) Countries that are being voted.
for (int k = 0 ; k < country.Length ; k++){
if (k != iVotedCountry[j]) {
country[k].iZeros++;
}
}
} //END COUNTRY.Length ARRAY
//Expected output
Console.WriteLine("___TheLooser___\nThe country or countries with more zeros recieved are: " );
//Then sort them in an array
//BUT I can't handle to count the times a country hasn't been voted or voted '0'
Teacher asks to show also, the 'TheLooser' winner, which is the country (or the countries, in a tie) who have been less voted or voted '0'.
My idea is to assign '0' vote to the 16 other countries after I have assigned the real PossibleVotes to the 10 random countries.
Im also interested in pseudo-code abstract ideas or comments to think about it.
Thank you
Upvotes: 2
Views: 347
Reputation: 1327
Instead of keeping the sum of the votes and number of zeros for each country (eg iVotes, iZeroes, iTwelves) consider this approach.
For each country keep all the votes. For example, if there are 10 rounds of voting, then you could have an array of 10 elements, with each element holding the score for that round eg 12, 10, 8 etc, with the default being 0.
Then you have a program in 3 parts.
Record all the votes
Sum the votes for each country and find the one with the highest, taking into account tie
Sum the votes for each country and find the one with the lowest, again, taking ties into account
Part 2 and 3 could be in 1 loop if you like, but cleaner not to be.
Upvotes: 0