1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C programming question

  1. Aug 28, 2011 #1
    1. The problem statement, all variables and given/known data

    Simulate the following program

    #include <stdio.h>


    int xxx ( int n )
    {
    if ( n < 10 ) /* A */
    return 0;
    else
    return 1 + xxx ( n/10 ); /* B */
    }

    main()
    {
    printf ( "xxx(333)=%d\n", xxx(333) );
    }
    2. Relevant equations



    3. The attempt at a solution

    I'm not sure if this is correct but here's what I have

    xxx(333) = 1 + xxx(33)

    xxx(33) = 1 + xxx(3)

    xxx(3) = 0

    xxx(33) = 1 + 0 = 1

    xxx(333) = 1 + 1 = 2
     
  2. jcsd
  3. Aug 28, 2011 #2

    I like Serena

    User Avatar
    Homework Helper

  4. Aug 28, 2011 #3

    uart

    User Avatar
    Science Advisor

    Yes, that's correct. It's called recursion and you should use it with care. In some problems it is the only easy was to do it, but always use simple iteration over recursion where possible.
     
  5. Aug 28, 2011 #4
    Thanks! :smile:

    The next part is
    In general (in terms of n), if n > 0, what gets printed?

    Well n is 333, and 2 gets printed, so in general n0 + 1?
     
  6. Aug 28, 2011 #5

    I like Serena

    User Avatar
    Homework Helper

    Hmm, would that hold for xxx(33)?
    Or xxx(3)?
     
  7. Aug 28, 2011 #6
    Is it just 2 that's printed?
    Or 2,1,0? :confused:
     
  8. Aug 28, 2011 #7

    I like Serena

    User Avatar
    Homework Helper

    Rather than answering that, I prefer to counter that with:
    What do you think? And why?

    As for another question:
    What would happen if you called xxx(3) in your main program instead of xxx(333)?
     
  9. Aug 28, 2011 #8
    xxx(333) = 1 + xxx(33)

    xxx(33) = 1 + xxx(3)

    xxx(3) = 0


    Meaning that 1+(333/10=33>10 ---> 1+33/10<10 so is over ) = 2

    So the program just counts the figures to the one's place!


    I was just wondering about the return 0, and i immediately thought that it would mean the value returned by the function would be zero!

    But then it hit me that the return add stored the previous counted values!

    Some one could tell me why that specifically happens?
     
  10. Aug 28, 2011 #9
    Only 2 gets printed since
    printf ( "xxx(333)=%d\n", xxx(333) ); only wants the value for xxx(333) which is 2

    If xxx(3) was the main programme 0 would be printed
    If xxx(33) was the main then 1 would be printed
     
  11. Aug 28, 2011 #10

    I like Serena

    User Avatar
    Homework Helper

  12. Aug 28, 2011 #11
    I'm still not sure what gets printed in terms of n?
     
  13. Aug 28, 2011 #12

    I like Serena

    User Avatar
    Homework Helper

    I know.
    But now you know it's not the formula you suggested.

    All right, what will you get for xxx(3333)?
    And xxx(11111)?
     
  14. Aug 28, 2011 #13
    For xxx(3333) you get 3
    for xxx(11111) you get 4
     
  15. Aug 28, 2011 #14

    I like Serena

    User Avatar
    Homework Helper

    See a pattern?
     
  16. Aug 28, 2011 #15
    Yeah, every multiple of 10 that n is adds 1 to the output.
    So the number of times that n is divisable by 10 is what gets printed, but I don't know how to write that in terms of n
     
  17. Aug 28, 2011 #16

    I like Serena

    User Avatar
    Homework Helper

    Perhaps something with log10?

    Btw, for a programming problem I suspect that "the number of times that n is divisable by 10" is an acceptable answer.

    But considering that you are mathematically inclined, what about xxx(-111)? :wink:
     
  18. Aug 28, 2011 #17
    log10(n). Since we're dealing with integers in C the float will be cut off giving us the right answer. :smile:

    xxx(-111) wouldn't work for the log function, but with the programme, it's less than 10 no matter how many times you divide by 10 so it would just print 0?
     
  19. Aug 28, 2011 #18

    I like Serena

    User Avatar
    Homework Helper

    Careful. :wink:

    Let's start by avoiding the phrase "float", since in general that one is not applicable, and you do not want to use that one.
    Please use "double".

    And yes log10(n), which is a C function, will give the correct answer assuming n > 0, and assuming you convert the result to an integer before trying to print it.
    How would you print it?

    And if n < 0, no division by 10 occurs...
     
  20. Aug 28, 2011 #19
    Alrighty. :smile:

    Do you mean how would I write log(n) in C or something else?
     
  21. Aug 28, 2011 #20

    I like Serena

    User Avatar
    Homework Helper

    Yes, how would you print your result for an integer n, without using the xxx() function, in C?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C programming question
  1. C programming question (Replies: 7)

  2. C programming question (Replies: 1)

  3. C programming question (Replies: 2)

  4. C programming question (Replies: 2)

  5. C programming question (Replies: 4)

Loading...