Sorry it took four years for a reply. I came across this while looking for the same thing. Saw the reply and figured it was worthy of something more useful.
Thought about the problem for a few secs and realised its pretty easy to solve. Just add the length to your value before calculating:
int c = 0;
int length = 4;
c = (-1 + length) % length;
lcd.print(c);
In most cases this will suffice as most algorithms just want to loop around arrays and get to the other end of the array when doing some sort of simple traversal. For me, it was a ring of LEDs I was animating using the FastLED library.
Hope that helps. If your calculations exceed the length you'll need to add a multiple of length to push the number to be positive before using the modulo operator.
This one is better, it is not restricted by having to add a fixed offset:
void setup() {
Serial.begin(9600);
int c;
const int length = 4;
for (int i = -10; i <= 10; i++)
{
c = (unsigned int)i % length;
Serial.print(i);
Serial.print(" is mod ");
Serial.println(c);
}
}
void loop() {}