I am having a problem with my code, it is suppose to be rearranging the characters inside the array, but every time i enter the lower bounds (x) and upper bounds (y), i rearranges the wrong alphabets. Here is my code

int main()
{
    char a[5]={'A','B','C','D','E'};
    int x, y;
    cout << "Input bounds for array" << endl;
    cin >> x >> y;
    rever(a, x, y);
    
    system("pause");
    return 0;
}

void rever(char a[], int x, int y)
{
     char reorder;
     if( x >= y)
     {
         for(int i = 0; i < 5; i++){
                 cout << a[i];
                 }
                 cout << endl;
                 return;
         }
     else{
         
     reorder = a[x];
     a[y] = a[x];
     a[x] = reorder;
     rever(a, ++x, --y);
     }
}

I enter 1 and 4 and my output is ABCCB

I think line 26 should be reorder = a[y]; :)

Now it returns AEDCB, when i should be recieving ADCBE.

Now it returns AEDCB, when i should be recieving ADCBE.

It's receiving "BDCE" (elements 1 - 4 of the array). If you want it to receive "ABCDE" you'd need to give it 0,4; instead of 1,4 :)

This question has already been answered. Start a new discussion instead.