C Programming: Simulate xxx(333) = 2

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
22 replies · 4K views
Maybe_Memorie
Messages
346
Reaction score
0

Homework Statement



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) );
}

Homework Equations


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
 
Physics news on Phys.org
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.
 
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?
 
Is it just 2 that's printed?
Or 2,1,0? :confused:
 
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?
 
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
 
I'm still not sure what gets printed in terms of n?
 
I like Serena said:
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)?

For xxx(3333) you get 3
for xxx(11111) you get 4
 
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
 
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:
 
I like Serena said:
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:

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?
 
Maybe_Memorie said:
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?

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...
 
I like Serena said:
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...

Alrighty. :smile:

Do you mean how would I write log(n) in C or something else?
 
To be honest, I have no idea. Logarithms aren't on our programming syllabus, so it probably doesn't matter?

Actually I better ask this since my exam is tomorrow and making a new thread is pointless since I only have one question to ask...

According to the rules of C, both arrays a and b, declared below, are treated as
arrays of 3 pointers. What is the difference between the two declarations?
int a[3][4]; int * b[3];

b is a memory address declared to be the address of an integer variable and a is a 3x4 matrix?
 
Hmm, b is an array of 3 pointers.
As yet those pointers don't point anywhere, and using them would cause an access violation.

And yes, a is a 3x4 matrix, which can be used immediately.

Note that if you allocate space for the 3 pointers in b, b behaves like a 3xn matrix as well, although behind the screens the memory is allocated differently from a.