lexi
lexi

Reputation: 1

CS50 Tideman code passes check50 fully but isn't printing the winner correctly

I've just "finished" tideman, and although check50 passes me, it's definitely not printing the winners correctly. Has anyone else had this issue?

This is my print winner code -

// Print the winner of the election
void print_winner(void)
{
    //array of total "trues" in locked[i][j] by candidate i.e. total edges
    int sum_source[candidate_count]; 
    int source;

    for (int i = 0; i < candidate_count; i ++)
    {
        source = 0;
        for (int j = 0; j < candidate_count; j ++)
        {
            if (locked[i][j] == true)
            {   
                source ++;
            }    
        sum_source[j] = source;    
        }
        // sum_source[i] = source; I thought sum_source should come here to sum by candidate i 
        //but this causes check 50 to fail print winner when pairs tied
    }

    int max_source = sum_source[candidate_count - 1]; //sets max number of trues by candidate to last candidate

    for (int k = 0; k < candidate_count - 1; k ++)
    {
        if (sum_source[k] > max_source) //reset max_trues for higher number of trues
        {
            max_source = sum_source[k];
        }
    } 


    for (int l = 0; l < candidate_count; l ++)
    {
        if (sum_source[l] == max_source)
        {
            printf("%s\n", candidates[l]); //print all candidates with number of trues = max_source as winners
        }
    }
    return;
}

My output is as follows: For some reason it's printing after every vote and just printing out the votes. Correct output in this case should just be "a" however check50 is passing me.

:) print_winner prints winner of election when one candidate wins over all others :) print_winner prints winner of election when some pairs are tied To see the results in your browser go to https://submit.cs50.io/check50/26f6f9fe89617259dc60658b35957549f52a8e2a

~/pset3/tideman/ $ ./tideman a b c

Number of voters: 2

Rank 1: a

Rank 2: b

Rank 3: c

a

b

c

Rank 1: a

Rank 2: b

Rank 3: c

a

b

c

Any ideas why check50 is not failing me and also what I'm doing wrong in code that's causing printing after each vote and not actually printing winner?

Upvotes: 0

Views: 1131

Answers (1)

Megazord
Megazord

Reputation: 51

I think you could get away with a simpler approach. Since what you are looking for is just the candidate whose column in locked has only false values (no one won against him). And since c stores boolean values as 0 and 1, that is like saying that the total sum of the column equals 0. You can easily test it like this:

// Print the winner of the election
void print_winner(void)
{
    // Create an array of int called sum and populate it with zeroes
    int sum[candidate_count];
    for (int i = 0; i < candidate_count; i++)
    {
        sum[i] = 0;
    }
    // Take the cumsum of each column and return the winner as soon as it's 0
    for (int i = 0; i < candidate_count; i ++)
    {
        for (int j = 0; j < candidate_count; j ++)
        {
            sum[i] += locked[j][i];
        }
        if (sum[i] == 0)
        {
            printf("%s\n", candidates[i]);
            return;
        }
    }
    return;
}

Upvotes: 0

Related Questions