C Programming: Simulate xxx(333) = 2

Click For Summary

Discussion Overview

The discussion revolves around simulating a recursive C program that counts the number of digits in a given integer. Participants explore the behavior of the function, its output for different inputs, and the implications of recursion versus iteration.

Discussion Character

  • Homework-related
  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants confirm the correctness of the initial solution for xxx(333) yielding 2.
  • There is a discussion about what gets printed in general terms for n, with some suggesting a formula while others question its validity.
  • Participants explore the output for different values of n, such as xxx(33) and xxx(3), leading to further inquiries about the function's behavior.
  • One participant proposes that the function counts the number of digits by dividing n by 10 repeatedly.
  • Another participant suggests that the output relates to the number of times n is divisible by 10, but expresses uncertainty about how to articulate this in terms of n.
  • There is a mention of using log10 to express the relationship mathematically, although the applicability of this for negative numbers is questioned.
  • Participants discuss the implications of using logarithms in C and the differences between array declarations in C programming.

Areas of Agreement / Disagreement

Participants generally agree on the output of the function for specific inputs, but there are multiple competing views regarding the generalization of the output in terms of n. The discussion remains unresolved on how to express the relationship mathematically for all cases.

Contextual Notes

Some participants express uncertainty about the behavior of the function for negative inputs and the implications of using logarithmic functions in C. There are also discussions about the differences in memory allocation for arrays in C, which may not be fully resolved.

Who May Find This Useful

This discussion may be useful for students learning about recursion in programming, those interested in the behavior of functions in C, and individuals exploring the mathematical implications of programming concepts.

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
Perfect! :smile:
 
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?
 
Hmm, would that hold for xxx(33)?
Or xxx(3)?
 
Is it just 2 that's printed?
Or 2,1,0? :confused:
 
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)?
 
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
 
  • #10
Yep! :smile:
 
  • #11
I'm still not sure what gets printed in terms of n?
 
  • #12
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
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
 
  • #14
See a pattern?
 
  • #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
 
  • #16
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:
 
  • #17
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?
 
  • #18
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...
 
  • #19
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?
 
  • #20
Yes, how would you print your result for an integer n, without using the xxx() function, in C?
 
  • #21
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?
 
  • #22
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.
 
  • #23
Okay, thank you! :smile:
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
9
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K