help a newb with recursion?

topic posted Thu, July 22, 2004 - 5:44 PM by  blank
Share/Save/Bookmark
Advertisement
hullo folks, first post on these boards. I'm learning c++ currently, and am working on an assignment with recursion.

the assignment is to write a recursive function named reverseWithinBounds, that has an argument that is an array of characters and two arguments that are bounds on array indices. the function has to reverse the order of those entries in the array whose indices are between the two bounds. so for example, if the array is
a[0] == 'A' a[1] == 'B' a[2] == 'C' a[3] == 'D' a[4] == 'E'

and the bounds are 1 and 4, then after the function is run the array elements should be:

a[0] == 'A' a[1] == 'E' a[2] == 'D' a[3] == 'C' a[4] == 'B'

i wrote mine so that the user can enter his own word and bounds, i opted to set it up for use with the word 'partytime'. here's my code -

#include<iostream>

using namespace std;

void reverseWithinBounds( char x[], const int bound1, const int bound2 );


int main()
{
char x[10];
int bound1, bound2;
char ch;

cout << "enter a nine letter word: " << endl;
for( int i = 0; i < 10; i++ ){
cin.get(ch);
x[i] = ch;
}
cout << "enter the first bound: " << endl;
cin >> bound1;
cout << "enter the second bound: " << endl;
cin >> bound2;

for( int j = 0; j < bound1; j++ )
cout << x[j];
reverseWithinBounds( x, bound1, bound2 );

return 0;

}




/******************************************************************/

void reverseWithinBounds( char x[],
const int bound1,
const int bound2 )
{

int i = bound1;
if( x[i] != '\n' ){
reverseWithinBounds( x, bound1, bound2 );
cout << x[i];
i++;
}
}

it compiles fine, it gets the information fine, but when it gets to the reverseWithinBounds function, it just shuts down. I'm sure it's something obvious causing the problem, I have a tendency to not see obvious problems ( occam and i are not friends )

greatly appreciate any help
posted by:
blank
Advertisement
Advertisement
  • Re: help a newb with recursion?

    Thu, July 22, 2004 - 7:30 PM
    nevermind! i see what my problem is now
    • Unsu...
       

      Re: help a newb with recursion?

      Sat, July 24, 2004 - 11:54 AM
      Recursion is a lovely thing. I do mostly php work, and squeed
      when the chance came up to write one, a database search for
      related entries omitting duplicate results. Like as if you go to
      get your car fixed and the mechanic laughs and pulls out this
      really strange looking wrench. Beautiful.