permutation code.

R

rrs.matrix

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);
}
printf("\n");
return;
}

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

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


permute(a+1,n-1);

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

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

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

rrs.matrix

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);
}
printf("\n");
return;
}


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


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


permute(a+1,n-1);


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


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



}


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

Flash Gordon

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.

Go back and read the comments I made on it in the thread
http://groups.google.co.uk/group/co...oup:comp.lang.c&rnum=2&hl=en#7fda5bd8bdca72db
Once you have addressed those points and produced some code that is
actually different you might get a few more comments.
 
O

osmium

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);
}
printf("\n");
return;
}

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

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


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;
b=temp;

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top