By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

permutation code.

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
RIFF.D
Jul 9 '06 #2

P: n/a
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

P: n/a
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.

Go back and read the comments I made on it in the thread
http://groups.google.co.uk/group/com...da5bd8bdca72db
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

P: n/a
<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);
}

Jul 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.