# Homework Help: C programming question

1. Aug 28, 2011

### Maybe_Memorie

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. Aug 28, 2011

### I like Serena

Perfect!

3. Aug 28, 2011

### uart

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.

4. Aug 28, 2011

### Maybe_Memorie

Thanks!

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?

5. Aug 28, 2011

### I like Serena

Hmm, would that hold for xxx(33)?
Or xxx(3)?

6. Aug 28, 2011

### Maybe_Memorie

Is it just 2 that's printed?
Or 2,1,0?

7. Aug 28, 2011

### I like Serena

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)?

8. Aug 28, 2011

### kalakoi

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?

9. Aug 28, 2011

### Maybe_Memorie

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

10. Aug 28, 2011

### I like Serena

Yep!

11. Aug 28, 2011

### Maybe_Memorie

I'm still not sure what gets printed in terms of n?

12. Aug 28, 2011

### I like Serena

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)?

13. Aug 28, 2011

### Maybe_Memorie

For xxx(3333) you get 3
for xxx(11111) you get 4

14. Aug 28, 2011

### I like Serena

See a pattern?

15. Aug 28, 2011

### Maybe_Memorie

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

16. Aug 28, 2011

### I like Serena

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)?

17. Aug 28, 2011

### Maybe_Memorie

log10(n). Since we're dealing with integers in C the float will be cut off giving us the right answer.

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?

18. Aug 28, 2011

### I like Serena

Careful.

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.

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

19. Aug 28, 2011

### Maybe_Memorie

Alrighty.

Do you mean how would I write log(n) in C or something else?

20. Aug 28, 2011

### I like Serena

Yes, how would you print your result for an integer n, without using the xxx() function, in C?