I haven't looked at C code in ages, but I think
int arr[n];
is wrong. Space for local variables is allocated at the start of the function, and at that point the value of n isn't available yet (could be 0, could be something else). You need to malloc() this array, that should fix it.