Permutations without repetition C












-1














I have this code for permutation WITH repetition. Would anyone help to modify it to be WITHOUT repetition. I can't figure it out.



int array1 = {1, 2, 3}; //array can be {1,1,2,3} for example also
int array2[3];

void permWithRep (int array1, int array2, int last, int index){
int i, len = last+1;

enter code here
for ( i=0; i<len; i++ )
{
array2[index] = array1[i] ;

if (index == last){
for(int i = 0; i < 3; i++) {
printf("%d ", array2[i]);
}
printf("n");
}
else // Recur for higher indexes
permWithRep (array1, array2, last, index+1);
}
}

int main()
{
int len = sizeof(array1)/sizeof(int);
permWithRep (array1, array2, len-1, 0);
return 0;
}









share|improve this question
























  • Would you define what you mean by "repetition"? That may sound like a strange question, but I do not see any repeated code. Perhaps you are talking about the loops. Do you want to remove all loops? Is this a homework exercise?
    – paddy
    Nov 12 '18 at 21:26










  • For anyone unclear on the terminology of the question - en.wikipedia.org/wiki/Permutation#Permutations_with_repetition
    – Wumpus Q. Wumbley
    Nov 12 '18 at 21:49










  • What have you tried? What don't you understand about the problem?
    – chb
    Nov 12 '18 at 23:01
















-1














I have this code for permutation WITH repetition. Would anyone help to modify it to be WITHOUT repetition. I can't figure it out.



int array1 = {1, 2, 3}; //array can be {1,1,2,3} for example also
int array2[3];

void permWithRep (int array1, int array2, int last, int index){
int i, len = last+1;

enter code here
for ( i=0; i<len; i++ )
{
array2[index] = array1[i] ;

if (index == last){
for(int i = 0; i < 3; i++) {
printf("%d ", array2[i]);
}
printf("n");
}
else // Recur for higher indexes
permWithRep (array1, array2, last, index+1);
}
}

int main()
{
int len = sizeof(array1)/sizeof(int);
permWithRep (array1, array2, len-1, 0);
return 0;
}









share|improve this question
























  • Would you define what you mean by "repetition"? That may sound like a strange question, but I do not see any repeated code. Perhaps you are talking about the loops. Do you want to remove all loops? Is this a homework exercise?
    – paddy
    Nov 12 '18 at 21:26










  • For anyone unclear on the terminology of the question - en.wikipedia.org/wiki/Permutation#Permutations_with_repetition
    – Wumpus Q. Wumbley
    Nov 12 '18 at 21:49










  • What have you tried? What don't you understand about the problem?
    – chb
    Nov 12 '18 at 23:01














-1












-1








-1







I have this code for permutation WITH repetition. Would anyone help to modify it to be WITHOUT repetition. I can't figure it out.



int array1 = {1, 2, 3}; //array can be {1,1,2,3} for example also
int array2[3];

void permWithRep (int array1, int array2, int last, int index){
int i, len = last+1;

enter code here
for ( i=0; i<len; i++ )
{
array2[index] = array1[i] ;

if (index == last){
for(int i = 0; i < 3; i++) {
printf("%d ", array2[i]);
}
printf("n");
}
else // Recur for higher indexes
permWithRep (array1, array2, last, index+1);
}
}

int main()
{
int len = sizeof(array1)/sizeof(int);
permWithRep (array1, array2, len-1, 0);
return 0;
}









share|improve this question















I have this code for permutation WITH repetition. Would anyone help to modify it to be WITHOUT repetition. I can't figure it out.



int array1 = {1, 2, 3}; //array can be {1,1,2,3} for example also
int array2[3];

void permWithRep (int array1, int array2, int last, int index){
int i, len = last+1;

enter code here
for ( i=0; i<len; i++ )
{
array2[index] = array1[i] ;

if (index == last){
for(int i = 0; i < 3; i++) {
printf("%d ", array2[i]);
}
printf("n");
}
else // Recur for higher indexes
permWithRep (array1, array2, last, index+1);
}
}

int main()
{
int len = sizeof(array1)/sizeof(int);
permWithRep (array1, array2, len-1, 0);
return 0;
}






c permutation repetition






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 22:01

























asked Nov 12 '18 at 21:19









Krivi21

82




