TL;DR:
int remainder = -1 % 5; //-1
int modulus = -1 modulus 5; //4 How do I do this?
I am trying to read the modulated value of an array. So the index modulated by the array length would be the updated index. For example:
array = {100, 200, 300, 400, 500}
array[6] = array[6 mod 5] = array[1] = 200
Easy enough. But when my index is negative, that's where I get into trouble.
array[-1] = array[???] = array[4] = 500
I do not know how to perform -1 mod 5. The remainder operator works for positives, but not negatives.
Here is my sample code that only works for positive values:
static void Main(string[] args)
{
int[] myArray = new int[5] { 100, 200, 300, 400, 500 };
int myIndex = myArray.Length - 1;
for (int i = 0; i < 15; i++)
{
myIndex = --myIndex % myArray.Length;
Console.WriteLine("Value: " + myArray[myIndex]);
}
Console.WriteLine("Done");
}
How can I get the modulus of a number, not the remainder in C#?