Velocity Reviews > printing the permutation, help

# printing the permutation, help

fool
Guest
Posts: n/a

 07-07-2006
Dear group,

Given a string I have to print the permutation, using some
looping tricks. This is not a Home work problem. My best
try as beginner is:

#include<stdio.h>
#include<stdlib.h>

int main(void)
{
char p[] = "abcd";
int i,j,k;
for(i=0;i<4;i++)
{
printf("first = %c\n",p[i]);
}
printf("\n");
for(i= 3 ; i < p[i] ; i--)
{
printf("second = %c\n",p[i]);
}
printf("\n");

for(i= 2 ; i <= p[i] ;i++)
{
printf("third = %c\n",p[i]);
}

return 0;
}

The first two for loops prints abcd and dcba. But I am
not able to proceed further. Can any one guide me. Pls
don't write the program instead pls give me a hint. Thanks
for any help.

rrs.matrix@gmail.com
Guest
Posts: n/a

 07-07-2006
the soulution u have will not work.
u have to use recursion.
check out this program.

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

Flash Gordon
Guest
Posts: n/a

 07-07-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
#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

Fred Kleinschmidt
Guest
Posts: n/a

 07-07-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> the soulution u have will not work.
> u have to use recursion.
> check out this program.
>

Don't top post!
"u" is not an English word.
And it is never "necessary" to use recursion. Helpful sometimes,
but all recursive constructs can be written using loops.

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

--
Fred

Flash Gordon
Guest
Posts: n/a

 07-07-2006
fool wrote:
> Dear group,
>
> Given a string I have to print the permutation, using some
> looping tricks. This is not a Home work problem. My best
> try as beginner is:
>
> #include<stdio.h>
> #include<stdlib.h>

Some spaces would make it easier to read.
#include <stdio.h>
#include <stdlib.h>

However, you are not currently using anything from from stdlib.h so I
would not bother including that.

> int main(void)
> {
> char p[] = "abcd";
> int i,j,k;
> for(i=0;i<4;i++)
> {
> printf("first = %c\n",p[i]);
> }
> printf("\n");
> for(i= 3 ; i < p[i] ; i--)
> {
> printf("second = %c\n",p[i]);
> }
> printf("\n");
>
> for(i= 2 ; i <= p[i] ;i++)
> {
> printf("third = %c\n",p[i]);
> }
>
> return 0;
> }

The rest of your code is find as far as it goes.

> The first two for loops prints abcd and dcba. But I am
> not able to proceed further. Can any one guide me. Pls
> don't write the program instead pls give me a hint. Thanks
> for any help.

This is really an algorithms question for comp.programming rather than a
C language question. However, I'll give you some hints.

First, don't worry about C, get a pen and paper and write out all of the
combinations. Do it logically not at random.

For the first character, you can pick any of the 4 characters. So pick
one. This only leave three options for the next character, and so on.
For the last character you only have one choice. So, to change anything
you have to back off and change the penultimate character. Just try it
and see how it works. Then try to write an algorithm to do what you have
done. If you've covered recursion that might help, but it can be done
without recursion as well.

As I say, if you want help on the algorithm post to comp.programming,
once you have an algorithm, try to implement it in C and post your
questions about the problems you are having here.
--
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

fool
Guest
Posts: n/a

 07-07-2006
Flash Gordon wrote:
> fool wrote:
> > Dear group,
> >
> > Given a string I have to print the permutation, using some
> > looping tricks. This is not a Home work problem. My best
> > try as beginner is:
> >
> > #include<stdio.h>
> > #include<stdlib.h>

>
> Some spaces would make it easier to read.
> #include <stdio.h>
> #include <stdlib.h>
>
> However, you are not currently using anything from from stdlib.h so I
> would not bother including that.
>
> > int main(void)
> > {
> > char p[] = "abcd";
> > int i,j,k;
> > for(i=0;i<4;i++)
> > {
> > printf("first = %c\n",p[i]);
> > }
> > printf("\n");
> > for(i= 3 ; i < p[i] ; i--)
> > {
> > printf("second = %c\n",p[i]);
> > }
> > printf("\n");
> >
> > for(i= 2 ; i <= p[i] ;i++)
> > {
> > printf("third = %c\n",p[i]);
> > }
> >
> > return 0;
> > }

>
> The rest of your code is find as far as it goes.
>
> > The first two for loops prints abcd and dcba. But I am
> > not able to proceed further. Can any one guide me. Pls
> > don't write the program instead pls give me a hint. Thanks
> > for any help.

>
> This is really an algorithms question for comp.programming rather than a
> C language question. However, I'll give you some hints.
>
> First, don't worry about C, get a pen and paper and write out all of the
> combinations. Do it logically not at random.
>
> For the first character, you can pick any of the 4 characters. So pick
> one. This only leave three options for the next character, and so on.
> For the last character you only have one choice. **So, to change anything
> you have to back off and change the penultimate character.**

swapping? If yes the is the above not possible with just some set of
loops? Sorry, another beginner question.

>Just try it
> and see how it works. Then try to write an algorithm to do what you have
> done. If you've covered recursion that might help, but it can be done
> without recursion as well.
>
> As I say, if you want help on the algorithm post to comp.programming,
> once you have an algorithm, try to implement it in C and post your
> questions about the problems you are having here.
> --
> 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

Flash Gordon
Guest
Posts: n/a

 07-08-2006
fool wrote:
> Flash Gordon wrote:
>> fool wrote:

<snip>

>> First, don't worry about C, get a pen and paper and write out all of the
>> combinations. Do it logically not at random.
>>
>> For the first character, you can pick any of the 4 characters. So pick
>> one. This only leave three options for the next character, and so on.
>> For the last character you only have one choice. **So, to change anything
>> you have to back off and change the penultimate character.**

>
> swapping? If yes the is the above not possible with just some set of
> loops? Sorry, another beginner question.

Use a pen and paper and do the task, then analyse how you did it. As far
as I can see you have not yet tried to do this, and that is what I
suggested you do. Also, read the following paragraph again...

>> As I say, if you want help on the algorithm post to comp.programming,
>> once you have an algorithm, try to implement it in C and post your
>> questions about the problems you are having here.

See, it says that you should ask in comp.programming. This is
comp.lang.c *not* comp.programming. You ask there because you have
general programming problems, not C specific problems.

Also, if you are doing a programming course of some kind, I suggest you
--
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