0

My code prints all names in the case of a two way/threeway tie, but for some reason I still get an error message in check50 that it does not. The only thing I could think of was maybe it had to do with the placement of the last return but moving it around didn't have much effect. I either got more error messages or just the same two about 2/3 way ties.

    for (int i = 0; i < candidates[i].votes; i++)
    {
        if (candidates[i].votes >= maxvote)
        {
           maxvote = candidates[i].votes;
        }
    }
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes == maxvote)
        {
            winner = candidates[i].name;
            printf("The winner is %s\n", winner);
        }
    }
    return;
***
  • Can you fix your question its quite hard to read and also the strcmp() function returns 0 if the two arguments you gave it is the same the syntax is strcmp(x, y) here you are comparing the values x and y then if it is true it returns 0 so write your if statement as if (strcmp(name, candidates[i].name) == 0). That's why in your error it says too few arguments because your syntax is wrong – Ojou Nii Apr 5 at 17:30
  • Watch Brian's walkthrough of the problem it really helped me to understand where to start – Ojou Nii Apr 5 at 17:34
  • Thank you Ojou Nii, that helped a lot. Now I am having trouble with the print winner section and I am not sure why. I fixed my question as you asked, changed my code, and attempted to find solutions for the new errors I received but I still cannot get my code to compile. Let me know if you need further information, and again I really appreciate the help. – ayeeitsalpha Apr 5 at 23:30
  • Maybe you are at the wrong directory, that's why you cant compile?? – Ojou Nii Apr 6 at 10:55
2
for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes >= maxvote)
        {
           maxvote = candidates[i].votes;
           winner = candidates[i].name;
           printf("The winner is %s\n", winner);
        }
    }

the logic of your code to get the max number of votes is correct, but your logic to get the winner is a little off ok, let's walkthrough it lets say you got this data tt = 5 votes, lip = 2 votes, and jj = 5 votes, in this scenario your output should be both tt and jj right because they have the same number of votes which is 5. So your code goes like this first checks tt who has 5 votes, is 5 greater than or equal to 0 which is your max vote. yes right so the max vote = 5 and the winner = tt so we print tt, then we go look at lip who has 2 votes, is 2 greater than 5? no right so we don't change anything and just look at the next candidate which is jj, jj has 5 votes is 5 greater than or equal to the max vote which right now is 5, it's true right because 5 == 5 so now the maxvote stays 5 and the winner changes to jj and then we print jj. In this scenario its correct right we print both tt and jj, but let's look at another example.. tt = 2 votes, lip = 3 votes, jj = 5 votes. In this scenario jj should be the only winner since he has the most number of votes so lets go through your code, first tt who has 2 votes, is 2 greater than or equal to 0 which is your maxvotes, yes right so max vote = 2 and winner is equal to tt then we print tt, then we check lip who has 3 votes is 3 greater than or equal to 2? yes right so max vote now is = 3 and winner is = lip then we print lip, then lastly we check jj who has 5 votes, is 5 greater than or equal to 3? yes again so maxvote is equal to 5 and winner is equal to jj then we print jj, so your output would be tt\nlip\njj\n... which is wrong right so very slight logic error there hope you get it, tip would be to separate your code for getting the max vote and your code for getting the winner your very close just a few adjustments and your there. Btw just keep asking questions nothings wrong with asking them.. Don't worry I struggled also in this problem and also asked for help so don't feel bad 'cause I was just like you 3 days ago...

| improve this answer | |
  • I separated the print function outside of the for loop to avoid printing incorrect names as it iterates and separated the winner assignment from the max vote assignment. When I separated the winner assignment outside of the for loop 'i' became an undeclared identifier again so I put it in the for loop outside of the if statements brackets to get it to compile but the winner was incorrect. After I attempted to leave winner assignment with the max vote assignment and only moved the print function outside the for loop which led to correct winner but no ties. Then I tried to make an equality – ayeeitsalpha Apr 11 at 22:18
  • equality comparison to register if multiple voters had the same amount of votes as the max vote to print out all names in case of a tie but it still only prints out one winner and I am unsure how the logic should work for that. The only thing I can think of is something like the phonebook example where the elements in the array were given their own identifiers but I don't think it would work since you don't know the number of candidates beforehand. Is this possible to do this way? – ayeeitsalpha Apr 11 at 22:25
  • Here's a way use a for loop to iterate over the candidates[i].votes to find the max number of votes, then create another for loop to iterate over all the candidates and add an if statement to check if the candidates[j].votes == max_votes, then if the statement is true print their name with a new line and that should do it.. – Ojou Nii Apr 12 at 7:15
  • When I do check50 it still gives me the errors that it doesn't print both winners in case of a tie or all three winners but when I run the program it does. At first it would not work correctly and I realized I had to include the print function in the new for loop. After that it worked but would print one of the winners twice but the winners were always correct even with a tie. Then I moved the print function from outside the new if bracket (in the new for loop) to the inside and now there are no extra names and it prints both/all three winners when there is a tie but I still get the error. – ayeeitsalpha Apr 12 at 22:51
  • printf("The winner is %s\n", winner); I think you should remove the "The winner is " in your printf function check50 expects only a certain output so you should just use printf("%s\n", winner); – Ojou Nii Apr 13 at 7:44
