I am trying to make a function that, in a list of arrays, will compare each array to every array that comes after it: Array 0 will compare itself to Arrays 1, 2, 3, 4 and so on, Array 1 will compare itself to Arrays 2, 3, 4 and so on, but not 0, as we already compared it. The same goes for the rest: Array 2 will compare itself to 3, 4 and so forth.
Normally this could be done with a simple nested for loop:
for (int i = 0; i <= number_of_arrays - 1; i++){
for (int j = i+1; j<= number_of_arrays; j++){
printf("Comparing Array %d with Array %d", i, j);
}
}
However, since i'm using OpenMP, i can't use a #pragma omp for
inside another.
The solution would usually be using collapse(2)
as well, but i also can't use since the counter of my internal loop is dependant of the counter of my external one.
In order to not have this problem, i must turn this into a single for
loop, the general solution to this seem to be:
for (int i = 0; i <= (number_of_arrays - 1)*number_of_arrays; i++){
printf("Comparing Array %d with Array %d", i/number_of_arrays, i%number_of_arrays);
}
}
This, however, will cause redundancy of my code, as i will, for instance, compare Array 0 to Array 1, and later Array 1 to Array 0.
How can i turn what i have into a single for
loop that will not do redundant comparisons?
for (int i = 0; i <= (number_of_arrays - 1)*number_of_arrays; i++)
{
if(i%number_of_arrays > i/number_of_arrays)
printf("Comparing Array %d with Array %d\n", i/number_of_arrays, i%number_of_arrays);
}