I have to write a recursive function called void recursive_call(int call)
based off an iterative function, that I need to convert to a recursive function that prints the exact following output:
1 2 3 4 5 6 7 8
This was written by call number 1
This was written by call number 2
This was written by call number 3
This was written by call number 4
This was written by call number 4
This was written by call number 3
This was written by call number 2
This was written by call number 1
This is the working iterative function for this:
1 2 3 4 5 6 7 8 9 10 11
void iterative_call()
{
int call = 1;
string statement = "This was written by call number ";
for (; call != 5; ++call) {
cout << statement << call << endl;
statement = " " + statement; // adds leading white space
}
for (--call; call != 0; --call)
cout << statement.erase(0,1) << call << endl; // removes leading white space
}
Any help with how to convert it would be fantastic. I know I can't use loops, and will have to call recursive_call(call++) but I'm not sure how to get this working.
Thanks!
1) They need to have a base case. This is the point at which your logic breaks out of the recursion. In your case, the base case is when call == 4.
2) They need to operate on subproblems or values that approach that base case. In your case, this is achieved by adding one the the argument for the next recursive call.
Give those two properties, how do you think you'd go about writing this? If you provide some code, it's easier to lend a hand on the parts you're struggling with.
@iHutch105 I would like to say I now understand, but the fact that it has to go up to 4 and then back down is really throwing me off. I don't have much clue about all how to start other than that if statements are needed.
@iHutch105
Obviously not following the specs, but I have rigged up this program that prints up to whatever number you have. So if I called recursive_call(5) this currently prints 0, 1, 2, 3, 4, 5. Is this at all on the right track?
@iHutch105
The trouble I'm having is the reverse, my current code (below) prints 1 2 3 4 3. I know that it cannot go down further as it hits (call < 4) and then adds again. So I'm wondering if you have any tips on how I'm supposed to make it double the four and then continuously go back to 1? Thanks!
edit: sorry. I was thinking 'can't alter the arguments values' not 'can't alter the argument list'.
Also, I forgot that static local variables can ve useful in recursive functions for transmitting state across recursion levels. Is that fair game?
Consider this version. It takes 1 integer argument as required.
void zig_zag( unsignedint n = 0 )
{
if( n < 4 )
{
std::cout << std::string( n, ' ' ) << "This was written by call number " << n+1 << '\n' ;
zig_zag(n+1) ;
std::cout << std::string( n, ' ' ) << "This was written by call number " << n+1 << '\n' ;
}
}
The version using the manipulator is somewhat clumsier (requires extra if statements):
1 2 3 4 5 6 7 8 9 10 11
void zig_zag2( unsignedint n = 0 )
{
if( n < 4 )
{
if( n > 0 ) std::cout << std::setw(n) << ' ' ;
std::cout << "This was written by call number " << n+1 << '\n' ;
zig_zag(n+1) ;
if( n > 0 ) std::cout << std::setw(n) << ' ' ;
std::cout << "This was written by call number " << n+1 << '\n' ;
}
}