-
sort 2D array
I would like to sort a 2D array[3][5] of float in descending order based on the last 'cell' e.g. [0][4], [1][4], . . .
Code:
. . .
scorecard[i][j] = ttl_pts; // load array
for(u = 1; u < FLIGHT; ++u)
for(v = FLIGHT; u < v; --v)
if(scorecard[v-1][4] < scorecard[v][4])
swap(scorecard[v-1], scorecard[v]); // designate row? swap
void swap (int * p, int * q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
}
This method isn't working. Thanks in advance for any help.
Keith
-
simplify
I tried to simplify by avoiding passing to a function :
Code:
/*for(u = 1; u < FLIGHT; ++u)
for(v = FLIGHT; u < v; --v)
if(scorecard[v-1][4] < scorecard[v][4])
temp[5] = scorecard[v-1];
scorecard[v-1] = scorecard[v];
scorecard[v] = temp;*/
the idea is wrong I suppose.
-
Thank you, Salem.
I see. I have to write to each element separately, rather than attempt to refernce a 'row' or 'set' of elements. Thank you for your help. I think I can work that out.
After your first comment, I recognized the error of using int *pointers in the swap() that were not matched to the array -- Pointers in multidimensional arrays are over my head at this point. I had mistakenly referred to this simpler swap function and overlooked that I was calling it, without noticing it would not work with an array.
_________________
I have been unable to understand the importance of void main() and have not found any mention of it outside of your icon animation (which is great). I notice that main() has changed over the iterations of C. Would you please say more about it if you haven't already elsewhere?
Keith