Vote colour image

This Pset3 took me around 8 hours to complete and my eyes are now slightly fuzzy. Although I spent most of the first 3 hours staring at the terminal and re-watching the videos, in the hope, it would spark some sort of solution, it was quite fun!

Alas, I decided to go with writing the pseudocode from the task. Here was the explanation on how to write “Vote Function”.

  1. vote takes a single argument, a string called name, representing the name of the candidate who was voted for.
  2. If name matches one of the names of the candidates in the election, then update that candidate’s vote total to account for the new vote. The vote function in this case should return true to indicate a successful ballot.
  3. If name does not match the name of any of the candidates in the election, no vote totals should change, and the vote function should return false to indicate an invalid ballot.
  4. You may assume that no two candidates will have the same name.

Here was my pset3 ‘pseudocode’.

1. iterate through the candidates
2. check for name match in string
3. add +1 to counter if matches
4. prints total no. of votes to check if works
5. else return false to stop

Counting The Votes

First, I wanted to iterate through the candidates. So I did a for loop to count through from 0 to total candidate number being the endpoint.

    for (int i = 0; i < candidate_count; i++)

Then, in the lecture, David shows us strcmp which you need the <string.h> library to be able to use. This compares whether first string is the same the second string and if it’s the same it returns 0. Therefor, I used an if statement for IF it did match then I would add +1 to the vote field counter for that specific candidate.

        if (strcmp(name, candidates[i].name) == 0)

Adding +1 to the vote field in the candidate structure.

            candidates[i].votes++;

Added return true; signify successful ballot and return false to signify the voted name does not exist.

Printing The Winner

Lots of googling and StackOverflow was needed for me to complete this part.

To begin with, I wanted to use an insertion sort method as a quick google search showed that it was the quickest method to use for small data. I wanted to put the votes in order from smallest to highest and take the highest votes as the winners.

Further research into how to use insertion sort method using C produced some confusing results for pre-written algorithm templates. I couldn’t really get my head around how I would replace the algorithm templates lol so I decided to go to StackOverflow and found some dude who had written this pseudocode.

1. Declare a maxvote int set to 0.
2. Iterate over the list of candidates and check the number of votes. 3. If that number is greater than the maxvote
4. Update the maxvote to that number. 
5. Once the loop finishes, you now have maxvote.
6. Next, again iterate over the list of candidates and print (as you go) each candidate who has that maxvote number of votes.

This helped SO MUCH! So here’s how I did it:

I declared maxvote as 0.

    int maxVote = 0;

Iterate over the list of candidates and check the number of votes.

    for (int k = 0; k < candidate_count; k++)

Check if the current candidate no. of votes is greater than the maxvote

        if (candidates[k].votes > maxVote)

If it is then we update the max vote to that number. So that we know that’s the largest no. of votes in the system. This keeps happening until we’ve gone through all candidate vote totals to check.

 maxVote = candidates[k].votes;

Then we iterate over the list of candidates one-by-one to check their total vote against the maxVote.

for (int l = 0; l < candidate_count; l++)

We then print each candidate who has that maxvote number of votes.

            printf("%s\n", candidates[l].name);

And there you have it pset3 in a nutshell! I have added the final code to the end of this post so you can check it. Remember to write YOUR OWN code or at least understand it as the whole system is based on honesty.

If anyone is interested I borrow this book: beginner’s guide book for C Programming from the library. It goes from absolute beginner through to the current level we are now to probably beyond. It has been super helpful to flick through and reinforce some of the stuff learned in the lectures in a new way (like pointers). David Maylan also recommends this book at the start of the program.


If you found this post useful please support House Ninety Two: Buy Me a Coffee at ko-fi.com