Find the index in the source array based on the current index plus the shift size.
Something like this might work for you;
#include <stdio.h>
#include <stdlib.h>
#define DATA_SIZE 10
void fill_data(int data[], int size) {
for (int i = 0; i < size; ++i)
data[i] = i;
}
void print_data(int data[], int size) {
for (int i = 0; i < size; ++i)
printf("%d: %d\r\n", i, data[i]);
}
void shift_data(int source[], int target[], int size, int step, int wrap) {
int source_index;
for (int i = 0; i < size; ++i) {
source_index = i + step;
if (wrap) {
source_index = source_index % size;
target[i] = source[source_index];
}
else {
if (source_index < 0 || source_index >= size)
target[i] = 0; else
target[i] = source[source_index];
}
}
}
int main() {
int source[DATA_SIZE];
int target[DATA_SIZE];
fill_data(source, DATA_SIZE);
printf("Source data:\r\n");
print_data(source, DATA_SIZE);
shift_data(source, target, DATA_SIZE, 1, 0);
printf("Target data, shifted down:\r\n");
print_data(target, DATA_SIZE);
shift_data(source, target, DATA_SIZE, -3, 0);
printf("Target data, shifted up by 3:\r\n");
print_data(target, DATA_SIZE);
shift_data(source, target, DATA_SIZE, -4, 1);
printf("Target data, rotated up by 4:\r\n");
print_data(target, DATA_SIZE);
return 0;
}
Hope this helps,
Fredrik