0

So lets say you have an integer in your code declared

int my_num = 967892; 

and you have an array

int a[6]; 

How would you put that integer into the array so it looks like this?

{ 9, 6, 7, 8, 9, 2 }
slim
  • 40,215
  • 13
  • 94
  • 127
Anonymous
  • 57
  • 1
  • 9
  • 1
    Is this the *only* integer you need, or do you need a dynamic way of doing this? – Bathsheba Nov 04 '15 at 13:32
  • What did you try so far? – alk Nov 04 '15 at 13:33
  • You can use modulo and division to get each multiple of 10. – 001 Nov 04 '15 at 13:33
  • @JohnnyMopp: that is indeed exactly how you do it, but you do get the least significant digit first using that method, and getting the size of the array is interesting too. – Bathsheba Nov 04 '15 at 13:35
  • It's almost answerable now. Two more things: 1) is the number allowed to be negative, 2) what happens if int[6] is insufficient, or too big to hold the data. – Bathsheba Nov 04 '15 at 13:39
  • @Bathsheba I am only allowed to use positive numbers. And the fact that the array is either insufficient or too big to hold the data is not taken into account in this scenario. – Anonymous Nov 04 '15 at 13:49
  • Then use an `unsigned` for `my_num`: this will greatly simplify the implementation. – Bathsheba Nov 04 '15 at 13:52

5 Answers5

2

Perhaps like this:

const unsigned char digits[] = { 9, 6, 7, 8, 9, 2 };

but there are many things that are unclear with your question, of course.

If you want to do this at runtime, as your comment now makes me believe, you need more code of course. Also, it will be tricky to make the array "fit" the number exactly, since that requires runtime-sizing of the array.

The core operation is % 10, which when applied to a number results in the rightmost digit (the "ones" digit).

unwind
  • 391,730
  • 64
  • 469
  • 606
  • So lets say you have an integer in your main declared `int my_num = 967892;` and you have an array `int a[6];`. How would you put that integer into the array so it looks like `{ 9, 6, 7, 8, 9, 2 }`? – Anonymous Nov 04 '15 at 13:34
  • That comment is better than the question text. Could you move it there too? – Bathsheba Nov 04 '15 at 13:35
  • So would something like this work `for (int i = 0; i < size; ++i, my_num /= 10){ a[(size - 1) - i] = my_num % 10;}` – Anonymous Nov 04 '15 at 14:11
2

You can do this by getting each digit and putting it into an array. Kudos to @unwind for thinking of using unsigned int because digits don't have signs. I didn't think of that.

DISClAIMER: this code is untested but would, theoretically, if I haven't made any mistakes that the community will catch, accomplish your task.

NOTE: This program is implementation-defined when theNum is negative. See this SO question for more info on what that means. Also, the accepted answer in the question of which this post is a duplicate has shorter code than this but uses log10 which (according to commenters) could be inaccurate.

//given theNum as the number
int tmp = theNum;
int magnitude = 0;
//if you keep dividing by 10, you will eventually reach 0 (integer division)
//and that will be the magnitude of the number + 1 (x * 10^n-1)
for (; tmp > 0; magnitude++){ //you could use a while loop but this is more compact
    tmp /= 10;
}
//the number of digits is equal to the magnitude + 1 and they have no sign
unsigned int digits[magnitude];
//go backwards from the magnitude to 0 taking digits as you go
for (int i = magnitude - 1; i > 0; i--){
    //get the last digit (because modular arithmetic gives the remainder)
    int digit = theNum % 10;
    digits[i] = digit; //record digit
    theNum /= 10; //remove last digit
}
Community
  • 1
  • 1
Arc676
  • 4,445
  • 3
  • 28
  • 44
  • 1
    One can also get the magnitude by doing `log10f(tmp) + 1`. – alk Nov 04 '15 at 13:42
  • I wasn't aware of all the functions in the C math library as I don't use it often. @Bathsheba Could you provide a link explaining the VLA extension? – Arc676 Nov 04 '15 at 13:43
  • @alk: I'm not certain that's completely reliable due to floating point. – Bathsheba Nov 04 '15 at 13:44
  • Oops. VLAs have been standard C for nearly 20 years! My bad. – Bathsheba Nov 04 '15 at 13:45
  • Note that this is implementation defined for negative `theNum` – Bathsheba Nov 04 '15 at 13:45
  • Pity. I think using `log10f` to calculate the number of digits is not always guaranteed to work. Why not test it for every `unsigned`? I think you might be surprised by the result. Your old method was better. – Bathsheba Nov 04 '15 at 13:46
  • @Bathsheba Alright, I'll roll back. Could you still provide some documentation for the VLA extension? I would really appreciate that. Thanks. – Arc676 Nov 04 '15 at 13:47
  • It's variable length arrays, and they have been standard C since 1999. Oops. – Bathsheba Nov 04 '15 at 13:47
  • @Bathsheba: There also of `log10l()` doing `long double`, but you are right this is another question. – alk Nov 04 '15 at 13:47
  • I think this answer is perfect now. (Apart from no negative check). Plus one. – Bathsheba Nov 04 '15 at 13:47
0

If this shall be done dynamically:

  1. Determine the number of digits as N.
  2. Allocate an array large enough (>=N) to hold the digits.
  3. Loop N times chopping of the digits and storing them in the array allocated under 2.
alk
  • 69,737
  • 10
  • 105
  • 255
0

I assume you want to achieve something like this:

int arr[SOME_SIZE];
int len = int_to_array(arr,421);
assert(len == 3);
assert(arr[0] == 4);
assert(arr[1] == 2);
assert(arr[1] == 1);

Since this is probably a homework problem, I won't give the full answer, but you'll want a loop, and you'll want a way to get the last decimal digit from an int, and an int with the last digit removed.

So here's a hint:

421 / 10 == 42
421 % 10 == 1

If you want to create an array of the right length, there are various approaches:

  • you could loop through twice; once to count digits (then create the array); once again to populate
  • you could populate an array that's bigger than you need, then create a new array and copy in as many members as necessary
  • you could populate an array that's bigger than you need, then use realloc() or similar (a luxury we didn't used to have!)
slim
  • 40,215
  • 13
  • 94
  • 127
0

The least significant digit is num % 10

The next digit can be found by num/=10;

This works for positive numbers, but is implementation defined for negative

mksteve
  • 12,614
  • 3
  • 28
  • 50