printing the permutation, help

Discussion in 'C Programming' started by fool, Jul 7, 2006.

  1. fool

    fool Guest

    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.
    fool, Jul 7, 2006
    #1
    1. Advertising

  2. fool

    Guest

    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);
    }
    , Jul 7, 2006
    #2
    1. Advertising

  3. fool

    Flash Gordon Guest

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


    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.
    --
    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, Jul 7, 2006
    #3
  4. <> wrote in message
    news:...
    > 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);
    > }
    >

    --
    Fred
    Fred Kleinschmidt, Jul 7, 2006
    #4
  5. fool

    Flash Gordon Guest

    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);
    > }
    > 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.
    --
    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, Jul 7, 2006
    #5
  6. fool

    fool Guest

    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);
    > > }
    > > 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. :(

    >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, Jul 7, 2006
    #6
  7. fool

    Flash Gordon Guest

    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
    Flash Gordon, Jul 8, 2006
    #7
    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. Roger B.
    Replies:
    13
    Views:
    601
    D.F.S.
    Sep 26, 2003
  2. m sergei
    Replies:
    4
    Views:
    11,107
    Philip Parker
    Jun 29, 2004
  3. fadliwdt

    [help] String permutation with input

    fadliwdt, Nov 17, 2006, in forum: C Programming
    Replies:
    0
    Views:
    415
    fadliwdt
    Nov 17, 2006
  4. fadliwdt

    [help] String permutation with input

    fadliwdt, Nov 17, 2006, in forum: C Programming
    Replies:
    0
    Views:
    503
    fadliwdt
    Nov 17, 2006
  5. Michael Press

    More help requested on permutation code.

    Michael Press, Mar 24, 2006, in forum: Perl Misc
    Replies:
    8
    Views:
    131
    Michael Press
    Mar 30, 2006
Loading...

Share This Page