9

I need to convert two integers into two arrays of digits, so for example 544 would become arr[0] = 5, arr[1] = 4, arr[2] = 4.

I have found some algorithms doing this, but they create new array, and return this. I would have to allocate this memory for two arrays, so I wanna pass two integers by reference and do this on them directly.

I guess I can do this, because these integers are in fact template types, so they should be changeable. That's why I added C++ tag here.

user2252786
  • 1,627
  • 5
  • 21
  • 32

4 Answers4

10

Just using something like this:

int n = 544; // your number (this value will Change so you might want a copy)
int i = 0; // the array index
char a[256]; // the array

while (n) { // loop till there's nothing left
    a[i++] = n % 10; // assign the last digit
    n /= 10; // "right shift" the number
}

Note that this will result in returning the numbers in reverse order. This can easily be changed by modifying the initial value of i as well as the increment/decrement based on how you'd like to determine to length of the value.


(Brett Hale) I hope the poster doesn't mind, but I thought I'd add a code snippet I use for this case, since it's not easy to correctly determine the number of decimal digits prior to conversion:

{
    char *df = a, *dr = a + i - 1;
    int j = i >> 1;

    while (j--)
    {
        char di = *df, dj = *dr;
        *df++ = dj, *dr-- = di; /* (exchange) */
    }
}
Brett Hale
  • 21,653
  • 2
  • 61
  • 90
Mario
  • 35,726
  • 5
  • 62
  • 78
9

A simple solution is:

int i = 12312278;

std::vector<int> digits;

while (i)
{
    digits.push_back(i % 10);

    i /= 10;
}

std::reverse(digits.begin(), digits.end());

or, string based ( i >= 0 )

for (auto x : to_string(i))
    digits.push_back(x-'0');
masoud
  • 55,379
  • 16
  • 141
  • 208
  • Neat idea. Although this won't work for `c`. Not sure why the question is tagged for `c` as well as `c++`. – Mario Apr 13 '13 at 12:21
1

Call the integer a.

To get the units digit of a, a % 10

To shift a down so the tens is the units digit, a / 10

To know when you're done, a == 0

To know how large your array needs to be in the first place, min(ceil(log(a+1, 10)), 1) (to convince yourself this works, try the logarithm part of it in a calculator. if you don't have multiple argument log, use the identity log(x,y) == log(x)/log(y))

Patashu
  • 21,443
  • 3
  • 45
  • 53
  • Often it's simpler to just repeatedly divide by 10 to pre-determine out the length of the result. Probably not much less efficient than using log, and easier to understand/validate. – Hot Licks Apr 13 '13 at 12:24
  • @Hot Licks Or you could just use `vector` for everything and never again have to fret about if your array is the right length or not :) – Patashu Apr 13 '13 at 12:24
0

You can do something like this if you are using C++:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() 
{

    int a=544;
    stringstream str;
    str << a;
    string arr;
    str>>arr;
    for(int i=0; i<arr.length(); i++)
    {
        cout << arr[i];
    }
    system("pause");
    return 0; 
}
Johnny Mnemonic
  • 3,822
  • 5
  • 21
  • 33