Go Down

### Topic: An array to divide an integer into individual part (Read 923 times)previous topic - next topic

#### Interesting ##### Feb 22, 2009, 05:47 amLast Edit: Feb 22, 2009, 07:49 am by Interesting Reason: 1
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.
Code: [Select]
`   int count = 0;   int numdigits;   int giraffe;   int nanswer;   answer = nanswer      if (answer < 0)      nanswer = -answer;      while(nanswer > 1)      {      numdigits++;      nanswer/=10;      }       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...)   answer = answer - (outArray[count]*pow(10, (numdigits-1));   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?

#### anachrocomputer #1
##### Feb 22, 2009, 05:00 pm
Code: [Select]
`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:

Code: [Select]
`int outArray;`

will be fine.

#### Interesting #2
##### Feb 22, 2009, 05:18 pm
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.

#### MikMo #3
##### Feb 22, 2009, 10:59 pm
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.

#### AlphaBeta #4
##### Feb 22, 2009, 11:32 pmLast Edit: Feb 22, 2009, 11:32 pm by AlphaBeta Reason: 1
Quote
I was wondering how would I go about pausing the program and waiting for a user's input before continuing on with the program?

Code: [Select]
`while(conditionNotMet){  //check if condition is met}`

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

You could make your own 'terminator'

Something like this? :

Code: [Select]
`/*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}`

#### Interesting #5
##### Feb 23, 2009, 06:43 pm
Thanks for the replies.

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

Code: [Select]
`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...   answer = answer - (outArray[count]*pow(10, (numdigits-1)));   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.

Go Up