472,805 Members | 3,586 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

printing the permutation, help

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.

Jul 7 '06 #1
6 1764
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);
}

Jul 7 '06 #2

<rr********@gmail.comwrote in message
news:11*********************@p79g2000cwp.googlegro 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
Jul 7 '06 #3
rr********@gmail.com 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
Jul 7 '06 #4
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
Jul 7 '06 #5
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
Jul 7 '06 #6
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
ask your tutor for more assistance.
--
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 8 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Talin | last post by:
I'm sure I am not the first person to do this, but I wanted to share this: a generator which returns all permutations of a list: def permute( lst ): if len( lst ) == 1: yield lst else: head =...
3
by: Jack Middleton | last post by:
Hi! I'm lookin for a faster permutation algorithm for matrices. I know that it can be done with multiplying a matrix with a permutation matrix. It just seems a waste to iterate through all those...
1
by: user | last post by:
Hello I have Array of 50 ints. I want to receive random permutation, so in each int will be different number from 0-49. Is there any class for permutation ? Thanx Michal
6
by: Rajesh | last post by:
Hello Everybody, Can anybody help me in writing a C program to generate and print all possible combinations of n numbers. For eg. for 3 numbers(1,2,3) there turn out 3! combinations. (1,2,3),...
1
by: dave | last post by:
Hi There Can anyone help to write permutation function for following formula.. nPr = n!/(n-r)! where n is length of given string and r is lengh of subset.. for example..if given string is "abcd"...
3
by: weidongtom | last post by:
Hi, I have been working at this problem, and I think I need a permutation algorithm that does the following: Given a list of elements that are either a character or a character follows by a...
6
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program...
7
by: xirowei | last post by:
Let's say i create a String array that store 4 Alphabets {"A","B","C","D"} How can i get the result if i need permutation of 4P3 and 4P2? I had refer to many examples from the internet, but...
0
by: 249740 | last post by:
Write a program that reads N phrases and determines if one phrase is a permutation of the other. For example: Phrase 1 is: “One World One Dream” Phrase 2 is: “World One One Dream”. Then the output...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.