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

1. ## 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?

2. ## 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

3. 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

4. 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

5. 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

6. 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;
}```

7. 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;
}```

8. 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

9. 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;
}```

10. yeah, if a variable is incremented or decremented more then once on the same command its undefined.

11. So the updated code is OK, then?

12. 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. Just making sure. I'm at work right now, so I just slapped it together and didn't compile it.

14. 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. opps yeah thats right

Popular pages Recent additions