0

Hi, I am trying to divide array elements in low and high using a single for loop. But, I don't know why I am getting ArrayIndexOutOfBoundsException. I tried working it out on paper and it seems correct. Can anyone point out my mistake?
Thanks!!

for(int i=0;i<=n/2;i++)
{
    ALow[i]=A[i];
    AHigh[i]=A[i+n/2];

}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by VernonDozier
0

Hi, I am trying to divide array elements in low and high using a single for loop. But, I don't know why I am getting ArrayIndexOutOfBoundsException. I tried working it out on paper and it seems correct. Can anyone point out my mistake?
Thanks!!

for(int i=0;i<=n/2;i++)
{
    ALow[i]=A[i];
    AHigh[i]=A[i+n/2];

}

Too little info. We don't know what n is. We don't know what indexes A[], ALow[] and AHigh[] are defined for. If n is even and A[] is defined for n elements, then its valid indexes would be 0 through n - 1. When i is equal to n / 2, i + n / 2 equals n / 2 + n / 2 = n, so A[i+n/2] = A[n] would be out of bounds since the highest legal index would be n - 1.

But that's just speculation. We'd have to see the rest of the code and possibly run the code.

Edited by VernonDozier: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.