# Thread: Create an array that holds the position from every element from another array

1. ## Create an array that holds the position from every element from another array

I want to create an array named pos that holds the position from every element from the array named per.For example if per={3,1,5,6,7,2} the pos arrays need to be pos[3]=1, pos[1]=2, pos[5]=3...Also the first element from per need to be empty.The code i have written so far is below.
Code:
```void main(){
int i;
int j;
int *per;
int n;
printf("enter the length of permutation\n");
scanf("%d",&n);
per =(int*)malloc(n*sizeof(int));
if(per==NULL){
printf("error \n");
exit(0);
}

printf("\nEnter the nodes:\n");
for(i=1;i<n;i++){//first element empty
scanf("%d", &per[i]);
}

int *pos;
pos =(int*)malloc(n*sizeof(int));
if(pos==NULL){
printf("error \n");
exit(0);
}
for(i=0;i<n;i++){
pos[i]=0;
}
for(i=1;i<n;i++){
pos[per[i]]=i;
}

return 0;
}```
I am doing something wrong but i don't know what.

2. Originally Posted by Xaris
Also the first element from per need to be empty.
You cannot really have it "empty" unless you designate a special int value as the "empty" value. Why does it need to be "empty" in the first place? I'd suggest that that makes it more likely that you'll make off-by-one errors, and here we can already see it in action: you prompt to "enter the length of permutation", then read the input into n, but then you allocate space for n elements rather than n+1 elements. Since you skip the first element, this means that you end up being one element short.

What I'd suggest instead is to use the entire dynamic array of n elements, including the first element, and have the pos array's values (i.e., the positions) be zero-based indexed as well. You only need the one-based index when printing output, if you want to print an index along with the values. If you're trying to say, do some permutation thing based on a maths reference that is one-based, it does mean that you'll have to transform the maths a bit.

Originally Posted by Xaris
I am doing something wrong but i don't know what.
Aside from what I pointed out, how do you know that you're doing something wrong?

3. Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void shuffle(int *a, int size) {
for (int i = size; i > 1; ) {
int r = rand() % i--;
int t = a[r]; a[r] = a[i]; a[i] = t;
}
}

int main() {
srand(time(NULL));

int n;
printf("Length of permutation: ");
scanf("%d", &n);

int *per = calloc(n + 1, sizeof *per); if (!per) { perror("calloc"); exit(0); }
for (int i = 1; i <= n; ++i) per[i] = i;  // first element unused
shuffle(per + 1, n); // don't pass first element to shuffle

int *pos = calloc(n + 1, sizeof *pos); if (!pos) { perror("calloc"); exit(0); }
for (int i = 1; i <= n; ++i) pos[per[i]] = i;

for (int i = 1; i <= n; ++i) printf("%2d ", i);      putchar('\n');
for (int i = 1; i <= n; ++i) printf("%2d ", per[i]); putchar('\n');
for (int i = 1; i <= n; ++i) printf("%2d ", pos[i]); putchar('\n');

free(pos);
free(per);

return 0;
}```
Code:
```Length of permutation: 20
1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
7  1  4  3 13  2 19 16 17 20 10  9 14 18  5 11 12  6 15  8
2  6  4  3 15 18  1 20 12 11 16 17  5 13 19  8  9 14  7 10```