Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

vbisme

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

This topic is 6436 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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



Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

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

Share this post


Link to post
Share on other sites
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 10

char 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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
digits= 0;
for( iTempnum= num; iTempnum != 0; iTempnum /= 10 ) numArr[ digits++ ]= iTempnum%10;

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 number

int numDigitsInNumber = (int)floor(log10(abs(n)))+1;



Dormeur

Edited by - Dormeur on November 2, 2000 8:47:14 AM

Share this post


Link to post
Share on other sites
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 number

int 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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!