I want to traverse a 2D square array that I have converted to 1D.
The problem is that I want to traverse it as if I was traversing the original 2D in diagonal strips.
The array is diagonal and I originally created it with malloc in 1D to avoid allocating too much memory.
The size of the array:
int Tsize = N*(N+1)/2; //table size -> diagonal array of (N+1)*N/2 elements
int* table = malloc(Tsize*sizeof(int));
I know that this way you traverse a flattened 2D array.
do{
i = row*N + col;
col++;
if (col>N-1){
col = 0;
row++;
}
printf("%d ", x[i]);
} while( row < N);
And here is what I found for traversing an array in diagonal strips.
For this array:
int x[3][3] = {1, 2, 3,
ø, 4, 5,
ø, ø, 6};
ø: i will not use that element.
I create this array:
int k[6] = {1,2,3,4,5,6};
and I want to traverse it like that:
1,4,6,2,5,3
Can you suggest anything? I'm stuck.