I figured out what was wrong. 2 typos. i<=b/2 instead of i=b/2 and

for(int i=0;i< counter-1;i++) instead of for(int i;i< counter-1;i++).

Thanks!