C++ Recursive function that reverses the order of an array's indexes between two bounds
Posted
by
CPT Kirk
on Stack Overflow
See other posts from Stack Overflow
or by CPT Kirk
Published on 2013-11-13T15:40:11Z
Indexed on
2013/11/13
15:54 UTC
Read the original article
Hit count: 164
I am trying to write a recursive function that has three arguments; an array and two array indexes. The function should reverse the order of the values between the two indexes. I would like to understand what is happening instead of just being told an answer.
Here is my code so far:
#include <iostream>
using namespace std;
char switchAroo(char a[], int b1, int b2);
int main()
{
char a[6] {'A', 'B', 'C', 'D', 'E', '\0'};
cout << a;
switchAroo(a, 2, 5);
return 0;
}
char switchAroo(char a [], int b1, int b2)
{
char temp;
if (b1 == b2)
cout << "The array after switchAroo is " << a << endl;
else
{
temp = a[b1];
a[b1] = a[b2];
a[b2] = temp;
b1++;
b2--;
return switchAroo(a, b1, b2);
}
}
I am getting the following warning code:
warning C4715: 'switchAroo' : not all control paths return a value
Any help would be greatly appreciated.
© Stack Overflow or respective owner