简体   繁体   中英

CS50 Plurality PSET3 - Code seems to work but check50 says otherwise

So the program is supposed to output the winning candidates of the known election type called plurality vote/ "winner takes all" through command-line args (candidate names). From what I've seen and personally compiled and such, it does just that. If there is a tie, it outputs all the tied candidates and if there is only a single winner, it outputs their name. When I run it through check50 for testing, it gives me the errors found in this image . The assignment begins on creating the functions "vote" and "print_winner". I would greatly appreciate someone who could forward me in the right direction:) Thank you

    #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)
{
    // TODO | DONE
    for (int i = 0; i < candidate_count; i++)
    {
        if ( strcmp(candidates[i].name, name) == 0)
        {
            candidates[i].votes++;
            return true;
        }
    }
    return false;
}

// Print the winner (or winners) of the election
void print_winner(void)
{
    int largest_vote = candidates[0].votes;
    int largest_vote_ids[candidate_count];
    bool tie_toggle = false;
    
    // Goes through candidate votes and checks whether or not there is a tie
    for (int i = 1; i < candidate_count; i++)
    {
        // Default winner if there is only 1 candidate
        if (candidate_count == 1)
        {
            printf("%s\n",candidates[0].name);
            return;
        }
        if (candidates[i].votes == largest_vote)
        {
            tie_toggle = true;
            // Stores candidate index as a tied competitor
            largest_vote_ids[i] = i;
        }
        // DISCARD (LOW VOTE)
        if (candidates[i].votes < largest_vote)
        {
            largest_vote_ids[i] = 100;
        }
        // NEW MAX VOTE
        if (candidates[i].votes > largest_vote)
        {
            tie_toggle = false;
            for (int a = 0; a < candidate_count; a++)
            {
                // Clears list of tied candidates
                largest_vote_ids[a] = 100; // 100 is considered "empty" - Reset list
            }
            // Stores candidates index as the current highest voted 
            largest_vote_ids[0] = i;
            largest_vote = candidates[i].votes;
        }
        
    }
    if (tie_toggle)
    {
        for (int i = 0; i<candidate_count; i++)
        {
            // For every tied candidate in the list, print their name (the int 100 refers to an empty element of the list)
            if (largest_vote_ids[i] != 100) 
            {
                printf("%s\n", candidates[largest_vote_ids[i]].name);
            }
        }
    }
    else
    {   // Print the winning candidate (solo win)
        printf("%s\n", candidates[largest_vote_ids[0]].name);
    }
    return;
}

So I took a much more simple approach and it worked out which took seconds, still really confused why the former code didn't work but oh well. Learned the importance of simplicity.

(Instead of making a list with possible tied candidates, I just went through each candidate and checked to see if their vote was equivalent to the highest vote and printed accordingly)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM