Thread: Recursive Functions

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    16

    Recursive Functions

    Show the exact screen-output of each of the following programs


    Code:
    #include<stdio.h>
    void rev( ) //function
    {
       char ch;
       scanf("%c", &ch); //reads what you entered in main
       if (ch < 'z')    //general case   
          rev ();
       printf ("%c", ch); //base case
    } //end of function
    
    void main()
    {
       printf("Enter any word and then press enter\n");
       rev (); //function call
    } //end of main
    
    Hint: Assume the user’s  input was: ysaezr
    Answer:


    Code:
    Enter any word and then press enter
    ysaezr
    zeasy
    i dont konw how to get the above answer

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    41
    Modify the statement
    printf ("%c", ch); //base case
    to
    printf ("%c, address = %d\n", ch, &ch); //base case

    In the above e.g
    if (ch < 'z') keeps calling rev() till it finds 'z'. Every call to rev() creates a new set of variable ch, which is getting printed after all the calls to rev() are completed.
    The statement
    printf ("%c, address = %d\n", ch, &ch); //base case
    shows the address of ch associated with each rev() call.

    finally it prints all the var in the reverse order as the user entered, because the var are stored after each recursion in buffer.
    so 'y' stored first goes to the last position.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    1. main returns int, not void
    2. This looks like C not C++
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    25
    Quote Originally Posted by jack999
    Show the exact screen-output of each of the following programs

    Answer:


    Code:
    Enter any word and then press enter
    ysaezr
    zeasy
    Is there something weird with this program ? Why doesn't it stop when it gets 'z' as the base case. Here we see the program let user enter the character 'r' after it received 'z'. What is wrong with this ?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    > What is wrong with this ?
    It's doing the reverse thing in a strange way, yes, but it was probably written to demonstrate recursion on some test from school. You fail.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    25
    Quote Originally Posted by citizen
    It's doing the reverse thing in a strange way, yes, but it was probably written to demonstrate recursion on some test from school. You fail.
    So would you please explain why doesn't it stop calling itself after received 'z' ? when user entered 'z' as input, the program would meet the base case's condition, how come it lets us enter the next char after 'z' ?

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    > So would you please explain why doesn't it stop calling itself after received 'z' ?

    Try running it. It does stop calling itself after the user enters z. You don't see the results until you hit [enter] though, so scanf just leaves a bunch of jargon in the input buffer. scanf is horrible at its job as you know.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    25
    Quote Originally Posted by citizen
    > So would you please explain why doesn't it stop calling itself after received 'z' ?

    Try running it. It does stop calling itself after the user enters z. You don't see the results until you hit [enter] though, so scanf just leaves a bunch of jargon in the input buffer. scanf is horrible at its job as you know.
    Actually, I didn't test this program, just wondered when I was looking at the way it works as the sample represented above. I am not similar with C code (scanf, printf,...), that was why I haven't run it.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >scanf is horrible at its job as you know.
    scanf is wonderful at its job. Most people just don't understand its job.

    >i dont konw how to get the above answer
    Trust it or trace it. There's a certain measure of trust that comes with recursion, mostly because all but the simplest of functions are difficult to trace in a reasonable amount of time. Here's the trace for your function:
    Code:
    Read 'y', less than 'z', call rev()
      Read 's', less than 'z' call rev()
        Read 'a', less than 'z' call rev()
          Read 'e', less than 'z', call rev()
            Read 'z', not less
            Print 'z'
          Print 'e'
        Print 'a'
      Print 's'
    Print 'y'
    Keep in mind that when one call to rev returns, it plops execution right back where it left off. So upon the return of rev, the next statement is the printf.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops or recursive functions?
    By mariano_donati in forum C Programming
    Replies: 5
    Last Post: 05-12-2008, 12:41 PM
  2. recursive function
    By technosavvy in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 05:42 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM