0

Objective: Print the name/s with the highest votes

So I was debugging my PSET3 Plurality for probably 8+ hours now. I thought everything was fixed since the result in the debugger was what I intended, but, in my integrated terminal, it produced the result differently.

Debugger result:

plurality/ $ debug50 plurality ./plurality me duck you

.

Number of voters: 7
Vote: me
Vote: me
Vote: me
Vote: d
Invalid vote.
Vote: duck
Vote: duck
Vote: you
me

Integrated Terminal result:

plurality/ $ ./plurality me duck you
Number of voters: 7
Vote: me
Vote: me
Vote: me
Vote: d
Invalid vote.
Vote: duck
Vote: duck
Vote: you
duck
plurality/ $ 

"me" should win right, but why did "duck" did?

My Code(I thought of making a minimal reproducible example, but I don't know how to, that's the most minimal I could think of, I'm sorry. Just scroll all the way down.):

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

//Swapping Function
void swap (char ** xptr, char ** yptr);

// 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 += 1;
            return true;
        }
    }
    return false;
}

This section might help you...

// Print the winner (or winners) of the election
void print_winner(void)
{
    // TODO

    for (int z = 0; z < candidate_count - 1; z++)
    {
        int n = 0;
        while (n < candidate_count - 2)
        {
            int a = candidates[n+1].votes;
            int b = candidates[n+2].votes;
            char *x = candidates[n+1].name;        //me
            char *y = candidates[n+2].name;        //duck
            if (a > b)
            {
                a = a + b;                          //Swapping techniques credits to GeekforGreeks.org & Childre'sTechVideo
                b = a - b;
                a = a - b;
                swap(&x, &y);
            }
            candidates[n+1].votes = a;
            candidates[n+2].votes = b;
            candidates[n+1].name = x;
            candidates[n+2].name = y;
            n++;
        }
    }
    int c = 0;
    while (c != candidate_count - 1)
    {
        c++;
        if (candidates[candidate_count-1].votes == candidates[c].votes)
        {
            printf("%s\n", candidates[c].name);
        }
    }
}

//Swap function
void swap (char ** xptr, char ** yptr)
{
    char * tmp = NULL;
    tmp = *xptr;
    *xptr = *yptr;
    *yptr = tmp;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Onelad
  • 3
  • 3
  • @SupportUkraine the candidates[0] is argv[1], so that's basically the ./plurality – Onelad Aug 03 '22 at 09:39
  • @SupportUkraine Will look into that qsort thing. I'm still a beginner, go easy on me :> – Onelad Aug 03 '22 at 09:41
  • @Onelad According to your OP, in the "Integrated Terminal" version, argv[1] would be the 1st candidate, not the name of the executable... – Fe2O3 Aug 03 '22 at 09:46
  • @Fe2O3 Oh I just realized something, I made the wrong syntax for debug... it's suppose to be like (ie. “debug50 ./tideman …..” so on). Thx. – Onelad Aug 03 '22 at 09:57
  • @SupportUkraine you're kinda right lol. – Onelad Aug 03 '22 at 10:03
  • @Gerhardh xD That's what I saw in my debugger. I thought that's a part of CS50's pre-made solution, but nah. plurality/ $ debug50 plurality ./plurality me duck you basically means what you've said – Onelad Aug 03 '22 at 10:10
  • 1
    Update, the code is finally fix yall, have a great day & thx. – Onelad Aug 03 '22 at 10:22
  • @Onelad Hi, glad to know you've found the solution to resolve this issue! Please consider adding an answer and changing its status to Answered. See [can I answer my own question..](https://stackoverflow.com/help/self-answer), Just a reminder :) – Jingmiao Xu-MSFT Aug 04 '22 at 03:20
  • @JingmiaoXu-MSFT I humbly thank your wise reminder, but I apologize, I would have to decline your offer. – Onelad Aug 09 '22 at 08:13

1 Answers1

0

It isn't necessary to attempt to sort... Just find the highest value and print as many candidates as have attained that amount.

int maxVotes = -1;
for (int z = 0; z < candidate_count; z++)
    if( candidates[z].votes > maxVotes )
        maxVotes = candidates[z].votes;

for (int z = 0; z < candidate_count; z++)
    if( candidates[z].votes == maxVotes )
        printf( "%s - %d votes\n", candidates[z].name, candidates[z].votes );
Fe2O3
  • 6,077
  • 2
  • 4
  • 20
  • I thought of something similar to that, but I think I'll be needing 'sorting' in my next problem set, 'Tideman'... nice solution though. – Onelad Aug 03 '22 at 09:35