#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    21
    Rep Power
    0

    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
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,111
    Rep Power
    1803
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    21
    Rep Power
    0
    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
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    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.
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,111
    Rep Power
    1803
    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.

IMN logo majestic logo threadwatch logo seochat tools logo