Advertisement

int 123 to array[3] = {1, 2, 3}

Started by November 02, 2000 02:45 AM
15 comments, last by vbisme 24 years, 2 months ago
let''s say:
    
int myInt;
int myArray[5];

myInt = 63251;
    
how do I take each digit in myInt and place it in each element of myArray so that myArray[0] = 1(which is the unit), myArray[1] = 5(the ten) ...and so on... one method I could think of is test how big myInt is, if > 10000, myArray[5] = myInt / 10000; this gives 6 so myArray[5] would be 6, then int temp; temp = myInt - 10000 the repeat the dividing. This is cumbersome however, could anybody tell me a better way?
Hi,

Have''nt really thought this out, but you can convert the int into a character string by :
int num = 1234;
char numStr[5];

itoa(num,numStr,10);

then you have each element in the character string :
numStr[1] - 10s,
numStr[2] - 100s,
numStr[3] - 1000s,
etc...

if you need integers, just convert back using atoi(...)

Boy, was this an ugly solution?

/ Tooon



Advertisement
Sorry, for providing you width such a lousy solution above,
just slept for 2h this night

Better way :

int num = 61234;
int numArr[4];

numArr[4] = num/10000; // result = 6
numArr[3] = (num%10000)/1000; // result = 1
numArr[2] = (num%1000)/100; // result = 2
numArr[1] = (num%100) /10; // result = 3
numArr[0] = (num%10); // result = 4

/ Tooon


i would do it something like this...

int myInt;
int myArray[5];
int i;

myInt = 63251;
for(i = 0; i < 5; i++)
{
myArray = (myInt / pow(10, i)) % 10;
}

not "super-effecient" but it does what it''s suppose to and i think its better than using atoi & itoa...

Nice!

One more question, how do you test for how many digit any give number has?
Hmm... when i needed a solution to that problem i used (rather ugly, a last resort):

if (num < 10) digits = 1;
else if (num < 100) digits = 2;
else if (num < 1000) digits = 3;

etc.

EDIT: i've just thought of a better one:

        #include <stdio.h>#include <math.h>#define MAX_NUM_OF_DIGITS    10char buffer[10];int digits, itemp, original_number;printf ("\nEnter a number: ");scanf("%d", &original_number);for (int i=1; i<MAX_NUM_OF_DIGITS; i++) {	itemp = pow(10, i);	if (itemp > original_number) {		digits = i;		break;	}}printf("\n%d\n", digits);    


Edited by - jumble on November 2, 2000 5:23:03 AM
jumble-----------
Advertisement
digits= 0;
for( iTempnum= num; iTempnum != 0; iTempnum /= 10 ) numArr[ digits++ ]= iTempnum%10;
Here''s my solution that can handle any number. Have you skipped your math lessons when you can''t use logarithm?

    void Int2Array(unsigned long value, unsigned char* array){  unsigned long  digits;  digits = log(value) + 1;  array = new unsigned char[digits];  for(unsigned long index = 0 ; index < digits; index++)  {    array[index] = value % 10;    value /= 10;  }}    


-Jussi

"Paina pääsi rauhaansa,
lepää hetki lennostasi,
anna maailman mennä tietään,
aina suurta yötä päin"

- CMX
Hey Selkrank, I dare you to try that out with a negative number . And you should use log10 (log with base number 10). And remember: log10(n) = log(n)/log(10).

Why not use this:

        #include <math.h>int n = 64253;    // Or another numberint numDigitsInNumber = (int)floor(log10(abs(n)))+1;    


Dormeur

Edited by - Dormeur on November 2, 2000 8:47:14 AM
Wout "Dormeur" NeirynckThe Delta Quadrant Development Page
quote: Original post by Dormeur

Hey Selkrank, I dare you to try that out with a negative number . And you should use log10 (log with base number 10). And remember: log10(n) = log(n)/log(10).


As you can see, I declared value an unsigned long, so it cannot be negative. And if I remember right, log() function is the 10-based logarithm, not e-based.

quote: Why not use this:

                #include <math.h>int n = 64253;    // Or another numberint numDigitsInNumber = (int)floor(log10(abs(n)))+1;    


What does floor() do?

-Jussi

"My spine hurts"

Edited by - Selkrank on November 2, 2000 8:51:35 AM

This topic is closed to new replies.

Advertisement