Thread: understanding output

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    understanding output

    I am not following this recursive function. Please help me code this to get the value of n as it hits each part before and after recursive call.

    Code:
    #include <stdio.h>
    
    void puzzle(int n)
    {
      if (n != 0) {
        printf("n = %d\n",n);        /* want value of n passed to function */
        printf("%d % 2 = %d\n",n,(n%2));  /* want value of n%2 to print out as n % 2 = "value"   */
        puzzle(n/2);
    
        putchar('0' + n%2);         /* prints out the valid output of function */
        printf("\tn now = %d\n",(n%2));      /* prints out value of n after output of function   */
      }
    }
    
    main()
    {
       int i;
       printf("input value \n");
       scanf("%d",&i);
    
    
       puzzle(i);
       printf("\nValue after function call = %d",i);   /* again prints out value of i which should be the original scanned input by user */
       printf("\n\n");
       return 0;
    }
    Sue B.

    dazed and confused


  2. #2
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    try this
    char ch='%';

    printf("%d %c = %d", n, ch, (n%2));

  3. #3
    Unregistered
    Guest
    I was able to produce this code:

    #include <stdio.h>

    void puzzle(int n) {

    if(n) {
    printf("n = %d\n",n);
    printf("%d %% 2 = %d\n", n, n%2);
    /* To display "%" on the screen, use the escape seq %% */
    puzzle(n/2);


    /* What is up with the following lines??? */
    putchar('0' + n%2);
    printf("\nn now = %d\n", n%2 );
    /* Maybe with some explaination, I could help more, like what
    is this program supposed to do exactly?? */

    }
    }

    main()
    {
    int i;
    printf("input value: ");
    scanf("%d",&i);

    puzzle(i);
    printf("\nValue after function call = %d\n\n",i);

    return 0;
    }


    Maybe post something about what you are trying to accomplish

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Actually you would do this:
    printf("%d %% 2 = %d\n",n,(n%2));
    % is the escape for the % sign.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    23
    Basically the code prints out the binary equivalent to the number input by user. But I am failing to see how it prints a 1, exactly. I think when with the expression

    putchar('0' + n%2);

    the code prints out a 0 if n%2 = 0 (thus 0 + 0 = 0)
    or prints out a 1 if n%2 = 1 (thus 0 + 1 = 0)
    Is this right??

    What I don't get is what is printed out (what 'n' is before and after) this putchar statement and right before it recursively calls the function again.

    This recursive stuff gets me a little uneasy, and I know from past exams the instructor will test heavily on this. So I want to get a good idea of this simple problem.

    Thanks for any ideas, help, suggestions....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  2. Understanding Output Generated
    By Air in forum C Programming
    Replies: 10
    Last Post: 01-15-2009, 01:52 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM