Silly do - while question

Discussion in 'C Programming' started by Newbie, Aug 2, 2008.

  1. Newbie

    Newbie Guest

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

    }
    Newbie, Aug 2, 2008
    #1
    1. Advertising

  2. Newbie <> writes:

    > #include <stdio.h>
    > #include <string.h>
    >
    > void Permute(char *Perm,
    > size_t n,
    > size_t unchanged)
    > {

    <snip>
    > /*
    > 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?

    --
    Ben.
    Ben Bacarisse, Aug 2, 2008
    #2
    1. Advertising

  3. Newbie

    Newbie Guest

    Ben Bacarisse wrote:
    > Newbie <> writes:
    >
    >> #include <stdio.h>
    >> #include <string.h>
    >>
    >> void Permute(char *Perm,
    >> size_t n,
    >> size_t unchanged)
    >> {

    > <snip>
    >> /*
    >> 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?


    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.
    Newbie, Aug 2, 2008
    #3
  4. Newbie <> writes:

    > Ben Bacarisse wrote:
    >> Newbie <> writes:
    >>
    >>> #include <stdio.h>
    >>> #include <string.h>
    >>>
    >>> void Permute(char *Perm,
    >>> size_t n,
    >>> size_t unchanged)
    >>> {

    >> <snip>
    >>> /*
    >>> 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?

    >
    > 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?

    --
    Ben.
    Ben Bacarisse, Aug 2, 2008
    #4
  5. Newbie

    Newbie Guest

    Ben Bacarisse wrote:
    > Newbie <> writes:
    >
    >> Ben Bacarisse wrote:
    >>> Newbie <> writes:
    >>>
    >>>> #include <stdio.h>
    >>>> #include <string.h>

    ....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.
    Newbie, Aug 2, 2008
    #5
  6. Newbie

    Newbie Guest

    Richard Heathfield wrote:
    > Newbie said:
    >
    >> Ben Bacarisse wrote:

    >
    > <snip>
    >
    >>> Ah, homework?

    >> No . Writing a recursive solution is a lot easier

    >
    > 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.
    Newbie, Aug 2, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Don

    Silly question....

    Don, Sep 26, 2003, in forum: VHDL
    Replies:
    2
    Views:
    470
  2. John
    Replies:
    2
    Views:
    477
  3. John

    silly question

    John, Jul 20, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    382
  4. =?Utf-8?B?RGF2aWQrKw==?=

    Silly newbie question.

    =?Utf-8?B?RGF2aWQrKw==?=, Nov 15, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    414
    Alan Silver
    Nov 21, 2005
  5. =?Utf-8?B?bXVzb3NkZXY=?=

    Silly question about XHTML

    =?Utf-8?B?bXVzb3NkZXY=?=, Jan 18, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    403
    Steven Cheng[MSFT]
    Jan 19, 2006
Loading...

Share This Page