Lexicographic algorithm
I'm trying to make this algorithm work, but it keeps telling me that my declaration array is not correct because it needs to have a constant value. Array declaration can be found in the main

#include<iostream>
#include<array>
using namespace std;

void nextPermutation(char* v, int n) { //function declaration for next permutation
    int i, j;                        //variable declaration
    for (i = n - 1; i > 0 && v[i - 1] >= v[i]; i--);   // we go to the index till v[i]>v[i-1]
    if (i > 0) {    //if i is greater than 0 than we have atleast one possible next permutation
        for (j = i; j <= n - 1 && v[j] > v[i - 1]; j++);   // we find the largest element after index i
        swap(v[i - 1], v[j - 1]);           // we swap it after we find it
    }
    j = n - 1;              //j is assigned the last element
    while (i < j) {         //we reverse the order from i to j element
        swap(v[i], v[j]);
        i++;
        j--;
    }
}
int main()
{

    int n;      //variable for the data lenght
    cout << "Enter the size of the array input numerical or string: ";
    cin >> n;
    char v[n];  //array declaration
    for (int i = 0; i < n; i++)cin >> v[i];  //input in the array
    nextPermutation(v, n);           //function called
    for (int i = 0; i < n; i++)cout << v[i] << " ";    //display the output
    return 0;
}`

I used a C++ online compiler and it didn't like the stray single quote on line 30 but for the number ten it did something.
Could you have a compiler setup issue instead?

commented: No, my problem is an array declaration +0

Given the code ran fine on an online c++ IDE and compiler I'll have to have a better explanation of what the issue is.

commented: I’ve been using visual studio 2022 do you think there’s an specific setup that I have to use ? +0

Don’t know if this helps but…
If your compiler has __STDC_NO_VLA__ = 1 then it doesn’t support variable length arrays (C++ 11)

commented: Most likely the issue. I've noticed that beyond the code, I have to know the IDE and compiler. +16

but it keeps telling me that my declaration array is not correct because it needs to have a constant value

That's a C restriction. Chances are your C++ compiler follows C standards for some reason.

You can always do it the "classic" C way by using <stdlib.h> (in C++ it's <cstdlib>) and allocating the array manually (then releasing the memory when you no longer need it.

You cannot declare an array with variable length in C++. You can use a vector instead.

#include<iostream>
#include<vector>

using namespace std;

void nextPermutation(vector<char>& v) { //function declaration for next permutation
    size_t n = v.size();
    size_t i, j;                        //variable declaration
    for (i = n - 1; i > 0 && v[i - 1] >= v[i]; i--);   // we go to the index till v[i]>v[i-1]
    if (i > 0) {    //if i is greater than 0 than we have atleast one possible next permutation
        for (j = i; j <= n - 1 && v[j] > v[i - 1]; j++);   // we find the largest element after index i
        swap(v[i - 1], v[j - 1]);           // we swap it after we find it
    }
    j = n - 1;              //j is assigned the last element
    while (i < j) {         //we reverse the order from i to j element
        swap(v[i], v[j]);
        i++;
        j--;
    }
}

int main()
{

    int n = 0;      //variable for the data lenght
    cout << "Enter the size of the array input numerical or string: ";
    cin >> n;
    vector<char> v(n);  //vector declaration
    for (int i = 0; i < n; i++)cin >> v[i];  //input in the vector
    nextPermutation(v);           //function called
    for (int i = 0; i < n; i++)cout << v[i] << " ";    //display the output
    return 0;
}

Try this one, however I copied it from somewhere.

#include <iostream>
#include <vector>
using namespace std;

void nextPermutation(vector<char>& v) {
    int n = v.size(); // Get the size of the vector
    int i, j;
    for (i = n - 1; i > 0 && v[i - 1] >= v[i]; i--);

    if (i > 0) {
        for (j = i; j < n && v[j] > v[i - 1]; j++);
        swap(v[i - 1], v[j - 1]);
    }

    j = n - 1;
    while (i < j) {
        swap(v[i], v[j]);
        i++;
        j--;
    }
}

int main() {
    int n;
    cout << "Enter the size of the array input numerical or string: ";
    cin >> n;

    vector<char> v(n);

    cout << "Enter " << n << " elements: ";
    for (int i = 0; i < n; i++) cin >> v[i];

    nextPermutation(v);

    cout << "Next permutation: ";
    for (int i = 0; i < n; i++) cout << v[i] << " ";

    cout << endl;
    return 0;
}

Thanks

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.