1

Your code is incrementing maxvote every time your if statement is true.. The goal is to get the max number of votes and see then check if who are the candidates that have the same number of votes to the max votes you got.

int maxvote = 0;
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes > 0)
        {
        maxvote++;
        }
    }

In this code of yours you initialize maxvote = 0; and you iteriate over the candidates array. lets say the candidates[2, 4, 6, 9].votes has those values right. so lets walkthrough the loop. first i = 0; so we access candidates[0].votes which is = 2; then your if statement says if candidates[i].votes is greater than 0 which is true because 2 is greater than 0 right so now your max_vote is now equal to 1. then we i++ so i is now 1; candidates[i].votes is 4 right so we then check your if statement is 4 greater than 0? yes so maxvote++ again so now maxvote = 2.. is that the correct logic?? You can try to use the debug mode so you can see what's happening to your code line by line.. I'm sorry if I' just giving vague hints but I think the best way you can get good at coding is you actually try to solve it yourself not by copying the code of others so I hope you get my hint.. Just ask me if you have more questions

| improve this answer | |
  • Don't worry I agree. Doing it myself is my preferred way of learning. I asked someone if it was normal to need to see someones code to understand at this level or if I was behind where I should be and he said that it was normal to start like that so you are definitely helping me more than him. My code compiles but now I am printing everyones name rather than just the winner. I would like to try figuring it out for myself using the debug50 before I ask you anything else but i'm not sure how to get it to work, i've only used check50 and style50. Sorry for the trouble and thank you very much! – ayeeitsalpha Apr 6 at 21:57
  • Sorry for the confusion, I have figured out how to run debug50 but all it says is "Usage: plurality [candidate ...]" so I am not sure what exactly I need to fix. – ayeeitsalpha Apr 6 at 22:23
  • that means you're program is returning a non-zero number at initiation. I'd recommend you review the piece of code that has that error in it to try and decipher why you're getting that error. If you still have issues I'd review the notes from this lecture, you can probably search for (int argc, string argv[]) to find the section that will help with this issue – Sean Apr 7 at 5:29
  • It means that when you run this program your prompt has to have more than one arguments. The way this program is we use ./plurality right, and the error says "Usage: plurality [candidate ...] it means that to run this program we must enter the candidates already so, it looks like this ./plurality Alice Bob Charlie, if you run this the candidates of the election will be Alice Bob and Charlie, the candidates are then stored to the string argv[] variable, and you can wherein the index 0 of this array is equal to plurality, index 1 is equal to Alice, index 2 is Bob, and index 3 is Charlie. – Ojou Nii Apr 7 at 7:11
  • You could see at the top of your main function it has an if statement right it says if (argc < 2) argc counts the arguments you give at prompt, so when we run it as ./plurality argc increments by 1 because argc counts plurality as an argument and since argc is only eequal to one that means that the if statement is true so the program terminates there because the if statement is true it prints "Usage: plurality [candidate ...] and returns 1. Keep grinding bro I'm only at pset4... – Ojou Nii Apr 7 at 7:18
0

It's funny, i discovered your error. Your's error is in the 'printf("the winner is %s\n", winner)', i tested just 'printf("%s\n", winner)' and it functioned. Here is my code:

int maxvote=0;
string winner;

for (int i = 0; i < candidates[i].votes; i++)
{
    if (candidates[i].votes >= maxvote)
    {
       maxvote = candidates[i].votes;
    }
}


for (int i = 0; i < candidate_count; i++)
{
    if (candidates[i].votes == maxvote)
    {
        winner = candidates[i].name;
        printf("%s\n", winner);
    }
}

// TODO
return;
| improve this answer | |

You must log in to answer this question.

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