If you have an array with 2 elements, the indices of those elements will be 0 and 1. If you run your for loop from 0 to <=2, then you will be trying to access indices 0, 1 and 2. If you want to stay in bounds, the for loop should just run from 0 to 1 (i.e. <2).
In general, that's 0 to size-1 as cire pointed out, where size is the number of elements in the array.
What exactly does "doesn't work" mean? Does it distribute illegal content? Does it randomly mute your volume or turn it to max? Does it cause inflation in a foreign country? You need to be specific.
If I put the changes coder777 suggested into your code and input random numbers (1 through 18 for A, and 22,23,24 for B), I get this output. What output are you expecting if this isn't what you wanted?