views:

749

answers:

11

I would like to convert an integer into an array, so that it looks like the following:

int number = 123456 ;
int array[7] ;

with the result:

array[0] = 1 
array[1] = 2
...
array[6] = 6
A: 

The easiest way I can imagine now is:

char array[40];
int number = 123456;

memset(array, 0x00, sizeof(array));

sprintf(array, "%d", number);

Additionally you can convert each digit to int just subtracting the char value by 0x30.

EDIT: If this is a homework, your teacher you probably ask you to write the program using % operator though (example 12 % 10 = 2). If this is the case, good homework ;-)

Andres
that's a char array, not an int array
catchmeifyoutry
If you're going to use sprintf, please at least use snprintf
Phil Nash
I know its a char array, but you can easily convert to int subtracting the char value by 0x30, as I observed in the answer. snprintf is not C standard.
Andres
If this is the case, of course, your is much better!
Andres
@Andres - snprintf is std C99, will be included in C++/1x and is available now on most C++ compilers as a common extension
Phil Nash
Great, good to know @Phil. I said that because I tried to use it in some projects in Unix, but the compiler we were using didn't had the support.
Andres
+2  A: 

You can extract the last digit of the number this way:

int digit = number % 10;
number /= 10;

Note that you should also check whether number is positive. Other values require additional handling.

avakar
A: 

You can use modulus to determine the last digit.

And you can use division to move another digit to the last digit's place.

Shmoopty
A: 

just use modular arithmetic:

int array[6];
int number = 123456;
for (int i = 5; i >= 0; i--) {
    array[i] = number % 10;
    number /= 10;
}
how about an answer which actually explains the approach so the OP *learns* something?
jalf
Also this is hard-coded to deal with 6 digit numbers
Phil Nash
That will only work properly on 6 digit non-negative integers.
Shmoopty
I wanted the OP to think about how to extend it to handle other numbers, without just giving the general case.
+6  A: 

Perhaps a better solution is to work backwards:

123456 % 10 = 6

123456 / 10 = 12345

12345 % 10 = 5

12345 / 10 = 1234

Broam
+1  A: 

Take the log10 of the number to get the number of digits. Put that in, say pos, then, in a loop, take the modulo of 10 (n % 10), put the result in the array at position pos. Decrement pos and divide the number by 10. Repeat until pos == 0

What did you want to do with the sign if it's negative?

Phil Nash
A: 

You can't simply "convert" it. The integer is not represented in software in decimal notation. So the individual digits you want don't exist. They have to be computed.

So, given an arbitrary number, how can you determine the number of ones?

We could divide by ten, and then take the remainder: For 123, the division would give 12, and then there's a remainder of 3. So we have 3 ones. The 12 tells us what we have past the ones, so it can be our input for the next iteration. We take that, divide by 10, and get 1, and a remainder of 2. So we have 2 in the tens place, and 1 left to work with for the hundreds. Divide that by 10, which gives us zero, and a remainder of 1. So we get 1 in the hundreds place, 2 in the tens place, and 3 in the ones place. And we're done, as the last division returned zero.

jalf
A: 

thank you all for your help. first of all: this is not my homework, it's part of a braintester i'm doing just for fun. the methos, proposed by ehuhtala works good with my programm! thank you all for your help :)

kantor
A: 

See SO question Language showdown: Convert string of digits to array of integers? for a C/C++ version (as well as other languages).

Peter Olsson
A: 

if this is really homework then show it your teacher - just for fun ;-)

CAUTION! very poor performance, clumsy way to reach the effect you expect and generally don't do this at home(work) ;-)

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

typedef std::vector< int > ints_t;

struct digit2int
{
    int operator()( const char chr ) const
    {
        const int result = chr - '0';
        return result;
    }
};

void foo( const int number, ints_t* result )
{
    std::ostringstream os;
    os << number;
    const std::string& numberStr = os.str();
    std::transform(
        numberStr.begin(),
        numberStr.end(),
        std::back_inserter( *result ),
        digit2int() );
}

int main()
{
    ints_t array;
    foo( 123456, &array );
    std::copy(
        array.begin(),
        array.end(),
        std::ostream_iterator< int >( std::cout, "\n" ) );
}
Dominic.wig
A: 

but what if i need a 30 digit long !!! i cant put it in a variable!

adir
ask a new question.
N 1.1