82












  • Would you define what you mean by "repetition"? That may sound like a strange question, but I do not see any repeated code. Perhaps you are talking about the loops. Do you want to remove all loops? Is this a homework exercise?
    – paddy
    Nov 12 '18 at 21:26










  • For anyone unclear on the terminology of the question - en.wikipedia.org/wiki/Permutation#Permutations_with_repetition
    – Wumpus Q. Wumbley
    Nov 12 '18 at 21:49










  • What have you tried? What don't you understand about the problem?
    – chb
    Nov 12 '18 at 23:01


















  • Would you define what you mean by "repetition"? That may sound like a strange question, but I do not see any repeated code. Perhaps you are talking about the loops. Do you want to remove all loops? Is this a homework exercise?
    – paddy
    Nov 12 '18 at 21:26










  • For anyone unclear on the terminology of the question - en.wikipedia.org/wiki/Permutation#Permutations_with_repetition
    – Wumpus Q. Wumbley
    Nov 12 '18 at 21:49










  • What have you tried? What don't you understand about the problem?
    – chb
    Nov 12 '18 at 23:01
















Would you define what you mean by "repetition"? That may sound like a strange question, but I do not see any repeated code. Perhaps you are talking about the loops. Do you want to remove all loops? Is this a homework exercise?
– paddy
Nov 12 '18 at 21:26




Would you define what you mean by "repetition"? That may sound like a strange question, but I do not see any repeated code. Perhaps you are talking about the loops. Do you want to remove all loops? Is this a homework exercise?
– paddy
Nov 12 '18 at 21:26












For anyone unclear on the terminology of the question - en.wikipedia.org/wiki/Permutation#Permutations_with_repetition
– Wumpus Q. Wumbley
Nov 12 '18 at 21:49




For anyone unclear on the terminology of the question - en.wikipedia.org/wiki/Permutation#Permutations_with_repetition
– Wumpus Q. Wumbley
Nov 12 '18 at 21:49












What have you tried? What don't you understand about the problem?
– chb
Nov 12 '18 at 23:01




What have you tried? What don't you understand about the problem?
– chb
Nov 12 '18 at 23:01












1 Answer
1






active

oldest

votes


















0














I tested this on few inputs and it works.

There is certainly a better way to do it, but that's what i did:



#include <stdio.h> 

int array1 = {1, 2, 3}; //array can be {1,1,2,3} for example also
int array2[3];
int fixed[3];



void permWithRep(int array1, int array2,int fixed_index, int size , int level){

if(level == size) return print_array(array2,size);

for(int k = 0; k< size; k++){

if(!is_present_in_fixed(fixed_index,level,k)){
fixed_index[level] = k;
array2[level] = array1[k];
permWithRep(array1,array2,fixed_index,size,level+1);
}

}
}

void print_array(int array, int size){
for(int k = 0; k < size; k++)printf("%d ",array[k]);
printf("n");
}

int is_present_in_fixed(int array, int size, int element ){
for(int k =0; k<size;k++)
if(element == array[k]) return 1;
return 0;
}



int main()
{
int len = sizeof(array1)/sizeof(int);
permWithRep (array1, array2, fixed, len, 0);
return 0;
}


