Recursion

May 12, 2019 at 11:34pm
I need to write a function that will swap two elements in an array of chars. This needs to be done recursively. I can swap the elements by creating a temporary variable but that handles the whole job and there is no room left for recursion. I have some other code up as an attempt but it is taking advantage of the swap function so I feel that it is not truly recursive. Can someone please critique this and offer some suggestions for a recursive formuala if needed?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
  
void reverseWithinBounds(char ptr[], int first, int second)
{
	if (first != second)
            return swap(ptr[first], ptr[second]);

	else {
	    first++;
	    second--;
	}
	
	reverseWithinBounds(ptr, first , second );
	
}
May 12, 2019 at 11:41pm
Both your description and your code are unclear.

Are you trying to reverse an array?

    "abcd"    →    "dcba"
May 12, 2019 at 11:49pm
Just need to swap the char values of two elements sent in as arguments. In a recursive manner.
Last edited on May 12, 2019 at 11:49pm
May 12, 2019 at 11:51pm
The function is doing that but I think it is because of the swap function alone. I have the feeling that the function is not really recursive and I need to make it so.
May 13, 2019 at 12:20am
But your function is senseless. It can't possibly do anything.
Show it in a full program.
Show the input and the expected and actual output.

Last edited on May 13, 2019 at 12:20am
May 13, 2019 at 12:32am


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

int main()
{
	int first = 0, second = 4;

	char ptr[6] = { 'A', 'B', 'C', 'D', 'E', '\0' };
	cout << ptr << endl;
	reverseWithinBounds(ptr, first, second);
	cout << ptr << endl;

	system("pause");
	return 0;
}



void reverseWithinBounds(char ptr[], int first, int second)
{
	
	if (first != second)
		return swap(ptr[first], ptr[second]);

	else {
		first++;
		second--;
	}
	reverseWithinBounds(ptr, first , second );
	
}



// output:
ABCDE
EBCDA


May 13, 2019 at 12:34am
I think it is my code that is not making sense and the swap function is the part that is working regardless of any of the other code in the function.
May 13, 2019 at 12:46am
Output:

 In function 'int main()':
7:2: error: 'cout' was not declared in this scope
7:17: error: 'endl' was not declared in this scope
8:40: error: 'reverseWithinBounds' was not declared in this scope
11:16: error: 'system' was not declared in this scope
 In function 'void reverseWithinBounds(char*, int, int)':
21:38: error: 'swap' was not declared in this scope
21:38: error: return-statement with a value, in function returning 'void' [-fpermissive]


Anway, if you just want to swap two values then I don't see how that can be done "recursively". Recursion is a kind of looping, but you don't need any looping to swap two values.
May 13, 2019 at 12:55am
I realize it can be done done without looping. This is given as an assignment so it is more of an educational thing in regards to recursion, rather than the sole purpose of swapping two values.
May 13, 2019 at 1:12am
I assume you're trolling.
Good one. :-)
May 13, 2019 at 3:06am
Are you looking for something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
void my_reverse(int* begin, int* end)
{
    if (begin >= --end) 
        return;
    
    std::iter_swap(begin++, end);
    my_reverse(begin, end);    
}

void reverse_within_bounds(int* begin, int f, int l)
{
    my_reverse(begin + f, begin + l); 
}
May 13, 2019 at 5:36am
I'm sorry guys. The function is to swap 2 elements but also needs to reverse the elements in between.

I actually got it working and I have to create an additional function that takes a single argument as a cstring and calls the reverseWithinBounds() function to reverse it. I feel like my code is correct for this but for some reason when I cout the result nothing displays and I am not seeing why.

Quick summary:
Is Recursive-reverseWithinBounds() reverses only elements in between and including arguments.
Not Recursive-reverseCstring() calls above function to reverse entire string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

void reverseWithinBounds(char[] , int , int );
void reverseCstring(char ptr[]);

int main()
{
	const int SIZE = 256;
	char array[SIZE]{};
	int first, second;
	
	cout << "Enter a string: ";
	cin.getline(array, SIZE);
	cout << array << endl;
	cout << "1st element: ";
	cin >> first;
	cout << "2nd element: ";
	cin >> second;
	reverseWithinBounds(array, first, second);
	cout << array << endl;

	reverseCstring(array);
	cout << array << endl;   // why doesn't this display anything
	
	system("pause");
	return 0;
}



void reverseWithinBounds(char ptr[], int first, int second)
{
	if (first >= second)
		return;

	swap(ptr[first], ptr[second]);

		first++;
		second--;
	
	reverseWithinBounds(ptr, first , second);
}



void reverseCstring(char ptr[])
{
	int start, end;
	
	start = strlen(ptr) - strlen(ptr);
	end = strlen(ptr);

    reverseWithinBounds(ptr, start, end);
	cout << ptr << endl;                      // why doesn't this display anything
}
May 13, 2019 at 6:10am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;

void reverseWithinBounds(char[] , int , int );
void reverseCstring(char ptr[]);

int main()
{
    const int SIZE = 256;
    char array[SIZE]{};
    cout << "Enter a string: ";
    cin.getline(array, SIZE);

    reverseCstring(array);
    cout << quoted(array) << '\n' ;
}

void reverseWithinBounds(char ptr[], int first, int second)
{
    if( second > first )
    {
        swap( ptr[first], ptr[second] );
        reverseWithinBounds( ptr, first+1, second-1 ) ;
    }

}

void reverseCstring(char ptr[])
{
    if( ptr != nullptr && ptr[0] != 0 ) // if it is not an empty string
    {
        // note: first character is at position zero,
        //       last character (just before the null character at the end)
        //            is at position strlen(ptr)-1
        reverseWithinBounds( ptr, 0, strlen(ptr)-1 );
    }
}
May 13, 2019 at 6:29am
I definitely appreciate all the code. Would you be able tell me why my cout statements after reverseCstring are not displaying anything? I feel that my code is so close to correct and it seems similar to yours.
May 13, 2019 at 6:41am
I think I just figured it out. For my end variable I had to do strlen()-1 to get rid of the null.

1
2
3
4
5
6
7
 

end = strlen(ptr); 

// is now
end = strlen(ptr) - 1;


seeing your code helped me to realize that. Thank you!
Last edited on May 13, 2019 at 6:41am
May 13, 2019 at 6:50am
Thanks again!
Last edited on May 13, 2019 at 6:50am
Topic archived. No new replies allowed.