0

I am having a lot of difficulty with conceptualizing this function and applying this weeks lessons on algorithms. With the code below, what I have been trying to do is to mimic a selection sort, iterating over each element, and storing in my new variable, winner, on first pass. The program compiles when I test check50 but essentially only counts the Alice vote correctly. Nothing else after that. I am sure that there are many things that could be done better but any hints or suggestions for good next steps would be much appreciated!

#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Max number of candidates
#define MAX 9

// Candidates have name and vote count
typedef struct
{
    string name;
    int votes;
} candidate;

// Array of candidates
candidate candidates[MAX];

// Number of candidates
int candidate_count;

// Function prototypes
bool vote(string name);
void print_winner(void);

int main(int argc, string argv[])
{
    // Check for invalid usage
    if (argc < 2)
    {
        printf("Usage: plurality [candidate ...]\n");
        return 1;
    }

    // Populate array of candidates
    candidate_count = argc - 1;
    if (candidate_count > MAX)
    {
        printf("Maximum number of candidates is %i\n", MAX);
        return 2;
    }
    for (int i = 0; i < candidate_count; i++)
    {
        candidates[i].name = argv[i + 1];
        candidates[i].votes = 0;
    }

    int voter_count = get_int("Number of voters: ");

    // Loop over all voters
    for (int i = 0; i < voter_count; i++)
    {
        string name = get_string("Vote: ");

        // Check for invalid vote
        if (!vote(name))
        {
            printf("Invalid vote.\n");
        }
    }

    // Display winner of election
    print_winner();
}

// Update vote totals given a new vote
bool vote(string name)
{
    for (int i = 0; i < candidate_count; i++)
    {
        if (strcmp(candidates[i].name, name) == 0)
        {
            candidates[i].votes += 1;
            return true;
        }
    }
    return false;
}

// Print the winner (or winners) of the election
void print_winner(void)
{
    int winner = 0;
    int j = 0;
    for (int i = 0; i < candidate_count; i++)
    {

        if (candidates[i].votes > candidate_count / 2)
        {
            printf("%s\n", candidates[i].name);
            break;
        }

        if (candidates[i].votes > 0)
        {
            winner = candidates[i].votes;
        }

        for (j = 0; j < candidate_count; j++)
        {
            if (candidates[j].votes == winner)
            {
                printf("%s\n",candidates[j].name);
            }
            printf("%s\n", candidates[j].name);
        }
    }
    return;
}

1 Answer 1

0

I don't know if this is the proper format to do so, but I wanted answer the question, at least generally. My hint to myself was to look at here my printf functions where in relation to my return function and how this worked in my loops.

As it was recommended to be more specific, I have edited this post for clarity. I found that the first if statement where I was attempting to find a winner by finding the candidate with greater than 50% of the vote. I deleted this. The last printf function I also deleted as I was getting the names of candidates twice. I was left with a loop within a loop that iterated over the candidates twice, once to store the winner and the second to find ties. Hopefully, this will add some clarity!

Again, apologies of this is not the correct way to post if you have arrived at your answer!

1
  • given that you are answering your own question it would make a lot more sense to actually answer your question and specify what the problem was and how to fix it. Your answer is worded unclearly but vaguely seems to amount to "insert some printf statements" which is generic advice that should be the first step before even asking a question. at bare minimum express the exact print statement you used and where placed that showed you your problem.
    – UpAndAdam
    Dec 5, 2023 at 15:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .