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]; :)

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

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

Probably because you are giving input 1 4 instead of 1 3 .

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 :)

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.