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

// Print the winner (or winners) of the election
void print_winner(void)
{    
    string temp ;   
    for( int i=0;i<candidate_count-1;i++)
    {
        for(int j=0;j<candidate_count-1;j++ )
        {
            if(candidates[i].votes>candidates[i+1].votes)
            {
                temp=candidates[i].name;
                candidates[i+1].name=candidates[i].name;
                temp=candidates[i+1].name;        
            }       
        }    
    }
    for( int i=0;i<candidate_count-2;i++)
    {
       if (candidates[i].votes==candidates[candidate_count-1].votes)
       {
           printf("%s\n",candidates[i].name);
        }
    }
    return;
}
1
  • what about it is wrong? Please add context. What is the problem, what results are produced or not produced? Simply posting code with little or no explanation is called a naked code drop and is not permitted on this forum. Please add more details. – Cliff B Apr 6 '20 at 19:40
1

To help facilitate people giving you answers in the future it helps if you can provide more context on the issues you're having. What error you're seeing and what line does it start on. That said you're issues are with your print_winner function.

First Issue: You have two loops but you never use j which your code is wrong or you're second loop is unnecessary.

Second Issue: what you're trying to accomplish in this function is to determine what the highest vote total is and print all the candidates that have a score = to that vote total. Try and re-write this function with that in mind.

You have a few other issues in this function that you should look into as well: you set a variable temp = to something but never use it again, (candidate_count - 2) would mean that the print loop doesn't run if you have only 2 candidates. Good Luck

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .