Silly do - while question

N

Newbie

#include <stdio.h>
#include <string.h>

void Permute(char *Perm,
size_t n,
size_t unchanged)
{
size_t outer = 0;
size_t inner = 0;
int temp = 0;

int flag = 0 ;

if(unchanged < n)
{
for(outer = unchanged; outer < n; outer++)
{

/*Rotate the array to the right*/

do {
temp = Perm[outer];
for(inner = outer; inner > unchanged; inner--){
Perm[inner] = Perm[inner - 1];
}
Perm[unchanged] = temp;

/*Done Rotation*/
}while( temp == Perm[unchanged]); /*I need to change outer here*/

/*
All i want here is to continue with rotation above if the first element
of right array is same as that before the rotation takes place.
I know this is really silly but i just cannot figure it out!
Please Help!!!
*/

Permute(Perm,
n,
unchanged + 1);
/*Bring array back into the right order so that the next
recursion level up from here works properly.
*/
for(inner = unchanged; inner < outer; inner++){
Perm[inner] = Perm[inner + 1];
}
Perm[outer] = temp;
}
}
else
{
printf("%s\n", Perm);
}

}
 
B

Ben Bacarisse

Newbie said:
#include <stdio.h>
#include <string.h>

void Permute(char *Perm,
size_t n,
size_t unchanged)
{
/*
All i want here is to continue with rotation above if the first
element of right array is same as that before the rotation takes
place.
I know this is really silly but i just cannot figure it out!
Please Help!!!
*/

The version you posted in comp.programming has the correct loop. Why
change it?
 
N

Newbie

Ben said:
The version you posted in comp.programming has the correct loop. Why
change it?

I have figured out the algorithm after some effort but I don't want to
print duplicated elements in the string.

So an input like "aabcc" should produce only distinct permutations.
 
B

Ben Bacarisse

Newbie said:
I have figured out the algorithm after some effort but I don't want to
print duplicated elements in the string.

So an input like "aabcc" should produce only distinct permutations.

Ah, homework?
 
N

Newbie

Ben said:
....snip..


Ah, homework?


No . Writing a recursive solution is a lot easier(even an iterative
one). In fact i don't need the "rotation" method if duplicate elements
are not a concern.
i know its pretty silly but i just cannot figure it out at this point of
time.

for(outer = unchanged; outer < n; outer++)
{

/*Rotate the array to the right*/
do {
temp = Perm[outer];
for(inner = outer; inner > unchanged; inner--){
Perm[inner] = Perm[inner - 1];
}
Perm[unchanged] = temp;

/*Done Rotation*/
}while( temp == Perm[unchanged]);

This is an infinite loop.I guess the while statement should be while (
temp == Perm [outer]).Tried increasing outer(keeping bounds in mind)
inside the do {} part as well but nothing will work when I really need
it to.

Anyways thanks for the reply.
 
N

Newbie

Richard said:
Newbie said:


Especially when someone else wrote it.
Yes sir. Its your Code. I never claimed it to be mine.

This is only half the solution I am trying to achieve.
If you can help me change this for duplicated strings,
it will suffice.But then it is a question more apt for
comp.lang.programming.Sigh !

void permute(char* v, const int start, const int n)
{
char tmp ;
int i;
int repeat = 0;

if (start == n-1) {
if (v != 0) {
for (i = 0; i < n; i++) {
printf("%c", v );
}
printf("\n");
}
}
else {
for (i = start; i < n; i++) {
tmp = v;
v = v[start];
v[start] = tmp;

permute(v, start+1, n);

v[start] = v;
v = tmp;
}
}
}

Probably the question was too silly to be answered.
Thank You.
 

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,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top