C program to find inverses of a permutation

This is a discussion on C program to find inverses of a permutation within the C Programming forums, part of the General Programming Boards category; Hi all, I'm kind of newbie to these programming stuff. Can you please help me out with a code to ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    13

    Question C program to find inverses of a permutation

    Hi all,

    I'm kind of newbie to these programming stuff. Can you please help me out with a code to find inverses of a permutation?

    For example, if the elements are 10 4 12 11 6, to find inverse, we have to compare first element with the subsequent elements, if the first element is larger than the compared element, then it is an inverse..

    to start: compare 10 to 4, Condition 1: the position of 10 is < 4, so now compare the value 10>4, so it is an inverse.. NExt compare, 10 with 12, similarly position of 10 is less than 12, now compare values 10<12, it is not inverse... so on..

    then compare 4 with all its subsequent elements. Similarly compare all the elemeents with *subsequent* elements alone and increment count if value of 1st elemnt> element compared..

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    So, I guess you just ignored the homework policy, eh?

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    13
    sorry abt that. I'll write my code and get back with my doubts..

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So you're counting all the swaps that would be needed, if you were going to do a very simple sort - you're just not making those swaps.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    13

    Question

    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    int n,a[100],i,j,count=0;
    printf("Enter total no of elements in permutation");
    scanf("%d",&n);
    printf("Enter all the elements of permutation");
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    
    count=0;
    for(i=0;i<n;i++)
    { 
    for(j=i+1;j<n-1;j++)
    {
    
    if(a[i]>a[j])
    count++;
    }
    }
    printf("%d",count);
    return 0;
    getch();
    }
    Hi, Im just trying to compare the first element of array with the subsequent remaining elements, if the first element is greater than the subsequently compared elements, Count is incremented by 1. For each subsequent element that is less than the compared element we increment by 1..Next, take the 2nd element and compare it with the subsequent elements, if it is greater, increment count by 1.. This carries on until the end of the array..

    What's the problem with the above code?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I should hope that you will tell me, what if anything, is wrong with your code, that you can't fix.

    General suggestions:

    1) don't include conio.h - you won't need it *HERE*, and your code will be more portable, without it.

    2) make your main() int main(), instead. THAT is C, has always been C as far back as Turbo C ver. 1.01 and the AT&T standard, from 20?? years ago.

    I know you'll see books and prof's teaching with main() or void main(), but that is NOT correct for working with the operating system, or the C standard.

    3) add the obligatory "return 0;", to indicate a normal return from a run, by your program. ONLY at the very last line of code - not before.

    4) replace "getch()", with the standard "getchar();" or SomeIntVariable = getchar();

    5) I'm not sure your logic is right on the high end of the array. There's more than one way to "skin" this cat, but my code for this sort (Selection Sort) is just a bit different:

    Code:
    // i<N on next line works, but the program makes one extra "null" loop
    for(i=0, count=0;i<N-1;i++)  {  
       for(j=i+1;j<N;j++)  {
          if(a[i] > a[j]) 
            count++;
      }
    }
    The above looks OK to me, but I haven't run it.

    6) Congrats on using code tags! Now it's time to indent your subordinate lines of code, appropriately. Your eye will spot all kinds of bugs, very quickly, that way.

    Edit: Yes, if you have a "count" on the last element of the array, your code crashes the program. Fix it code works OK.
    Last edited by Adak; 08-20-2010 at 10:42 PM.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    13
    Thanks a lot, man... You're the man

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 05:24 PM
  2. Please find an error in my noob program
    By alexpos in forum C Programming
    Replies: 2
    Last Post: 10-23-2005, 03:55 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Replies: 4
    Last Post: 08-15-2002, 12:35 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21