-1

I can not figure out why the values of voterFavorites are not changing here. Are arrays in c not referenced when returning from a function? I am planning on using voterFavorites in another method when determineWinner returns.

int determineWinner( int tallyCard[], int maxVotes, int numberOfRestaurants,int **voterFavorites) {
int x, y, z, zz, max = -999, min = 999, maxIndex, percent; 

for ( x = 0 ; x < numberOfRestaurants ; x++ ) { 
    if (tallyCard[x] > max) {
        maxIndex = x;
        max = tallyCard[x];
        printf("%i maxIndex; %i max\n", maxIndex, max);
    } 
    if (tallyCard[x] < min) {
        if (tallyCard[x] != 999) {
            min = tallyCard[x];
        }
    }
}

percent = max * 100 /maxVotes;
printf("%i percent \n", percent);
if ( percent >= 50) {
    return maxIndex;
} else {
    for(x = 0; x < maxVotes ; x++) {
        for (y = 0; y < 4; y++) {
            if (voterFavorites[x][y] == min) {
                voterFavorites[x][y] = 999;
            }
        } 
    }
    return -444; // arbitrarily small number
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
dopps
  • 1
  • 4
  • A pointer to a pointer is not the same as a two-dimensional array – Joel Feb 04 '14 at 03:07
  • Welcome to Stack Overflow. Please read the [About] page soon. The problem is not self-evidently in this code, which means it is probably in the calling code. Please show how you are allocating the array of pointers and initializing it. Please also assure us that you are compiling with stringent warnings enabled and that your code compiles with no warnings (o/s, compiler). Note that `z` and `zz` are unused, and `maxIndex` can be used without being initialized, and your code is missing the final close brace of the function — if you indented systematically, it would be easy to spot such problems. – Jonathan Leffler Feb 04 '14 at 03:11

1 Answers1

1

I can not figure out why the values of voterFavorites are not changing here.

This is an opportunity to learn more about how to do runtime debugging. You should seek to partition the problem space into answers to two major questions:

  1. Am I executing the code that I expect (the assignment to voterFavorites[x][y])?
    ... and if so...
  2. Why is it when I iterate over these values from the calling frame, I'm not able to see the changes?"

To answer the first question, you should use a debugger and set a breakpoint on the statement in question. If it's never encountered, it's likely that the conditional(s) leading up to it are not as you expect.

To answer the second question, you should note the virtual memory addresses of the base of the arrays you're using and the specific address of elements you expected to receive an assignment. Compare those addresses with the ones which appear in the calling frame or other code which accesses the arrays.

I suspect that you will learn that the 2D array is not what you expect it to be, either because it was not allocated or initialized correctly.

Brian Cain
  • 14,403
  • 3
  • 50
  • 88
  • This would not have fit as a comment. But I recognize that this is not an adequate answer, so I plan to delete it after a few downvotes or after the OP has read it. – Brian Cain Feb 04 '14 at 03:15
  • I've been gdb-ing it for a while and I get an infinite loop from the calling function (it should be this way until percent >= 50). I've also put printf debugging blocks to check voterFavorites directly without any difference in the original values. EDIT: I'm VERY new to C and if I remember correctly you need to use malloc/calloc to make it so you can change the values later down the road. – dopps Feb 04 '14 at 03:28