0

I have been doing CS50 week 3's pluarity problem set and I saw very strange output. Here is my code first.

#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
    for (int i = 0; i < candidate_count; i++)
    {

        if (strcmp(candidates[i].name, name) == 0)
        {
            candidates[i].votes = candidates[i].votes + 1;
            return true;
        }
    }

    return false;
}

// Print the winner (or winners) of the election
void print_winner(void)
{
    string winner[candidate_count];
    int max_vote = 0;
    int current_index = 0;

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

            max_vote = candidates[i].votes;
            winner[current_index] = candidates[i].name;
            current_index++;
        }
        else if (i > 0)
        {

            if (candidates[i].votes > max_vote)
            {

                for (int j = 0; j < candidate_count; j++)
                {
                    winner[j] = "0";
                }

                current_index = 0;

                winner[current_index] = candidates[i].name;
                current_index++;
                max_vote = candidates[i].votes;
            }
            else if (candidates[i].votes == max_vote)
            {

                winner[current_index] = candidates[i].name;
                current_index++;
            }
        }
    }

    for (int k = 0; k < current_index + 1; k++)
    {
        if (strcmp(winner[k], "0") != 0)
        {
            printf("Hello %s\n", winner[k]);
        }
    }
}

and I ran the code like this-

~/pset3/plurality/ $ ./plurality Alice Bob Kyaw Soe
Number of voters: 2
Vote: Alice
Vote: Bob

the expected output is -

~/pset3/plurality/ $ ./plurality Alice Bob Kyaw Soe
Number of voters: 2
Vote: Alice
Vote: Bob
Hello Alice
Hello Bob

but the actual result is -

~/pset3/plurality/ $ ./plurality Alice Bob Kyaw Soe
Number of voters: 2
Vote: Alice
Vote: Bob
Hello Alice
Hello Bob
Hello AWL=#(

My question is what does AWL=#( mean? if I use Bob and Kyaw instead of Alice, I got my expected result. What does "Alice" do to the problem internally? Can someone please explain?

WhozCraig
  • 65,258
  • 11
  • 75
  • 141

1 Answers1

0

In print_winner(), your loop

for (int k = 0; k < current_index + 1; k++)

is going out of bounds of the strings you assign. It should be either

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

or

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

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770