<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
your problem - responses
>
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);
}