October 1st, 2013, 12:14 PM

Display the number from parameter help please
i need a little help please i have to write a function which will display any number put it in parameter.For now i have this:
Code:
void my_putchar(char c)
{
write(1, &c, 1);
}
int my_put_nbr(int nb)
{
if (nb == '0')
{
my_putchar('0');
}
if ( nb > 0)
{
my_putchar((nb / 10) + '0' );
my_putchar((nb % 10) + '0');
}
if (nb < 0)
{
my_putchar('');
my_putchar((nb * 1 / 10) + '0');
my_putchar((nb * 1 % 10) + '0');
}
}
int main()
{
my_put_nbr(42);
}
This works for numbers 99 to 99 but how can i do if the number is 849 or 1254 or any one who is un int??
Thanks in advance
October 1st, 2013, 01:21 PM

There are a number of ways to tackle this. It is easiest to start from the least significant digit, but then you have the issue of outputting the number backward. That can be resolved in at least two ways:
 Build the characters in a buffer array, then have a loop that outputs the array content backward.
 Use recursion.
If you start from the most significant digit, you need to work out beforehand how many digits there are. That can be determined somewhat expensively by:
Code:
int digits = (int)log10( nb ) + 1 ;
The recursive method is by far the most efficient, but in some cases recursion is undesirable (systems with very limited stack size for example  applies mostly to embedded systems in this case).
C Code:
void printDecimalInteger( int n )
{
if( n < 0 )
{
putchar( '' ) ;
n = n ;
}
if( n > 10 )
{
printDecimalInteger( n / 10 ) ;
}
putchar( n % 10 + '0' ) ;
}
Last edited by clifford; October 1st, 2013 at 01:41 PM.
October 2nd, 2013, 02:26 AM

i have try but it doesn't work
Code:
void my_putchar(char c)
{
write(1, &c, 1);
}
int my_put_nbr( int nb )
{
if( nb < 0 )
{
my_putchar( '' ) ;
nb = nb ;
}
if( (nb > 10 )
{
my_putchar(nb / 10) ;
}
my_putchar( nb % 10 + '0' ) ;
}
int main()
{
my_put_nbr(424);
}
If is not this what you want to say i'm sorry but im a a noob in coding
October 2nd, 2013, 09:12 AM

Use an array to store the digits. If you just output the digits as you create them, then you will output the number backwards. In order to display them in the correct order, store them temporarily in an array and then output them in the reversed order that they appear in the array. You're not doing that so of course it's not working.
You are going to repeatedly divide the number by 10 until it's zero; eg, 1234 becomes 123 which becomes 12 which becomes 1 while becomes 0 and you're done. You are not doing that, so of course it's not working.
When you divide the number by 10, then you will have lost the digit that you want, so you need to get that digit before you divide the number. You are not doing that, so of course it's not working.
Think about what you need to do and step through the process with pencil and paper. That step has nothing to do with coding, so noobishness would not be an issue.
October 2nd, 2013, 01:05 PM

Originally Posted by sparky90ful
i have try but it doesn't work
My code works  your's does not  your code is not the same as mine!
Where you have:
Code:
if( (nb > 10 )
{
my_putchar(nb / 10) ;
}
my_putchar( nb % 10 + '0' ) ;
you should have
Code:
if( nb > 10 )
{
my_putchar( nb % 10 + '0' ) ;
}
my_putchar(nb / 10) ;
You have inexplicably swapped the output statement with the recursion as well as introducing a stray parenthesis. A straight copy & paste would have solved your problem, replacing putchar() for my_putchar().
Last edited by clifford; October 2nd, 2013 at 01:07 PM.