Why am i getting this output?

Im trying to write a program that outputs the next permutation of a numerical string in lexcographic order. My issue is why my program is outputing in a wierd format. For some reason on the print of the final string, if it was run through the Reverse_Tail function, which reverse the order of the tail digits, the output would be as seen below:


Normal:

2 1 3 4

Weird:

2
1 3 4

Note: all of the funtions work like they were ment to, its just that the output is weird.

Here is the Reverse_Tail function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Reverse_Tail(vector<char>& List,int k){
  
  int i = k+1;
  
  int lim = (List.size() - i)/2;
  
  int len = List.size()-1;

  while(i < List.size() - lim){
    
    List = Swap(List,i,len);
    len--;
    i++;

}

}


and here is the whole program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<iostream>
#include<vector>

using namespace std;

void Reverse_Tail(vector<char>& List,int k);
vector<char> Swap(vector<char> InputList,int k, int l);
vector<char> GetInput();
int Find_K(vector<char> List);
int Find_l(vector<char> List,int k);

int main(){
  
  vector<char> Test2,Test = GetInput();
  
  int K = Find_K(Test);
  int l = Find_l(Test,K);
 
  for(int i=0;i<Test.size();i++){
  
     cout << Test[i] << " " ;
      
}
  
 cout << endl << "K = " << K << endl<< "l = " << l << endl<< endl;
  
  Test2 = Swap(Test,K,l);
  //Ran the next for loop spit vector before the Reverse_Tail function and it does display normally
     for(int i=0;i<Test2.size();i++){
  
     cout << Test2[i] << "  " ;
      
}
  cout << endl<< endl;
  
  Reverse_Tail(Test2,K);
  // now when i run the same loop again it displays incorrectly

    for(int i=0;i<Test2.size();i++){
  
     cout << Test2[i] << "  " ;
      
}
  cout << endl<< endl;
  return 0;
}

void Reverse_Tail(vector<char>& List,int k){
  
  int i = k+1;
  
  int lim = (List.size() - i)/2;
  
  int len = List.size()-1;

  while(i < List.size() - lim){
    
    List = Swap(List,i,len);
    len--;
    i++;

}

}

vector<char> Swap(vector<char> InputList,int k, int l){
  
  vector<char> OutList = InputList;
  
  int Temp = OutList[l];
  
  OutList[l] = InputList[k];
  
  OutList[k] = Temp;
  
  return OutList;
  
}

int Find_l(vector<char> List,int k){
  
  int l=List.size()-1;
  
  while(List[l] < List[k]){
    
    l--;
    
  }
  
  return l;
  
}


//Remeber k is an index
int Find_K(vector<char> List){
  
  int k = List.size()-2;
  
  while(List[k] > List[k+1]){
    
    k--;
    
  }
  
  if(k == List.size()-1){
    
    return -1;
    
  }
  
  return k;
  
}

vector<char> GetInput(){

vector<char> InputString;
 
cout << "Please input the string of symbols you would like to gain all permutations of: ";

char temp = 0 ;

while( temp != '\n' ){

cin.get(temp);

InputString.push_back(temp);

}

return  InputString;

}
Last edited on
while( temp != '\n' ){

cin.get(temp);

InputString.push_back(temp);

}
Like this, exactly one newline will get into the string.
ok i see thanks alot!
ok new problem, i modified it so the program computes all of the permutations but i am getting a memory error:



terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted




here is the current program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include<iostream>
#include<vector>

using namespace std;

vector<char> Next_Permutation(vector<char> InList);
void Reverse_Tail(vector<char>& List,int k);
vector<char> Swap(vector<char> InputList,int k, int l);
vector<char> GetInput();
int Find_K(vector<char> List);
int Find_l(vector<char> List,int k);
int Factorial(int n);

int main(){
 
 vector<char> Input = GetInput();//Getting initial input use 1234 for test
  
 int limit = Factorial(Input.size()); // finds how many permutations will be made
 
 vector<char>* AllPerms = new vector<char>[limit]; //AllPerms is the collection of all permutations(it is an array of vectors where each vector is a permutation)
 
 AllPerms[0] = Input; //setting intial permutation;
 
 for(int i=1;i<2;i++){
  
   
   // here is where i believe the program crashes. I've tried      test = Next_Permutation(AllPerms[i-1])       then 
   // doing        AllPerms[i] = Test          and the program runs through the first line fine but crashed on AllPerms[i] = Test?
   
  AllPerms[i] = (Next_Permutation(AllPerms[i-1]));
   
}
 for(int j=0; j < limit;j++){
   
  for(int i=0;i<AllPerms[j].size();i++){
  
     cout << AllPerms[j][i] << " " ;
      
}

cout << endl;

 }
 //cout << endl << "K = " << K << endl<< "l = " << l << endl<< endl;
  

  cout << endl<< endl;
  return 0;
}

int Factorial(int n){
  
  if(n==0)
    return 1;
  else
    return n*Factorial(n-1);
  
}

vector<char> Next_Permutation(vector<char> InList){
  
  int K = Find_K(InList);
  int l = Find_l(InList,K);
  
  vector<char> Output = Swap(InList,K,l);
  
  Reverse_Tail(Output,K);
  
}

void Reverse_Tail(vector<char>& List,int k){
  
  int i = k+1;
  
  int lim = (List.size() - i)/2;
  
  int len = List.size()-1;

  while(i < (List.size() - lim -1)){
    
    List = Swap(List,i,len);
    len--;
    i++;

}

}

vector<char> Swap(vector<char> InputList,int k, int l){
  
  vector<char> OutList = InputList;
  
  int Temp = OutList[l];
  
  OutList[l] = InputList[k];
  
  OutList[k] = Temp;
  
  return OutList;
  
}

int Find_l(vector<char> List,int k){
  
  int l=List.size()-1;
  
  while(List[l] < List[k]){
    
    l--;
    
  }
  
  return l;
  
}

int Find_K(vector<char> List){
  
  int k = List.size()-2;
  
  while(List[k] > List[k+1]){
    
    k--;
    
  }
  
  if(k == List.size()-1){
    
    return -1;
    
  }
  
  return k;
  
}

vector<char> GetInput(){

vector<char> InputString;
 
cout << "Please input the string of symbols you would like to gain all permutations of: ";

char temp = 0 ;

while( temp != '\n' ){

cin.get(temp);

InputString.push_back(temp);

}

InputString.pop_back();

return  InputString;

}
Topic archived. No new replies allowed.