output produced by function call
For the following function can one of you guys take a look and see if what I have at the bottom is correct..
void f(char ch)
{
if (('A' <= ch) && (ch <= 'H'))
{
f(ch - 1);
cout << ch;
}
else
cout << endl;
}
Determine the output that will be produced by the following function calls
f('C')
I think it is B
f('G')
I think it is F
Re: output produced by function call
I'm assuming you meant for that to test >= 'A', rather than <=, because otherwise you're just going to get a bunch of gibberish no matter what.
Re: output produced by function call
no I double checked and the statement reads if (('A' <= ch) && (ch <= 'H'))
Re: output produced by function call
Why don't you implement it to see the result by yourself? It's a very small code, and you have already typed most of it...
Re: output produced by function call
Trace the recursion and you will see that both your answers are wrong. But as leoalvesmachado suggests, you might want to compile and run a program with this to confirm it.
Of course, one assumption here is that the character set has the letters from 'A' to 'Z' in contiguous alphabetical order, as in ASCII.
Re: output produced by function call
Quote:
Originally Posted by
jimJohnson123
no I double checked and the statement reads if (('A' <= ch) && (ch <= 'H'))
Well then, the problem is trivial, because that condition is going to be false for either of those inputs.
Re: output produced by function call
I think I figured it out...ABC for answer one and ABCDEFG for answer two?
Re: output produced by function call
Quote:
Originally Posted by Lindley
Well then, the problem is trivial, because that condition is going to be false for either of those inputs.
I may be missing something, but the condition should evaluate to true for both.
Quote:
Originally Posted by jimJohnson123
I think I figured it out...ABC for answer one and ABCDEFG for answer two?
Yes, although a newline is also printed before those outputs.
Re: output produced by function call
Quote:
Originally Posted by
laserlight
I may be missing something, but the condition should evaluate to true for both.
Okay, you're right. And I've been talking nonsense in this thread. I didn't realize the constant was on a different side in each expression----I just saw the same operator used twice and my brain went "Huzzwhaaa???".