Vidya - 1 month ago 8
C Question

# How to get indexes alone from an array

I have the following array and index

``````0 1 2 3 4 5 6 7 8 <-indexes -- n values
7 7 8 8 6 9 8 7 9 <-values -- array x
``````

I have another array with the indices

``````0 1 2 3 4 5 6 7 8 <-indexes -- m values
7 8 9 9 8 7 7 8 9 <-values  -- array y
``````

what I want is to obtain all the index(in reverse order of array x):

``````6: 4
7: 7 1 0
8: 6 3 2
9: 8 5
``````

And then, for each element in the y array, I want to make another array index[ ] have the values. So, index[6] = 4, index[7] = 7, 1, 0 etc.

I have done:

``````for (i=n-1; i>=0; i--) {
index[y[i]] = index[y[i]]+i;
}

for (i=0; i<m; i++) {
strings[i] = strings[i] + index[x[i]];
}

for (i=0; i<m; i++) {
printf("%d",strings[i]);
}
``````

The desired output is to print a sequence like 710632858563271071063285 but this is not printing the desired output.

``````#include <stdio.h>
#include <string.h>

int main(void) {
int x[] = {7, 7, 8, 8, 6, 9, 8, 7, 9};
int n = sizeof(x)/sizeof(*x);
int y[] = {7, 8, 9, 9, 8, 7, 7, 8, 9};
int m = sizeof(y)/sizeof(*y);
int index[10][10];

memset(index, -1, sizeof(index));
for(int i = n -1; i >= 0; --i){
int j = 0;
while(index[x[i]][j] >= 0)
++j;
index[x[i]][j] = i;
}
for(int i = 0; i < m; ++i){
for(int j = 0; j < 10 && index[y[i]][j] >= 0; ++j){
printf("%d", index[y[i]][j]);
}
}
return 0;
}
``````
Source (Stackoverflow)