0

[![enter image description here][1]][1]

I keep getting a tabulation error with check50 that I cannot replicate

11 voters, 4 candidates, 3 or 4 rounds of voting works fine with I run it, and debug50 doesn't show anything either.

After a candidate is eliminated, I cycle voter preferences left (pref[j][0] = pref[j][0]) until there is a valid candidate in the [j][0] position, and then retabulate.

here is the change voter preference code and the tabulate code. i realise recursion be employed somewhere in this problem but I kept with loops.

int n = 0;
        for (int i = 0; i < candidate_count; i++)
        {
            if (candidates[i].eliminated == true)
            {
                for (int j = 0; j < voter_count; j++)
                {
                    if (preferences[j][0] == i)
                    {
                        do
                        {
                            for (int v = 0; v < candidate_count; v++)
                            {
                                preferences[j][v] =  preferences[j][v + 1];
                            }
                            n = preferences[j][0];
                        }
                        while (candidates[n].eliminated == true);
                    }
                }
            }
        }
        // Reset vote counts back to zero
        for (int i = 0; i < argc - 1; i++)
        {
            candidates[i].votes = 0;
        }
    }
```

```
void tabulate(void)
{
    //iterate through the candidates
    for (int i = 0; i < candidate_count; i++)
    {
        //identify remaining candidates
        if (candidates[i].eliminated == false)
        {for (int j = 0; j < voter_count; j++)
            {
                if (preferences[j][0] == i)
                {
                    candidates[i].votes += 1;
                }
            }
        }
    }    
       
    return;
}```


  [1]: https://i.stack.imgur.com/1qoF6.png
0

If I'm understanding what you're doing correctly, the first chunk of code is somewhere in eliminate. What you're doing may well work correctly in the full program, but check50 doesn't test your full program for this problem. It tests the individual methods. (To test this, you can comment out all but one of the functions, and run check50. You should pass all the tests for that function, even if the program would never get that far if you ran the whole thing.) So you can't depend on preferences looking like it would in your program. When check50 runs, preferences looks like it does in the version that the CS50 people decided it should look like, which is what it looks like after vote runs.

What this means for you is that you should remove the code where you edit preferences, and do all the checking you need in tabulate, or another function you call from tabulate.

  • That makes a lot of sense. The instructions didn't expllictily say to not change main (whereas with plurality and tideman they do), and I had added something to main, which explains why I could NOT get the error when i ran tests. I have actually fixed it simplify what was in the tabulate() function and remove the code from main. in retrospect, it was probbaly only the latter that was causing teh problem. now I know! thanks. Hours wasted hunting down the "bug". – Christine Jul 7 '20 at 23:21

You must log in to answer this question.

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