Create an array that holds the position from every element from another array
Quote:
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;
}
Quote:
I am doing something wrong but i don't know what.