 # An array to divide an integer into individual part

Would something like this work for breaking up a number into individual integers?

I want to take something like 5620 and turn it into an array with  as the entries. But i would also like this to be an unconstrained (no set number of entries) becaues the numbers I’m using will change. Would something like this work?

I just cut this function out of my code, but i’ll be inserting the value answer into the function.

``````   int count = 0;
int numdigits;
int giraffe;
{
numdigits++;
}
int outArray[numdigits];
int numsaver = numdigits;

while(numdigits > 0) //this function writes the individual numbers to the array outArray
{
outArray[count] = (answer/(pow(10,(numdigits-1)); //takes the answer, divides by 10^(number of digits - 1) (which gives the num digits, but I don't know how to multiply something by not 10...)
numdigits -=;
count ++;
}
``````

Also, I was wondering how would I go about pausing the program and waiting for a user’s input before continuing on with the program?

``````int outArray[numdigits];
``````

This is not legal in C. Array dimensions must be integer constants, not variables. As for C++, I'm not sure -- did this code compile (verify) OK?

Anyway, you can work out the maximum possible number of digits because of the limited range of an integer variable. You'll waste a few bytes, but something like:

``````int outArray;
``````

will be fine.

Yes it compiled okay. Is there any value to write to the array to make it stop reading? I know it's possible with strings (\0), but is there a way to have it stop at the 5th entry if necessary? I don't want to have it always output the entire array of digits.

you can use the itoa function which turns an integer into it's string representation, and then pick the individual chars. from the string.

The downside of this method is that itoa will add abaut 600 bytes to the size of your sketch.

I was wondering how would I go about pausing the program and waiting for a user’s input before continuing on with the program?

``````while(conditionNotMet)
{
//check if condition is met
}
``````

Is there any value to write to the array to make it stop reading?

You could make your own ‘terminator’

Something like this? :

``````/*not tested*/
byte END = 255;
byte array[] = { 1 , 2 , 3 , 4 , 5 , 6 };

array = END;

//will cycle through i e[0,3]
for ( byte i=0; i<6 , array[i] != END , i++)
{
//execute code
}
``````

Thanks for the replies.

So it is indeed possible to end an array with something like this

``````while(numdigits > 0) //this function writes the individual numbers to the array outArray
{
outArray[counta] = (answer/(pow(10,(numdigits-1)))); //takes the answer, divides by 10^(number of digits - 1) which gives the num digits, but I don't know how to multiply something by not 10...
numdigits = numdigits - 1;
count ++;
}
if(numdigits == 0)
{
byte END = 255
outArray[count] = END

}
``````

I set that array to 16 earlier, so that's the largest it could possibly get, I can write to the array like that correct?

Thanks a lot for the replies again.

I'll check that itoa out later, sounds cool.