Thread: Digit selector... (Mathmatically complex, for some)

  1. #1
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42

    Digit selector... (Mathmatically complex, for some)

    Ok my question is, how on earth do you select an individual digit from a float and most importantly an int?

    Like for VERY obvious reasons this won't work:
    Code:
    int i;
    i=32;
    
    printf("Your number was: %d%d",i[0],i[1]);
    Please note, I KNOW this WILL NOT work.

    but is there a mathmatical or hopefully a function that will let you dynamically select a digit, IE let you find the digit in the 10's, 100's or what not?
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Lightbulb Not a challange

    First of all it is not a challange.

    Use a mod function in a while loop:
    Code:
    char str[15];
    int i,j,k=0;
    i=32;                   // example
    while ( i > 0 )
    {
         j = i % 10;
         i = i / 10;
         str[k++] = j + '0';
    }
    str[k++] = i;
    str[k] = '\0';
    Or use the itoa function

    Or, simply use....(this method can be used for floats, ints or just about anything...But beware of the number of digits you allote for the string buffer)
    Code:
    char str[15];
    int i=35,j;
    sprintf(str,"%d",i);
    for(j=0;j<strlen(str); j++)
        printf("%c",str[j]);

    -Harsha
    Last edited by vsriharsha; 08-28-2004 at 02:40 AM. Reason: New Idea
    Help everyone you can

  3. #3
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42
    Hold up, hold up, sorry, I should have made myself clearer, I understand that i've been overlooking the % operator, however I want the results in an int or float, not a char, I was just using that as a fake example such as in char a="abcd"; a[0] would refer to 'a'. I was making an inquisition as to whether or not there was a similar way to access a digit from an int, sorry for not making that clear
    Last edited by Axpen; 08-28-2004 at 02:52 AM. Reason: Poor readability punctuation error
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

  4. #4
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Hmmm,
    Like this??
    Code:
    int i = 527;
    int j, k, l;
    <somecode>
    printf("J = %d\t K = %d\t L = %d\n",j,k,l);
    will give you...
    5 2 7
    Is this what you're looking for? If yes, then mod operator is one solution....

    Cheers,
    Harsha
    Help everyone you can

  5. #5
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42
    Yes, that's precisely what I needed, sucks to be me, I should've remembered about the mod operator, however I rarely used it before, thanks, that answers my question, see before I was trying code like:
    Code:
    int i;
    i=4321;
    
    printf("Thousand: %d\nHundred: %d...",(i/1000),((i-((i/1000)*1000))/100));
    That may not be mathmatically correct, but I can't find the code I used, it gets out of hand with a lot of digits, anyways, thanks for the help
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    This seems to work for me:
    Code:
    #include <stdio.h>
    
    int place(int num, int p)
    {
      int n = 1;
    
      while(p-- > 0)
        n *= 10;
    
      return (num%n)/(n/10);
    }
    
    int main(void)
    {
      int num = 1234;
    
      printf("%d %d %d %d\n",
        place(num, 4), place(num, 3), place(num, 2), place(num, 1));
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    #include <stdio.h>
    
    int main( void )
    {
    
      char str[ BUFSIZ ];
      float n = -1234.5678;
      int j, k = 0;
      int arr[ BUFSIZ ];
      sprintf( str, "%f", n );
      for( j = 0; j < strlen( str ); j++ )
        arr[ k++ ] = ( str[ j ] == '.' || str[ j ] == '-' ) ? k-- : str[ j ] - '0';
    
      for( j = 0; j < k; j++ )
        printf( "%d", arr[ j ] );
    
      printf( "\n%f\n", n );
    
      return 0;
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    XSquared:

    The behavior of this is undefined

    Code:
        arr[ k++ ] = ( str[ j ] == '.' || str[ j ] == '-' ) ? k-- : str[ j ] - '0';
    With
    Code:
      float n = 0.5;
    Your program, compiled with Borland bcc32 Version 5.5.1 or Microsoft Visual C++ 6.0, gives the following output:

    1500000
    0.500000
    Even if some compilers give the result that you expected, the behavior is still undefined.

    This does spoil the effect of your example (which shows that, since 1234.5678 is not exactly representable by a float, it is not reasonable to expect to extract the original input digits.)

    Regards,

    Dave
    Last edited by Dave Evans; 08-28-2004 at 01:05 PM.

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I'm not sure what is undefined about it. Is it because I'm using the k++ and k-- in the same line of code?

    Code:
    #include <stdio.h>
    
    int main( void )
    {
    
      char str[ BUFSIZ ];
      float n = -1234.5678;
      int j, k = 0;
      int arr[ BUFSIZ ];
      sprintf( str, "%f", n );
      for( j = 0; j < strlen( str ); j++ )
      {
    
        if( str[ j ] != '.' && str[ j ] != '-' )
        {
    
          arr[ k ] = str[ j ] - '0';
          k++;
    
        }
    
      }
    
      for( j = 0; j < k; j++ )
        printf( "%d", arr[ j ] );
    
      printf( "\n%f\n", n );
    
      return 0;
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    yeah, if a variable is incremented or decremented more then once on the same command its undefined.

  11. #11
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    So the updated code is OK, then?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Appears to be, unless of course you run the code on a system that doesn't have the characters 0 - 9 in a sequentically

  13. #13
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Just making sure. I'm at work right now, so I just slapped it together and didn't compile it.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  14. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Thantos
    yeah, if a variable is incremented or decremented more then once on the same command its undefined.
    There's a little more to it than that.

    See, for example

    3.1: Why doesn't this code:

    a[i] = i++;

    work?
    at http://www.faqs.org/faqs/C-faq/faq/

    Dave

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    opps yeah thats right

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting 'undelcared identifier' ???
    By Bill83 in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2006, 01:00 PM
  2. arithmetic operator friend functions
    By linucksrox in forum C++ Programming
    Replies: 7
    Last Post: 02-06-2006, 11:39 PM
  3. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 05:31 PM
  4. Problem from texbook
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2002, 04:55 AM
  5. Roman number converter
    By BalanusBob in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2002, 06:29 AM