CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2010
    Posts
    76

    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

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  3. #3
    Join Date
    May 2010
    Posts
    76

    Re: output produced by function call

    no I double checked and the statement reads if (('A' <= ch) && (ch <= 'H'))

  4. #4
    Join Date
    May 2010
    Posts
    24

    Lightbulb 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...

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: output produced by function call

    Quote Originally Posted by jimJohnson123 View Post
    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.

  7. #7
    Join Date
    May 2010
    Posts
    76

    Re: output produced by function call

    I think I figured it out...ABC for answer one and ABCDEFG for answer two?

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: output produced by function call

    Quote Originally Posted by laserlight View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured