printing the permutation, help

F

fool

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);
}
printf("\n");
for(i= 3 ; i < p ; i--)
{
printf("second = %c\n",p);
}
printf("\n");

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

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.
 
R

rrs.matrix

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

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 said:
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.

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


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

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


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

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

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.
 
F

Fred Kleinschmidt

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

fool said:
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);
}
printf("\n");
for(i= 3 ; i < p ; i--)
{
printf("second = %c\n",p);
}
printf("\n");

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

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.
 
F

fool

Flash said:
fool said:
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);
}
printf("\n");
for(i= 3 ; i < p ; i--)
{
printf("second = %c\n",p);
}
printf("\n");

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

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. :(
 
F

Flash Gordon

fool said:
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...

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
ask your tutor for more assistance.
 

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,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top