ccs50

Why does this code get marked as not working? CS50 Plurality


I'm doing the cs50 basic course task plurality. Now I have the problem that all input I used got the right output, but the cs50 correctness check says otherwise.

Is this because I don't use a sorting algorithm as suggested or is the code flawed in any other way?

This is my code:

#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;

candidate winners[MAX];
int winner_count = 0;
// 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++)
    {
        winners[i].votes = 0;
        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) == false)
        {
            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++;
            if(candidates[i].votes > winners[0].votes){
                winners[0] = candidates[i];
                winner_count = 1;

            }else if(candidates[i].votes == winners[0].votes){
                winners[winner_count] = candidates[i];
                winner_count++;

            }

            return true;


        }
    }
        return false;

}

// Print the winner (or winners) of the election
void print_winner(void)
{
    for(int i = 0; i < winner_count; i++){
        printf("%s\n", winners[i].name);
    }
    // TODO
    return;
}

Solution

  • Your code seems functional but in case of identical vote counts, might not output the winners' list in the expected order. This should not be considered an error, unless the requirements specifically require the same order.

    You can try and implement a simpler algorithm where you only keep track of the maximum vote during the voting phase and iterate through the candidates array to output the winners' list in the same order as the command line arguments.

    Here is a modified version:

    #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];
    int max_votes = 0;
    
    // 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) == false) {
                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++;
                if (max_votes < candidates[i].votes)
                    max_votes = candidates[i].votes;
                return true;
            }
        }
        return false;
    }
    
    // Print the winner (or winners) of the election
    void print_winner(void) {
        for (int i = 0; i < candidate_count; i++) {
            if (candidates[i].votes == max_votes)
                printf("%s\n", candidates[i].name);
        }
    }