Hello, I'm very new at C++ and I have to write a code that uses a recursive function to reverse some characters within a boundary, so lets say I have A[1] == ‘A’ A[2] == ‘B’ A[3] == ‘C’ A[4] == ‘D’ A[5] == ‘E’ and I put in a boundary like reverse between 2 to 5 I will get
A[1] == ‘A’ A[2] == ‘E’ A[3] == ‘D’ A[4] == ‘C’ A[5] == ‘B’

So far this is what I have but I keep getting this link error and undefined symbol reallyReverse(char*, int, int)

include <iostream>
#include <string>
using namespace std;
void reversebound(int, int, int);

void reverse(char s[], int length);

void reallyReverse(char a[], int lo, int hi);

int main()
{
  char line[80];
  int start, end;
  char c;
  int length = 0;

  //
  // Get a string from the user
  //

  cout << "Enter a string up to 80 characters: ";
  cin.get(c);
  while (c != '\n' && length < 80)
    {
      line[length] = c;
      length++;
      cin.get(c);
    }

  //
  // If line long enough, test reallyReverse directly
  //

  if (length > 1)
    {
      cout << "Reversing elements 1.." << length/2 << endl;
      reallyReverse(line, 1, length/2);
      for (int i=0; i<length; i++)
	cout << line[i];
      cout << endl;

      // Restore original line
      reallyReverse(line, 1, length/2);
    }


  //
  // Reverse it
  //

  cout << "Reversing whole line" << endl;
  reverse(line, length);

  //
  // Print it
  //

  for (int i=0; i<length; i++)
    cout << line[i];
  cout << endl;

}

void reverse(char s[], int length)
{
  reallyReverse(s, 0, length-1);
}


void reallyReverse(char a[],int lo, int hi)
{
    if (lo>=hi)
	return;
	char temp;
    char *s2 = *a;
    temp = s2[lo];
    s2[lo] =s2[hi];
    s2[hi]=temp;
    reallyReverse(a,lo + 1, hi -1);
}

From a compilation viewpoint, I see nothing that should prevent this code from linking. I do see a missing # in line 1, which I assume you removed inadvertently, and a type error in line 75; but when those errors are corrected, the program compiles and links over here.

I did not check for run-time problems.

Really? I'm having problems with this function here,

void reallyReverse(char a[],int lo, int hi)
{
    if (lo>=hi)
	return;
	char temp;
    char *s2 = *a;
    temp = s2[lo];
    s2[lo] =s2[hi];
    s2[hi]=temp;
    reallyReverse(a,lo + 1, hi -1);
}

I'm trying to rewrite it, but not coming up with any other clever way to get the results I want.

As I said, I did not look at the contents of the code. You said there was a link error, so I cut and pasted the entire code, compiled and linked it, and found only the problems I cited.

Well, Can you please help me with this? I'm trying to stop the case but I get function missing argument in this function.

void reallyReverse(char a[],int lo, int hi)
 {
	if  (lo==hi) 
	   {
		
		 endl;
	      }
	else
	{ 
		char temp =a[lo];
		a[lo]= a[hi];
		a[hi] = temp;
		reallyReverse(a,lo++,hi++);
		
	}

 }

error C2568: 'identifier' : unable to resolve function overload
ssignment 4.cpp(81) : warning C4551: function call missing argument list
I get this error
Thank You.

What is the purpose of line 6? It makes no sense.

For that matter, the call to reallyReverse in line 13 also makes no sense, because the value of lo++ (and, similarly, hi++) is the same as the value of lo. Of course, it has the side effect of incrementing lo (and hi), but since you don't use those variables again, the call to reallyReverse has exactly the same effect as if you had written

reallyReverse(a,lo,hi);

so I get suspicious: Why didn't you just do that instead? It must be that you think the uses of ++ make a difference, and they don't in this case.

I actually I made a mistake in that code, what I'm trying to do there is swap from hi to lo so it should actually be

reallyReverse(a, lo++, hi--)

for line 6, I'm trying to stop the case, but I can't think of anything, so I guess I thought endl will end it.

reallyReverse(a, lo++, hi--) is meaningless for the same reason I already cited.

I'm thinking that maybe it would be a good idea for you to spend some more time understanding the basics of how C++ expressions work before you tackle recursion.

I see, The problem is, this is my first C++ course and it's online. I've been reading alot about methods used in C++ but not enough coding, I understand concepts.

But I don't think the reallyReverse(a,lo++, hi--) is meaningless because hi starts of with the last character in the array, and lo starts of with the 2nd character in the array. the content array index "hi" is swaped with the content of array index "lo". I increment the index, which is lo++ then I call the function then swap again, and i do the same for hi--. Maybe this should give you a better understanding of what I'm trying to accomplish.

But I don't think the reallyReverse(a,lo++, hi--) is meaningless because hi starts of with the last character in the array, and lo starts of with the 2nd character in the array. the content array index "hi" is swaped with the content of array index "lo". I increment the index, which is lo++ then I call the function then swap again, and i do the same for hi--. Maybe this should give you a better understanding of what I'm trying to accomplish.

I understand it. The trouble is that you're wrong. I wish I had a less harsh way of putting it.

Here is why it's wrong. Consider the following very simple function:

void g(int);

void f(int n)
{
    g(n++);
}

The use of n++ in the call to g is a mistake, plain and simple, because calling g(n++) has the same effect of calling g(n) except that as a side effect, it increments n and then throws it away without doing anything with it.

Now let's change the code slightly:

void g(int);

void f(int n)
{
    if (n == 0)
        g(n++);
}

