In my program below, I am having problems running my program. The error I receive is "could not convert `nextPermutation(((char*)(&elements)), n)' to `bool'" I just want to print out the permutation of ABC. Will someone please check my nextPermutation function to see if everything is correct. I know there a function next_permutation function in #include <algorithm>, but I want try it manually.

Example Input:

"Enter the string"

A,B,C

Output:

{a, b, c}
{a, c, b}
{b, a, c}
{b, c, a}
{c, a, b}
{c, b, a}

Program:

#include <iostream>


using namespace std;

void nextPermutation(char elements[], int n);


int main()
{
    
 char elements[100];
 int n = 3;
 
 
 cout<<"Enter the string"<<endl;
 
 for (int i = 0; i<n; i++)
 {
 cin>>elements;
 }
 
 
 do
 {
 cout<<elements;
 }while(nextPermutation(elements,n));



system("PAUSE");
return 0;

}



void nextPermutation( char elements[], int n )
{          
                    
  char j = n-1;
  int k,r,s;
  
  while (elements[j] > elements[j+1])
  {
   j = j-1;
  }
  
   k = n;
  
  while (elements[j] > elements[k])
  {
    k = k-1;
  }
  
  swap(elements[j],elements[k]);
  
   r = n;
  
   s = j+1;
  
  while (r > s)
  {
    swap(elements[r],elements[s]);
    
    r = r-1;
    
    s = s+1;
  }
}

> while(nextPermutation(elements,n));
But you marked the function as returning void.

You need to return a value indicating if there are more permutations.

Thanks for your reply.

I made some changes in main to help progress in my program, and I hope you can help me futher. Right now, my program is outputting just "AC". I have no clue what happened to "B", and I am trying to resolve that. However my goal is to print out the permutation of "ABC". If there is any advice to help my function resolve this problem, I will appreciate it.

Current Output:
ac

Expected Output:
abc
acb
bac
bca
cab
cba


Program:

#include <iostream>


using namespace std;

int nextPermutation(char elements[], int n);


int main()
{
    
 char elements[] = {'A','B','C'};
 int n = 3;
 
 nextPermutation(elements,n);
 
 for (int i = 0; i < n; i++)
 {
 cout<<elements[i];
 }



system("PAUSE");
return 0;

}



int nextPermutation( char elements[], int n )
{          
                    
  char j = n-1;
  int k,r,s;
  
  while (elements[j] > elements[j+1])
  {
   j = j-1;
  }
  
   k = n;
  
  while (elements[j] > elements[k])
  {
    k = k-1;
  }
  
  swap(elements[j],elements[k]);
  
   r = n;
  
   s = j+1;
  
  while (r > s)
  {
    swap(elements[r],elements[s]);
    
    r = r-1;
    
    s = s+1;
  }
}

> while (elements[j] > elements[j+1])
j starts of as n-1, which means j+1 is actually n (which is also off the end of the array.

Also, what's stop the j=j-1 going negative?

Thanks again. I have did some work on my function and this is the output(s) that am receiving. The letters "abc" seem correct, but the names are not. However, after the output is printed to the screen, I get the error "Program.exe has stopped working" and then I am prompted to close the window.

input:
a
b
c

output:
a b c
a c b
b a c
b c a
c a b
c b a

input:
Jerry
Matt
Bill

output:
Jerry Matt Bill
Matt Bill Jerry
Matt Jerry Bill

Program:

#include <iostream>

using namespace std;

bool nextPermutation(string elements[], int n);


int main()
{
    
 string elements[1000];
 const int n = 3;


cout<<"Enter the string"<<endl;
for (int i = 0; i<n; i++)
{
cin>>elements[i];
}
 
do
{
cout<<elements[0]<<" "<<elements[1]<<" "<<elements[2]<<endl;
}
 while (nextPermutation(elements,n));




system("PAUSE");
return 0;

}
bool nextPermutation(string elements[], int n )
{

int j = n-1;

while (elements[j] > elements[j+1])
{
j = j-1;
}
if(j==-1) return false;

int k = n;

while (elements[j] > elements[k])
{
k = k-1;
}

swap(elements[j],elements[k]);

int r = n-1; 

int s = j+1;

while (r > s)
{
swap(elements[r],elements[s]);

r = r-1;

s = s+1;
}
return true;
}

I am having trouble finding out the problem that is causing my result below. I feel like I am almost there to the correct result, but need another push.

input:

Jackie
Kevin
Eric

output:

Jackie Kevin Eric
Kevin Eric Jackie
Kevin Jackie Eric

#include <iostream>


using namespace std;

bool nextPermutation(string elements[], int n);



int main()
{
    
 string elements[1000];
 const int n = 3;


cout<<"Enter the string"<<endl;
for (int i = 0; i<n; i++)
{
cin>>elements[i];
}
 
do
{
cout<<elements[0]<<" "<<elements[1]<<" "<<elements[2]<<endl;
}
 while (nextPermutation(elements,n));




system("PAUSE");
return 0;

}
bool nextPermutation(string elements[], int n )
{

int j = n-2;

while (j>=0 && elements[j] > elements[j+1])
{
--j;
}
if(j==-1) return false;

int k = n-1;

while (j>=0 && elements[j] > elements[k])
{
--k;
}

swap(elements[j],elements[k]);

int r = n-1;

int s = j+1;

while (r > s)
{
swap(elements[r],elements[s]);

r = r-1;

s = s+1;
}
return true;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.