farmerdoug,
No offense implied, I find both your code and its intent very difficult to follow so the following may be off base and simply add noise to the discussion but the following (untested, uncompiled) code may be what you're after. If not my apologies.
I don't understand your use of 'atoi' but left it in assuming you do.
No offense implied, I find both your code and its intent very difficult to follow so the following may be off base and simply add noise to the discussion but the following (untested, uncompiled) code may be what you're after. If not my apologies.
Code:
#define kPAGES (2000)
#define kROWS (10)
#define kCOLUMNS (45)
#define kBYTE_COUNT_PER_COLUMN (12 * sizeof(char))
#define kBYTE_COUNT_PER_ROW (kCOLUMNS * kBYTE_COUNT_PER_COLUMN)
#define kBYTE_COUNT_PER_PAGE (kROWS * kBYTE_COUNT_PER_ROW)
char* allocate_memory(void)
{
void* ptr = calloc((kPAGES * (kROWS * kCOLUMNS)), kBYTE_COUNT_PER_COLUMN);
if ( NULL == ptr )
{
printf("no memory");
}
return (char*)ptr;
}
//n = number of items to sort
void sort(char* array, int n, int row, int column)
{
char tmp[kBYTE_COUNT_PER_PAGE];
#define INDEX(PP,RR,CC) ((PP * kBYTE_COUNT_PER_PAGE) + ((RR * kBYTE_COUNT_PER_ROW) + (CC * kBYTE_COUNT_PER_COLUMN)))
#define DUMP(NN) for(int i=0; i<NN; i++) {printf("%s \n ",&array[INDEX(i,0,3)]);}
DUMP(n);
printf("\n");
for ( int j = (n - 1); j >= 0; j-- )
{
for ( int k = 1; k <= j; k++ )
{
if ( atoi(array + (k - 1)*kPAGES + row*kROWS + column*kCOLUMNS) > atoi(array + (k)*kPAGES + row*kROWS + column*kCOLUMNS) )
{
printf("\n");
memcpy(tmp, &array[INDEX(k - 1, 0, 0)], kBYTE_COUNT_PER_PAGE);
memcpy(&array[INDEX(k - 1, 0, 0)], &array[INDEX(k,0,0)], kBYTE_COUNT_PER_PAGE);
memcpy(&array[INDEX(k,0,0)], tmp, kBYTE_COUNT_PER_PAGE);
DUMP(n);
}
}
}
#undef INDEX
#undef DUMP
}
I don't understand your use of 'atoi' but left it in assuming you do.