Here, the use of ++ in the call to g(n++) is a mistake for exactly the same reason that it was a mistake in the first example.

Finally, let's change the code again to make it recursive:

void f(int n)
{
    if (n == 0)
        f(n++);
}

Here, we have changed the call to g into a recursive call to f. This change makes no difference in the fact that the use of ++ in the call is a mistake. It is a mistake for exactly the same reason that it was a mistake in the first and second examples.

hmm, ok for example I have this in the book I'm using and I don't see the difference

#include<iostream>
using namespace std;
void cheers(int n);
 int main()
 {
  cheers(3);
  return 0;
 }
 void cheers(int n)
 {
 if (n==1)
 {
 cout<< "Hurray\n";
 }
else
 {
 cout<< "Hip";
  cheers(n-1);  //Are you saying this won't work either?
 }
}

Are you saying line 18 won't work either in this program? Because this works and I don't see the difference between this and what I wrote. i mean I can changed it to lo+1 and hi-1.. but that's the same as lo++ and hi++

If you think that lo+1 is the same as lo++, you're wrong. To see for yourself, try running the following program:

#include <iostream>

int main()
{
    int m = 42, n = 42;

    std::cout << m++ << " " << n+1 << std::endl;
}

haha, GOOD POINT!! Thank you. Can you give me a hint on how the if(lo==hi)should check to see if it's NOT the base case? I just can't think of an expression to stop the recursion. Between line 6 to 9

void reallyReverse(char a[],int lo, int hi)
 {
	if  (lo==hi) 
	   {
		
		 endl;
	      }
	else
	{ 
		// Swaping the hi and lo characters
		char temp =a[lo];
		a[lo]= a[hi];
		a[hi] = temp;
		reallyReverse(a,lo+1,hi+1);
		
	}

 }

How about starting by writing down a clear description of what the function is supposed to do? In particular: Does the first character affected have index lo, lo-1, or lo+1? Does the last character have index hi, hi-1, or hi+1?

the function will take the arguments from reallyReverse(line, 1, length of string) Then swap out index [1] with the last index lets say [6]. That is what the function of reallyReverse is for. Hence this is why I wrote this code.

void reallyReverse(char a[],int lo, int hi)
 {
	if  (lo==hi) 
	   {
		
		 endl;
	      }
	else
	{ 
		// Swaping the hi and lo characters
		char temp =a[lo];
		a[lo]= a[hi];
		a[hi] = temp;
		reallyReverse(a,lo+1,hi+1);
		
	}

 }

The code you posted has at least three problems.

1) I still don't understand the point of the "endl" statement. It doesn't actually do anything.

2) Passing hi+1 in the recursive call cannot be right, because it guarantees that you will attempt to access data beyond the end of your string.

3) Even after you fix the problem in (2), if you call reallyReverse( /*anything*/, 1, 2), the program never terminates.

I don't think it would be fair to tell you what to change to fix these problems, but at least you know where to look.

To answer your questions
1) I originally used the "endl" statement to end the recursion, what the book refer to as stopping case. That didnt workout too well so I just put cout<< "end";, because to be honest I just don't know how else to stop it.

2) I made a mistake in that code when I just run it, i changed it to reallyReverse(a,lo+1,hi-1);

3)This answer is pretty much as the same for answer 1, I'm using mycodeMate to help me and the hint that is given is "The base case occurs when lo and hi meet or cross, that is, when there are no characters in the substring to reverse, since there is nothing to do in the base case, the condition in the if should check to see if it's NOT the base case." I honestly don't understand that part. "Check to see if's the base case"

My code now looks like this

void reallyReverse(char a[],int lo, int hi)
 {
	if  (lo==hi) 
	   {
		
		 cout<<"end";
	      }
	else
	{ 
		// Swaping the hi and lo characters
		char temp =a[lo];
		a[lo]= a[hi];
		a[hi] = temp;
		reallyReverse(a,lo+1,hi-1);
		
	}

 }

Can you at least help me with the base or stopping case?

I figured it out by way, the only problem is the memory dump i get in my answer, not sure how to fix that.

#include "stdafx.h"
using namespace std;

void reverse(char s[], int length);

void ParReverse(char a[], int low, int hi);

int main()
{
  char line[80];
  int start,end;
  char c;
  int length = 0;

  //
  // Get a string from the user
  //

  cout << "Enter a string up to 20 characters: ";
  cin.get(c);
  while (c != '\n' && length < 20)
    {
      line[length] = c;
      length++;
      cin.get(c);
    }

  //
  // If line long enough, test reallyReverse directly
  //

  if (length > 1)
    {
		cout<< "Enter where you want to start reversing,can't be more than input characters: ";
		cin>> start;
      cout << "Reversing elements.."<< start << "..through.." << length << endl;
      ParReverse(line, start, length);    //
      for (int i=0; i<length; i++)
	cout << line[i];
      cout << endl;

      // Restore original line
      ParReverse(line, 1, length);
    }


  //
  // Reverse it
  //

  cout << "Reversing whole line" << endl;
  reverse(line, length);

  //
  // Print it
  //

  for (int i=0; i<length; i++)
    cout << line[i];
  cout << endl;

}

void reverse(char s[], int length)
{
  ParReverse(s, 0, length-1);
}


 void ParReverse(char a[],int low, int hi)
 {
	if  (low>=hi)     //base case
	   {
		
		 return;
	      }
	else
	{ 
		// Swaping the hi and lo characters
		char temp =a[low];
		a[low]= a[hi];
		a[hi] = temp;
		ParReverse(a,low+1,hi-1); // Increment low and decrement high till they are equal then go to base case
		
	}

 }

THank You by the way for the help.

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.