I was wondering if is it possible to iterate trough all arrays elements starting from any of its elements without pre-sorting the array.
just to be clearer suppose i have the array of 5 elements:
0 1 2 3 4
i want to read all elements starting from one of their index like:
2 3 4 0 1
or
4 0 1 2 3
the idea is to keep the element order in this way:
n ,n+1 ,..., end ,start, ..., n-1
One solution could be (pseudocode):
int startElement;
int value;
for(startElement;startElement<array.count;startElement++){
value = array[startElement];
}
for(int n = 0; n<startElement;n++){
value = array[n];
}
but I don't know if there's a better one. Any suggestions?
Use the modulus operator:
int start = 3;
for (int i = 0; i < count; i++)
{
value = array[(start + i) % count];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With