I think what you're trying to do is convert an integer to a binary
string. For example, for the input 11, the output would be "1011". Is that right? The reason you might need to reverse an array is that it's easier to figure out the bits from right to left.
The calculations you're doing look OK to me, but you're just printing your results to the screen. What you want to do is start building up a String with the r's as you calculate them. The last thing you'd do then is reverse the String.
This creates an array with n elements with indexes from 0 to n-1. The means a[n] is definitely going to be a problem, because that's one past the last element. Now, "r" is what you put in the array, so using it as an index doesn't make sense either. That is, binary[r] isn't what you want to be doing.