-5

I have this code to do permutations of a string.

#include <iostream> 
#include <string.h>
using namespace std;

/* Prototipo de función */
void Permutaciones(char *, int l=0); 
void sort(string scadena[]);
//array global to copy all permutations and later sort

string array[900000];
int m=0;
int main() {
int casos;
cin>>casos; 
char palabra[casos][13];
for(int i=0;i<casos;i++)
    cin>>palabra[i];

for(int i=0;i<casos;i++){
    m=0;
    Permutaciones(palabra[i]);
    sort(array);
}

  sort(array);
system("pause");
return 0;
}

void sort(string scadena[]){
string temp;

for(int i=0;i<m;i++){
    for(int j=i+1;j<m;j++){
        if(scadena[i]>scadena[j]){
            temp=scadena[i];
            scadena[i]=scadena[j];
            scadena[j]=temp;    
        }           
    }
}

for(int i=0;i<m;i++){
    for(int j=1;j<m;j++){
        if(scadena[i]==scadena[j] && j!=i){
            for(int k=j;k <m; k++){
                scadena[k]=scadena[k+1];
            }
            m--;
            j--;
        }

    }
}   
for(int i=0;i<m;i++){
    cout<<scadena[i]<<endl;
}   
}


void Permutaciones(char * cad, int l) {
char c;    /* variable auxiliar para intercambio */
int i, j;  /* variables para bucles */
int n = strlen(cad);

for(i = 0; i < n-l; i++) {
  if(n-l > 2){
    Permutaciones(cad, l+1);
  } 
  else {
        array[m]=cad;
        m++;            
  }
  /* Intercambio de posiciones */
  c = cad[l];
  cad[l] = cad[l+i+1];
  cad[l+i+1] = c;
  if(l+i == n-1) {
     for(j = l; j < n; j++){
        cad[j] = cad[j+1];
     } 
     cad[n] = 0;
  }
 }
}

And the code generates all permutations fine, and later sorted the array and it works fine. But when i am intenting remove the repeated strings, the code show me somethings repeated, and not sorted.

Who can say me what is my error?

Deanie
  • 2,316
  • 2
  • 19
  • 35
  • 2
    Use a debugger or something similar and step through the code line by line. That makes for a good exercise anyway. – jogojapan Jul 02 '13 at 03:00
  • Iam an agriculture engineer, i do not know debugger – sergio ovalle Jul 02 '13 at 03:10
  • I would highly recommend you to **a)** always use English identifiers, or at least **b)** translate before posting to an English language website. Sooner or later, non-Spanish people will need to read your code. Like now. – Lstor Jul 02 '13 at 03:14
  • 6
    No offense meant, but your comment has just made it on my personal List of Great Stackoverflow Quotes. "I am an agriculture engineer. I do not know debugger." Beautiful. – jogojapan Jul 02 '13 at 03:18
  • 2
    Anyone think this question is is eerily similar to [another question](http://stackoverflow.com/questions/17396222/how-to-generate-all-permutations-of-an-array-in-sorted-order) by another user with coincidentally similar name? – Borgleader Jul 02 '13 at 03:19
  • 1
    But seriously, a debugger is a tool used to de-bug, i.e. to help remove the bugs (as in: problems/errors) from the code. One of its main functions is that it allows you to execute the program "step by step", usually roughly corresponding to "line by line", while observing how the values of the variables change. – jogojapan Jul 02 '13 at 03:19
  • "When in Rome, do as the Romans" - **"When programming, do as the programmers."** We would use a debugger. It is not hard to learn, and if you are ever going to write another program, the time is well invested. – Lstor Jul 02 '13 at 03:21

2 Answers2

1

You could have accomplished it easier using standard library:

#include <algorithm>

using namespace std;

int main() {
    int a[] = {1, 2, 5, 6, 7};
    int n = 5;
    do {
// print array a
    } while (next_permutation(a, a + n));
}

Unless the task was to implement it on your own. And of course make sure your array is sorted before you try to permutate it in this way, otherwise you will miss some permutations.

imslavko
  • 6,596
  • 2
  • 34
  • 43
0

HERE, is a simplest code for generating all combination/permutations of a given array without including some special libraries (only iostream.h and string are included) and without using some special namespaces than usual ( only namespace std is used).

void shuffle_string_algo( string ark )
{

//generating multi-dimentional array:

char** alpha = new char*[ark.length()];
for (int i = 0; i < ark.length(); i++)
    alpha[i] = new char[ark.length()];

//populating given string combinations over multi-dimentional array
for (int i = 0; i < ark.length(); i++)
    for (int j = 0; j < ark.length(); j++)
        for (int n = 0; n < ark.length(); n++)
            if( (j+n) <= 2 * (ark.length() -1) )
                if( i == j-n)
                    alpha[i][j] = ark[n];
                else if( (i-n)== j)
                    alpha[i][j] = ark[ ark.length() - n];

if(ark.length()>=2)
{
    for(int i=0; i<ark.length() ; i++)
    {
        char* shuffle_this_also = new char(ark.length());
        int j=0;
        //storing first digit in golobal array ma
        ma[v] = alpha[i][j];

        //getting the remaning string
        for (; j < ark.length(); j++)
            if( (j+1)<ark.length())
                shuffle_this_also[j] = alpha[i][j+1];
            else
                break;
        shuffle_this_also[j]='\0';

        //converting to string
        string send_this(shuffle_this_also);

        //checking if further combinations exist or not
        if(send_this.length()>=2)
        {
            //review the logic to get the working idea of v++ and v--
            v++;
            shuffle_string_algo( send_this);
            v--;
        }
        else
        {
            //if, further combinations are not possiable print these combinations 
            ma[v] = alpha[i][0];
            ma[++v] = alpha[i][1];
            ma[++v] = '\0';
            v=v-2;

            string disply(ma);
            cout<<++permutaioning<<":\t"<<disply<<endl;
        }
    }
}
}

and main:

int main()
{
string a;
int ch;
do
{
    system("CLS");
    cout<<"PERMUNATING BY ARK's ALGORITH"<<endl;
    cout<<"Enter string: ";
    fflush(stdin);
    getline(cin, a);

    ma = new char[a.length()];

    shuffle_string_algo(a);

    cout<<"Do you want another Permutation?? (1/0): ";
    cin>>ch;
} while (ch!=0);

return 0;
}

HOPE! it helps you! if you are having problem with understanding logic just comment below and i will edit.

ARK
  • 229
  • 1
  • 11