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

  1. #1
    Registered User
    Join Date
    Dec 2020
    Posts
    2

    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.
    Last edited by Xaris; 12-15-2020 at 08:23 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,311
    Quote 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.

    Quote 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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,102
    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
    Last edited by john.c; 12-16-2020 at 07:51 AM.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to compare element of array with other element
    By Djsarkar in forum C Programming
    Replies: 28
    Last Post: 08-04-2020, 03:59 PM
  2. array of ptrs vs constant ptr to an first element of array
    By monkey_c_monkey in forum C Programming
    Replies: 5
    Last Post: 08-30-2012, 11:39 PM
  3. Replies: 3
    Last Post: 02-17-2011, 06:21 AM
  4. size of an array poited by array element
    By mitofik in forum C Programming
    Replies: 7
    Last Post: 12-24-2010, 12:09 AM
  5. Trying to create a 5 element integer array
    By nadeni0119 in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2003, 08:35 PM

Tags for this Thread