0

I am so close to a successful runoff program, but I've been having issues with the tabulation section. I decided that this was a good place to implement a recursive function, but it is a new concept to me so maybe I'm missing something. The rest of my code is perfectly functional. The only check50 error I am getting is this:

:( tabulate counts votes when multiple candidates are eliminated tabulate function did not produce correct vote totals

My code is as follows:

void tabulate(void)
{
    for (int voter = 0; voter < voter_count; voter++)
    {
        int rank = 0;
         elim_test(voter, rank);
    }
    return;
}

//recursive function for tabulating non-eliminated candidates
void elim_test(int voter, int rank)
{
    if (candidates[preferences[voter][rank]].eliminated == false)
    {
        candidates[preferences[voter][rank]].votes++;
        return;
    }
    elim_test(voter, rank++);
}

What am I missing here?

1

rank++

This means "tell me the value of rank, then add one to rank". So in the last line of elim_test, you're calling elim_test again with the exact same values over and over again.

To fix this, you can either use ++rank, which means "add one to rank, then tell me the value of rank", or you can write elim_test(voter, rank + 1);, since there's no reason to change the value of rank.

| improve this answer | |
  • This was exactly the problem, thank you so much! I need to be more careful about studying the definitions of operators. – Max Lunifeld Jun 18 at 22:06
  • Happy to help! Also consider looking more into the concept of scope. You're treating rank like you would if it was global, but it's actually multiple variables in multiple locations that all have the same name, but can have different values. There's rank in tabulate, and there's a new rank every time you call elim_test, and all of those can be different – Llenk Jun 18 at 22:51
0

First of all your nesting of loops isn't correct, recursive functions can only be called inside a parent function.

Now if you want to make your function recursive then you should use int as a global variable outside the{ } inside your function

You don't need to use recursion as in my case I approached this problem without any typical syntaxes and recursion.

Anyways it depends upon you how you think and how you solve (not mine)

| improve this answer | |

You must log in to answer this question.

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