|
-
May 25th, 2010, 11:37 AM
#1
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
-
May 25th, 2010, 11:46 AM
#2
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.
-
May 25th, 2010, 11:53 AM
#3
Re: output produced by function call
no I double checked and the statement reads if (('A' <= ch) && (ch <= 'H'))
-
May 25th, 2010, 12:00 PM
#4
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...
-
May 25th, 2010, 12:05 PM
#5
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.
-
May 25th, 2010, 12:06 PM
#6
Re: output produced by function call
 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.
-
May 25th, 2010, 12:06 PM
#7
Re: output produced by function call
I think I figured it out...ABC for answer one and ABCDEFG for answer two?
-
May 25th, 2010, 12:09 PM
#8
Re: output produced by function call
 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.
 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.
-
May 25th, 2010, 05:35 PM
#9
Re: output produced by function call
 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???".
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|