Code of putnbr, I can't display 0

newbeeC -  
ydurce Posted messages 81 Status Membre -
Hello,
I have a small algorithm problem; in class I need to recode the function putnbr. I'm almost there, except that it doesn't handle when the parameter is 0. Does anyone have an idea?

int my_put_nbr(int nb)
{
if (nb < 0)
{
my_putchar('-');
nb = -nb;
}
if (nb == 0)
return;
my_put_nbr(nb / 10);
my_putchar(nb % 10 + '0');
}

Configuration: Windows 7 / Firefox 3.6.11

1 réponse

ydurce Posted messages 81 Status Membre 18
 
Hello,

you can use a static initialized to 0 that you will increment on the last line of your function, and that you will test in the if (nb==0) for 0 or !=0 to know if a parameter 0 was passed at the beginning.
don’t forget to reset it to 0 after the test.

best regards
4