What I did:




  • Add an array (fixed_index) that keep track of the already visited indexes for one permutation.

  • In the loop, check if the index of current element belongs to the index already visited in that permutation (is_present_in_fixed(fixed_index,level,k)).

  • If so, it just skip that element (it does not enter the if statement).

  • Otherwise adds that element to the visited for that permutation and go on with recursive call.

  • When all array has been iterated on (level == size), just print the results contained in array2.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270284%2fpermutations-without-repetition-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I tested this on few inputs and it works.

    There is certainly a better way to do it, but that's what i did:



    #include <stdio.h> 

    int array1 = {1, 2, 3}; //array can be {1,1,2,3} for example also
    int array2[3];
    int fixed[3];



    void permWithRep(int array1, int array2,int fixed_index, int size , int level){

    if(level == size) return print_array(array2,size);

    for(int k = 0; k< size; k++){

    if(!is_present_in_fixed(fixed_index,level,k)){
    fixed_index[level] = k;
    array2[level] = array1[k];
    permWithRep(array1,array2,fixed_index,size,level+1);
    }

    }
    }

    void print_array(int array, int size){
    for(int k = 0; k < size; k++)printf("%d ",array[k]);
    printf("n");
    }

    int is_present_in_fixed(int array, int size, int element ){
    for(int k =0; k<size;k++)
    if(element == array[k]) return 1;
    return 0;
    }



    int main()
    {
    int len = sizeof(array1)/sizeof(int);
    permWithRep (array1, array2, fixed, len, 0);
    return 0;
    }


    What I did:




    • Add an array (fixed_index) that keep track of the already visited indexes for one permutation.

    • In the loop, check if the index of current element belongs to the index already visited in that permutation (is_present_in_fixed(fixed_index,level,k)).

    • If so, it just skip that element (it does not enter the if statement).

    • Otherwise adds that element to the visited for that permutation and go on with recursive call.

    • When all array has been iterated on (level == size), just print the results contained in array2.






    share|improve this answer


























      0














      I tested this on few inputs and it works.

      There is certainly a better way to do it, but that's what i did:



      #include <stdio.h> 

      int array1 = {1, 2, 3}; //array can be {1,1,2,3} for example also
      int array2[3];
      int fixed[3];



      void permWithRep(int array1, int array2,int fixed_index, int size , int level){

      if(level == size) return print_array(array2,size);

      for(int k = 0; k< size; k++){

      if(!is_present_in_fixed(fixed_index,level,k)){
      fixed_index[level] = k;
      array2[level] = array1[k];
      permWithRep(array1,array2,fixed_index,size,level+1);
      }

      }
      }

      void print_array(int array, int size){
      for(int k = 0; k < size; k++)printf("%d ",array[k]);
      printf("n");
      }

      int is_present_in_fixed(int array, int size, int element ){
      for(int k =0; k<size;k++)
      if(element == array[k]) return 1;
      return 0;
      }



      int main()
      {
      int len = sizeof(array1)/sizeof(int);
      permWithRep (array1, array2, fixed, len, 0);
      return 0;
      }


      What I did:




      • Add an array (fixed_index) that keep track of the already visited indexes for one permutation.

      • In the loop, check if the index of current element belongs to the index already visited in that permutation (is_present_in_fixed(fixed_index,level,k)).

      • If so, it just skip that element (it does not enter the if statement).

      • Otherwise adds that element to the visited for that permutation and go on with recursive call.

      • When all array has been iterated on (level == size), just print the results contained in array2.






      share|improve this answer
























        0












        0








        0






        I tested this on few inputs and it works.

        There is certainly a better way to do it, but that's what i did:



        #include <stdio.h> 

        int array1 = {1, 2, 3}; //array can be {1,1,2,3} for example also
        int array2[3];
        int fixed[3];



        void permWithRep(int array1, int array2,int fixed_index, int size , int level){

        if(level == size) return print_array(array2,size);

        for(int k = 0; k< size; k++){

        if(!is_present_in_fixed(fixed_index,level,k)){
        fixed_index[level] = k;
        array2[level] = array1[k];
        permWithRep(array1,array2,fixed_index,size,level+1);
        }

        }
        }

        void print_array(int array, int size){
        for(int k = 0; k < size; k++)printf("%d ",array[k]);
        printf("n");
        }

        int is_present_in_fixed(int array, int size, int element ){
        for(int k =0; k<size;k++)
        if(element == array[k]) return 1;
        return 0;
        }



        int main()
        {
        int len = sizeof(array1)/sizeof(int);
        permWithRep (array1, array2, fixed, len, 0);
        return 0;
        }


        What I did:




        • Add an array (fixed_index) that keep track of the already visited indexes for one permutation.

        • In the loop, check if the index of current element belongs to the index already visited in that permutation (is_present_in_fixed(fixed_index,level,k)).

        • If so, it just skip that element (it does not enter the if statement).

        • Otherwise adds that element to the visited for that permutation and go on with recursive call.

        • When all array has been iterated on (level == size), just print the results contained in array2.






        share|improve this answer












        I tested this on few inputs and it works.

        There is certainly a better way to do it, but that's what i did:



        #include <stdio.h> 

        int array1 = {1, 2, 3}; //array can be {1,1,2,3} for example also
        int array2[3];
        int fixed[3];



        void permWithRep(int array1, int array2,int fixed_index, int size , int level){

        if(level == size) return print_array(array2,size);

        for(int k = 0; k< size; k++){

        if(!is_present_in_fixed(fixed_index,level,k)){
        fixed_index[level] = k;
        array2[level] = array1[k];
        permWithRep(array1,array2,fixed_index,size,level+1);
        }

        }
        }

        void print_array(int array, int size){
        for(int k = 0; k < size; k++)printf("%d ",array[k]);
        printf("n");
        }

        int is_present_in_fixed(int array, int size, int element ){
        for(int k =0; k<size;k++)
        if(element == array[k]) return 1;
        return 0;
        }



        int main()
        {
        int len = sizeof(array1)/sizeof(int);
        permWithRep (array1, array2, fixed, len, 0);
        return 0;
        }


        What I did:




        • Add an array (fixed_index) that keep track of the already visited indexes for one permutation.

        • In the loop, check if the index of current element belongs to the index already visited in that permutation (is_present_in_fixed(fixed_index,level,k)).

        • If so, it just skip that element (it does not enter the if statement).

        • Otherwise adds that element to the visited for that permutation and go on with recursive call.

        • When all array has been iterated on (level == size), just print the results contained in array2.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 23:05









        Ollaw

        940517




        940517






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270284%2fpermutations-without-repetition-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python