Hey, I am having trouble implementing a c++ function that will allow the user to enter a lower and upper bounds and reverse everything in a char[] in between the bounds, so if array is,

char[] = {'A','B','C','D','E'};

and the user input was 1 and 4 for 1 as the lower bounds
and 4 being the upper bounds, then the output should be,


I know I am getting close with the code I have right now,
but I am not sure where my logic is incorrect...

the code I have for this function so far is,

void reOrder(char a1[], int start, int end){ // NOT WORKING
char temp;

if(start > end) {
cout << " " << endl;
for(int i = 0; i < 5; i++){
cout << a1[i] << " ";
cout << "" << endl;
temp = a1[end];
a1[end] = a1[start];
a1[start] = temp;
reOrder(a1, ++ start, -- end);


and my output is incorrect,


If anyone could help me figure this out I will greatly appreciate it, I am trying to learn recursion.

5 Years
Discussion Span
Last Post by Lerner

Your function and its output are correct.

If you consider the interval to be inclusive [start, end], then inverting the sequence [1,4] should produce the output AEDCB (because 4 is the last element).

If you consider the interval to be non-inclusive of the end-point, as in [start,end), then you should just call your function with end - 1 .


Use pencil and paper to work it out.

It appears that you have started with the variable called end at too high a value.

Also the termination conditions might need to be adjusted somewhat.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.