Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am looping through an array but I am having some trouble at first and last items in the array.

The code compares arr[i] with arr[i+1] and arr[i-1]. Obviously I want the index to compare i-1 as the last element but this causes an out of bound array index.

Current Loop:

     (i=1; i<arr.size-1; i++) 

    if (arr(i-1,0)==2 || arr(i+1,0)==2)
    {
        do stuff
    }

It only works if i starts at i=1 and loops to arr.size-1. How can I get around this?

share|improve this question
1  
What is the variable?? Is it a real variable, or a function? –  ikh Aug 13 '14 at 11:29
    
It depends on your usage of the array. You could try and loop on the other "side" of the array (compare arr[arr.size] with arr[array.size-1] and arr[0] ) –  Dimitri Mockelyn Aug 13 '14 at 11:30
2  
your code doesn't make sense, post all of your code. then maybe we can help. –  Dima Maligin Aug 13 '14 at 11:35
    
Handle one edge case, loop from 1 to size-1, handle the other edge case. Wrap "do stuff" in a function so you don't need to duplicate it. –  molbdnilo Aug 13 '14 at 11:42
1  
Assuming that this forms part of some algorithm, what are the boundary conditions (i.e. what does the algorithm say about how the first and last elements should be handled)? Are they special cases? –  Niall Aug 13 '14 at 11:55

4 Answers 4

up vote 0 down vote accepted

either add explicit checks for i==0 and i==arr.size-1, or use some math, something like this:

if (variable(i>0?i-1:arr.size-1,0)==2 || variable((i+1)%arr.size,0)==2)
share|improve this answer
    
this is wrong! you are checking for i > 0 and if its not you use the first index! how do you know that the first index should be used if i !> 0. and another thing how can you answer a question without a reasonable understanding of what he is trying to do? –  Dima Maligin Aug 13 '14 at 11:49
    
@DimaMaligin No, it's not wrong. If i>0, then the prev. index is used, if i==0, then the last (arr.size-1) index (and not the first as you wrote). And I'm pretty sure I understand what he wants :) –  glezmen Aug 13 '14 at 11:55
    
my bad you are right. didn't noticed the comma there before the 0, that still dose not make your answer correct though. –  Dima Maligin Aug 13 '14 at 12:00
    
This is a great solution thanks a lot. –  user3512203 Aug 13 '14 at 12:48

There is no way around this. You are operating at the limits of your array and you have to make sure you do not work outside of the memory portion where your elements are.

Here are some code snippets that should illustrate the principal connection.

int i;
int a[ARR_MAX]; //a[0] to a[ARR_MAX-1]


for(i = 0; i < ARR_MAX; ++i)
{
    a[i-1] //is an error if i is 0
    a[i] //the usual way to access an array
    a[i+1] //is an error if i is (ARR_MAX-1)
}

for(i = 1; i < (ARR_MAX-1); ++i)
{
    a[i-1] //i does not start a 0
    a[i+1] //i is never (ARR_MAX-1)
}

for(i = 0; i < ARR_MAX; ++i)
{
    a[(i-1)%ARR_MAX] //is an error if i is 0
    a[i%ARR_MAX] //is irrelevant in this loop
    a[(i+1)%ARR_MAX] //is a[0] if i is (ARR_MAX-1)
}

for(i = 1; 1; ++i) // loops forever, i will get negative at some point
{
    a[i%ARR_MAX]//can never be an error, unless i is negative
    a[(i+n)%ARR_MAX]//can never be an error, unless i is negative
    a[(i-n)%ARR_MAX]//is an error for n > i
}

Applied to your case it may simply be correct to us start and stop the loop one element earlier. I will refine this statement if you can give some details about your usecase.

share|improve this answer

To have circular indexes, you may use modulus as: (for i in [0..array.size[)

if (variable((i - 1 + arr.size()) % arr.size(), 0) == 2
 || variable((i + 1) % arr.size(), 0) == 2)
share|improve this answer

If the elements in your array are on positions i=0..arr.size-1, then you could use something like this: if ((i>0 && variable(i-1,0)==2) || (i<arr.size-1 && variable(i+1,0))). If the i>0 returns false, then variable(i-1,0)==2 won't even be checked so it won't give you exception.

share|improve this answer
    
this doesn't answer the original question, because he wanted to 'wrap around' the array boundaries (so index -1 should be the last element for example) –  glezmen Aug 13 '14 at 11:37
    
this is wrong! if your condition is false you are not doing anything. how do you know if "nothing" should be done in that case? and another thing how can you answer a question without a reasonable understanding of what he is trying to do? –  Dima Maligin Aug 13 '14 at 11:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.