Re: printing the permutation, help



rrs.matrix@xxxxxxxxx wrote:
the soulution u have will not work.
u have to use recursion.
check out this program.

Please provide context when replying. People might not be able to see the post you are replying to. See the section about Google at http://clc-wiki.net/wiki/intro_to_clc for details.

Also, please don't use stupid contractions like u. They make it far harder for the non-native English speakers here, and don't help the native English speakers either. Remember, far more people will read any given message than write it. You saving 1 second in typing compared to costing a lot of people rather more than that, you do the maths on whether it is a time saver.

#include<stdio.h>

The space shortage was alleviated some years back. To make it easier to read use:
#include <stdio.h>


int a[]={1,2,3,4};

Yuck. Horrible. Don't use global variables without a very good reason. The rest of your code just becomes harder to read, especially when it gets more complex.

permute(int * b,int n)

Implicit int was removed in the C99 standard and many considered it bad style even before then, and you don't return a value anyway!
void permute(int *b,int n)

Also, the space between the * and the b does not help readability in my opinion.

{
int i;

Don't use tabs for indentation. They sometimes get stripped as your message is transmitted over Usenet leaving the code to appear unindented and hard to read. Use spaces instead.

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

Now accessing the global variable. Horrible.

}
printf("\n");
return;
}

int temp;

OK, now this is not valid according to any C standard. It is only with C99 that the ability to declare variables within a block was added, before then you have to declare all your variables before the first statement in the block, but since you use implicit int you are not using C99.

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);

I'm sure this is wrong. You are always passing a pointer to the second element of your global variable a each time you recurs down. I've not checked to see if your code works since it is too horrible for me to bother compiling and running.

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

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

main()

Implicit int again. Try
int main(void)

{
permute(a,4);
}

You appear to be very inexperienced at C. You would be better off reading and asking questions rather than trying to advise people who know very little.

Also, there are plenty of solutions without bothering with recursion.
--
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
.



Relevant Pages


Loading