0

I have written a function, print2d(), to print out a 2D array. I am using it to print out the preferences array after calling record_preferences() in main according to the provided template. The output of the print2d function is different from the result of debugging using debug50. The record_preferences function has passed all tests, so the problem must be in print2d(). As you can see, I have called print2d() after record_preferences(), so it seems to me that other functions called after print2d() that deal with preferences[] are not affecting it.

In main():

record_preferences(ranks);
print2d(candidate_count,candidate_count, preferences);
// Update preferences given one voter's ranks
void record_preferences(int ranks[])
{
    for (int rank = 0; rank < candidate_count - 1; rank++)
    {
        // current candidate with rank over candidate with rank+1  

        int winnerIndex = ranks[rank];

        for(int lowerRank = rank + 1; lowerRank < candidate_count; lowerRank++)
        {
            int loserIndex = ranks[lowerRank];

            preferences[winnerIndex][loserIndex]++;
        }
    }
}

void print2d(int rows, int cols, int array[][cols])
{
    printf("{");

    for (int r = 0; r < rows; r++)
    {
        printf("(%i",array[r][0]);
        for (int c = 1; c < cols; c++)
        {
            printf(", %i",array[r][c]);
        }
        printf(")\n");
    }
    printf("}\n");
}

The result of entering:

./tideman a b c
Number of voters:1
Rank 1: c
Rank 2: a
Rank 3: b

{(0, 1, 0) // result of print2d
(0, 0, 0)
(0, 0, 0)
}

print2d should print out

{(0, 1, 0) 
(0, 0, 0)
(1, 1, 0)
}

Thank you!

EDIT:

// Update ranks given a new vote
bool vote(int rank, string name, int ranks[])
{
    for (int i = 0; i < candidate_count; i++)
    {
        if (strcmp(candidates[i],name) == 0)
        {
            ranks[rank] = i;
            return true;
        }
    }
    return false;
}

  • looks to me that your array doesn't contain the correct data. I'd look at all the code that populates the 2d array. Also, if you're getting different results when you run it inside debug50 and just on the command line, then I would suspect some memory is being overwritten somewhere. Can you post your code please? – Cliff B Mar 30 at 23:03
  • Hi @CliffB, I've added the record_preferences() function. If this isn't enough code, I would gladly post some more. Thanks for taking a look at this! – monkey king Mar 31 at 23:02
0

first off, you don't have to pass candidate_count into the function. You have access to that variable inside the function without passing it in (just like in the function above it).

second, I think you're issue lies in record_preferences...when you say that rank < candidate_count - 1, what you're saying is that you can never record preferences for your final candidate in the array

That said, there could be additional issues with your vote function as well, but without seeing that it's hard to tell

| improve this answer | |
  • Hi @Sean, I designed print2d with the intention of using it for different 2D arrays, not just preferences[]. In record_preferences, I wrote rank < candidate_count - 1, because the candidate of lowest preference does not win over anybody. As a result, I thought that that candidate did not need to be recorded in preferences. Am I wrong? Anyways, thanks for looking at this. I will post the vote() function too. – monkey king Apr 8 at 21:45
  • Sorry I meant for that last to be recorded as a comment not an answer (new to commenting on stack). Also, you are correct, I had my code bases mixed up when I made my previous comment. both ways yield the same result but -1 is more efficient. Your issue lies in the way you're passing the array into the function. when you pass the array in as (..., array[][MAX]) it prints out the correct results. I believe the issue is caused by the fact that you're passing in a 9x9 array (even if most of those are empty) but the function is expecting an array with a max # of cols of 3 – Sean Apr 10 at 1:24

You must log in to answer this question.

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