C Programming: Simulate xxx(333) = 2

In summary: What is the difference between the two?The arrays a and b are of the same size, but a has a greater number of elements because it is an array of pointers.
  • #1
Maybe_Memorie
353
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
  • #2
Perfect! :smile:
 
  • #3
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
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?
 
  • #5
Hmm, would that hold for xxx(33)?
Or xxx(3)?
 
  • #6
Is it just 2 that's printed?
Or 2,1,0? :confused:
 
  • #7
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
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
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:
 

FAQ: C Programming: Simulate xxx(333) = 2

How do I simulate xxx(333) = 2 in C programming?

To simulate xxx(333) = 2 in C programming, you will need to use the modulus operator (%) to extract each digit from the number 333. Then, you can use if statements to check if the extracted digits match the desired pattern (in this case, xxx) and add them together. Finally, you can compare the result to 2 and print the appropriate message.

Can I use a loop to simulate xxx(333) = 2 in C programming?

Yes, you can use a loop to simulate xxx(333) = 2 in C programming. You can use a for loop to iterate through the digits of 333 and use the modulus operator (%) to extract each digit. Then, you can use the extracted digits to check for the desired pattern and add them together. Finally, you can compare the result to 2 and print the appropriate message.

How can I handle invalid input when simulating xxx(333) = 2 in C programming?

You can handle invalid input by using a combination of if statements and error handling techniques such as try-catch blocks. You can check if the input is a valid number and within the range of acceptable values before performing the simulation. If the input is invalid, you can print an error message and prompt the user to enter a valid input.

Is there a simpler way to simulate xxx(333) = 2 in C programming?

There are different ways to simulate xxx(333) = 2 in C programming, but the simplest way would be to use the pre-defined functions in the math library. You can use the pow() function to raise a number to a power and the floor() function to round down to the nearest integer. By combining these functions, you can easily simulate xxx(333) = 2 without the need for loops or if statements.

Can I use variables to store the result of simulating xxx(333) = 2 in C programming?

Yes, you can use variables to store the result of simulating xxx(333) = 2 in C programming. You can declare a variable of the appropriate data type and assign the result of the simulation to it. This allows you to use the result in other parts of your program or perform further calculations on it.

Similar threads

Replies
12
Views
2K
Replies
7
Views
3K
Replies
4
Views
1K
Replies
12
Views
1K
Replies
3
Views
1K
Replies
3
Views
925
Back
Top