471,092 Members | 959 Online

# permutation code.

this is my version of permutation and combinations.
is this piece of code correct.
#include<stdio.h>
int a[]={1,2,3,4};

permute(int * b,int n)
{
int i;
if(n==1)
{
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
printf("\n");
return;
}

int temp;
for(i=0;i<n;i++)
{
temp=b[0];
b[0]=b[i];
b[i]=temp;

printf("\n");
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
permute(a+1,n-1);

temp=b[0];
b[0]=b[i];
b[i]=temp;

printf("\n");
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
}
}

main()
{
permute(a,4);
}

Jul 9 '06 #1
4 8462
RIFF.D
Jul 9 '06 #2
int a[]={1,2,3,4};
permute(int * b,int n)
{
int i;
if(n==1)
{
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
printf("\n");
return;
}
int temp;
for(i=0;i<n;i++)
{
temp=b[0];
b[0]=b[i];
b[i]=temp;
printf("\n");
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
permute(a+1,n-1);
temp=b[0];
b[0]=b[i];
b[i]=temp;
printf("\n");
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
}

}
main()
{
permute(a,4);
}

Jul 9 '06 #3
rr********@gmail.com wrote:
this is my version of permutation and combinations.
is this piece of code correct.
#include<stdio.h>
<snip>

It is no more correct than when you posted it on the 7th, it also has
not improved since I commented on it also on the 7th. Any C compiler in
conforming mode (conforming to any version of the C standard) is
required to issue diagnostics for the code. If I ignore the diagnostics
and run it anyway it does not produce the correct result.

Once you have addressed those points and produced some code that is
actually different you might get a few more comments.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jul 9 '06 #4
<rr********@gmail.comwrote:
this is my version of permutation and combinations.
is this piece of code correct.
#include<stdio.h>
int a[]={1,2,3,4};

permute(int * b,int n)
{
int i;
if(n==1)
{
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
printf("\n");
return;
}

int temp;
for(i=0;i<n;i++)
{
temp=b[0];
b[0]=b[i];
b[i]=temp;

printf("\n");
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
permute(a+1,n-1);
You have to make up your mind. Is the array a global variable, as you use
it here? Or is it a parameter passed to functions, as used in main? It is
pretty much essential when using recursion, that a function called via.
recursion receives its knowledge via parameters. So start by putting the
array in main and doing whatever fixup that implies. I am new to this thread
so I have no idea what your real underlying problem is. But this problem is
a biggie and must be fixed before you can proceed. .

Also, don't post code that the compiler complains about. You don't declare a
return type for permute().. This attracts criticism like a misplaced
decimal point at an accountant's convention. It may have nothing to do with
your problem but it causes all kinds of hostile - and perhaps not germane to
>
temp=b[0];
b[0]=b[i];
b[i]=temp;

printf("\n");
for(i=0;i<4;i++)
{
printf("%d",a[i]);
}
}
}

main()
{
permute(a,4);
}

Jul 9 '06 #5

### This discussion thread is closed

Replies have been disabled for this discussion.