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.

3
  • 1
    Which are you using, C or C++? Commented Apr 13, 2013 at 12:18
  • 1
    " into two arrays of digits, so for example" -- There is only one array in your example. And did you have a question? Commented Apr 13, 2013 at 12:19
  • 1
    You need to learn about modulo division ("remainder") with %. You'd have had it coded faster than it took you to ask the question. Commented Apr 13, 2013 at 12:20

4 Answers 4

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) */
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

If you want actual characters, than a[i++] = n % 10 should be changed to a[i++] = '0' + (n % 10)
8

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');

1 Comment

Neat idea. Although this won't work for c. Not sure why the question is tagged for c as well as c++.
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))

2 Comments

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 Or you could just use vector<int> for everything and never again have to fret about if your array is the right length or not :)
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; 
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.