What is an efficient way to get column from multi-dimensional array in C? -
i have 2 structs: array2d (multidimensional) , array (one-dimensional). column type array2d , copy type array.
although code works below, , recognize poor way of getting column array, i'm curious optimizations there might avoid o(n2) algorithm. efficient way column array in c?
bool arr2_getcolumn(array2d *arr, const int column_index, array *returnedarray) { int x, y; int = 0; /* check valid array. */ if (arr->blnisinit != true) return false; /* initialize array column's height. */ if (!arr_init(returnedarray, arr->height)) return false; /* copy on column. */ (y = 0; y < arr->height; y++) { (x = 0; x <= column_index; x++) { if (x == column_index) { returnedarray->array[i] = arr->array[y * arr->width + x]; i++; } } } /* set new size. */ returnedarray->size = arr->height; return true; }
get rid of i
, x
.
for (y = 0; y < arr->height; y++) { returnedarray->array[y] = arr->array[y * arr->width + column_index]; }
Comments
Post a Comment