# C program to find inverses of a permutation

• 08-20-2010
newbie123
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..
• 08-20-2010
rags_to_riches
So, I guess you just ignored the homework policy, eh?
• 08-20-2010
newbie123
sorry abt that. I'll write my code and get back with my doubts..
• 08-20-2010
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.
• 08-20-2010
newbie123
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?
• 08-20-2010
I should hope that you will tell me, what if anything, is wrong with your code, that you can't fix. :p

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.
• 08-22-2010
newbie123
Thanks a lot, man... You're the man :)