#include<stdio.h> #include<math.h> void sort(long int *a,long int n); void swap(long int *x,long int *y); main() { long int d=0,a,x,y,z,b,i=0,j,k,l,c=0; long int p[100], q[100]; printf("Enter the number: "); scanf("%ld",&a); b=a; while (b!=0) { p[i++]=(b%10); b/=10; } sort(p,i); for (j=0;j<i;++j) c+= (p[j])*(pow(10,i1j)); for (x=pow(10,i1);x<=c;++x) { y=x,l=0,z=0; while (y!=0) { q[l++]= (y%10); y/=10; } sort(q,i); for (k=0;k<i;++k) { if (q[k]==p[k]) ++z; else goto fool; } if (z==i) { printf("%ld\n",x); ++d; } fool: } printf("No. of possible permutations of %ld is %ld",a,d); } void sort(long int *a,long int n) { long int p=n1,i; while (p>=0) { for(i=0;i<=(p1);++i) { if (a[i]<=a[i+1]) swap(&a[i],&a[i+1]); else continue; } p; } } void swap(long int *x,long int *y) { long int t; t=*x; *x=*y; *y=t; }
11 Replies  10268 Views  Last Post: 09 December 2010  05:28 AM
#1
C program to generate all possible permutations of a given number
Posted 08 December 2010  04:43 AM
Replies To: C program to generate all possible permutations of a given number
#2
Re: C program to generate all possible permutations of a given number
Posted 08 December 2010  08:00 AM
Replace this:
goto fool; } if (z==i) { printf("%ld\n",x); ++d; } fool: }
With this:
break; } if (z==i) { printf("%ld\n",x); ++d; } }
*Just because it compiles, does not make it right. I'll willing to bet the problem is in how its being handled differently by MinGW (Quincy) and Borland.
#3
Re: C program to generate all possible permutations of a given number
Posted 08 December 2010  10:05 AM
#4
Re: C program to generate all possible permutations of a given number
Posted 08 December 2010  11:23 AM
Right, the goto must go. You can't nest it like that and it's poor programming anyway.
Using the rather quirky logic here, this should work:
void permuteNumber(long int a) { long int d=0,x,y,z,b,i=0,j,k,l,c=0; long int p[100], q[100]; b=a; while (b!=0) { p[i++]=(b%10); b/=10; } sort(p,i); for (j=0; j<i; ++j) { c += (p[j])*(pow(10,i1j)); } for (x=pow(10,i1); x<=c; ++x) { y=x,l=0,z=0; while (y!=0) { q[l++]= (y%10); y/=10; } sort(q,i); for (k=0; k<i; ++k) { if (q[k]!=p[k]) { break; } z++; } if (z==i) { printf("%ld\n",x); ++d; } } printf("No. of possible permutations of %ld is %ld\n\n",a,d); }
I must admit, once I figured out what it was on about, I was kind of amused.
There is a compiler gotcha here and that's the use of math.h. In gcc, you'll need to explicitly link the math library, the command line flag is "lm".
You can get rid of your pows, actually.
What you have could be written like this:
int isMatch(long int *a, long int *b, int i) { while(i > 0) { if (a[i]!=b[i]) { return 0; } } return 1; } int breakValueIntoDigits(long int value, long int *a) { int size = 0; while (value!=0) { a[size++]=(value%10); value/=10; } sort(a,size); return size; } void permuteNumber(long int a) { long int d=0,x=1,i,j,c=0; long int p[100]; i = breakValueIntoDigits(a,p); for (j=0; j<i; j++) { x *= 10; c *= 10; c += p[j]; } for (x/=10; x<=c; x++) { long int q[100]; breakValueIntoDigits(x,q); if (isMatch(p,q,i)) { printf("%ld\n",x); ++d; } } printf("No. of possible permutations of %ld is %ld\n\n",a,d); }
#5
Re: C program to generate all possible permutations of a given number
Posted 08 December 2010  12:19 PM
#6
Re: C program to generate all possible permutations of a given number
Posted 08 December 2010  01:28 PM
Yes, the code I posted is ANSI C and should run on any compliant compiler. I tried to make the function names clear so a programmer might follow what was going on. I don't believe I used any methodology that should confuse a novice programmer.
It was generated with gcc, at freely available compiler suite available on a wide range of systems. You mentioned you were using Quincy which is built on top of this toolset.
Given this, you may have to make a change to your Quincy project to include the math library to get it to compile. If you're getting an error about not linking to pow, then this is the case.
#7
Re: C program to generate all possible permutations of a given number
Posted 08 December 2010  01:45 PM
cupidvogel, on 08 December 2010  01:19 PM, said:
As soon as you type anything looking like this :
cupidvogel, on 08 December 2010  05:43 AM, said:
You are opening your self to endless reticule I didn't comment because your code lacks the usual "learning C with Borland" silliness, but it is a little hard to follow dude.
cupidvogel, on 08 December 2010  01:19 PM, said:
Yes it is C. Gcc = GNU C Compiler. MinGW(Quincy) is GCC based.
#8
Re: C program to generate all possible permutations of a given number
Posted 08 December 2010  02:03 PM
#9
Re: C program to generate all possible permutations of a given number
Posted 08 December 2010  04:48 PM
#10
Re: C program to generate all possible permutations of a given number
Posted 08 December 2010  10:47 PM
@cupidvogel I will suffer endless ridicule for my spelling/grammar, but Baavgai sums it all up rather nicely.
#11
Re: C program to generate all possible permutations of a given number
Posted 09 December 2010  12:32 AM
#12
Re: C program to generate all possible permutations of a given number
Posted 09 December 2010  05:28 AM
Code blocks is popular. Eclipse and Netbeans are nice IDEs. Anything that runs on both Windows and Linux is my preferred software. For C++, Qt Creator is impressive.
If you don't care about IDE, just use gcc from the command line. Pretty much everything I post here is written in a highlighting editor ( SciTE ) and compiled at the command line. Even larger project, actually, though then I take the time to write a make file.
