Well since I worked on your original question, I'll go ahead and post the results for that as well in case you change your mind. Converting a number to an array can be approached in a number of ways. Here is one scheme using a recursive function and a helper to correct the order of the digits.
note: for 32-bit OS, overflow will occurr due to x86 utilizing a 4-bit long, a 32-bit safe version using 8-bit long long is included below, a 3rd version using preprocessor directives integrating both versions is included at the end:
#include <stdio.h>
#include <stdlib.h>
#define MAXDIG 32
void digits2array (long x, long *a, size_t *idx);
int digits2array_rev (long x, long *a, size_t *i);
int main (void) {
long n = 25464878454;
long ar[MAXDIG] = {0};
size_t idx = 0;
int i = 0;
digits2array (n, ar, &idx); /* convert n to array */
printf ("\n array:\n\n");
for (i = 0; i < idx; i++) /* output results */
printf (" ar[%2d] : %ld\n", i, ar[i]);
return 0;
}
/* converts x to array of digits in a (reverse order) */
int digits2array_rev (long x, long *a, size_t *i)
{
if (x < 10) {
a[(*i)++] = x;
return x;
}
a[(*i)++] = x % 10;
return digits2array_rev (x / 10, a, i);
}
/* helper function to reverse results of digits2array_rev */
void digits2array (long x, long *a, size_t *idx)
{
long tmp[MAXDIG] = {0};
int i = 0;
digits2array_rev (x, tmp, idx); /* fill array with digits (reversed) */
for (i = 0; i < *idx; i++) /* reverse to correct order */
a[*idx - 1 - i] = tmp[i];
}
Output/Results
$ ./bin/digits2array
array:
ar[ 0] : 2
ar[ 1] : 5
ar[ 2] : 4
ar[ 3] : 6
ar[ 4] : 4
ar[ 5] : 8
ar[ 6] : 7
ar[ 7] : 8
ar[ 8] : 4
ar[ 9] : 5
ar[10] : 4
32-bit Safe Version (using long long)
On 32-bit OS's overflow would still occur. Changing the types to long long (8-bit int on x86), allows the program to operate on x86 without issue.
#include <stdio.h>
#include <stdlib.h>
#define MAXDIG 32
void digits2array (long long x, long long *a, size_t *idx);
long long digits2array_rev (long long x, long long *a, size_t *i);
int main (void) {
long long n = 25464878454;
long long ar[MAXDIG] = {0};
size_t idx = 0;
int i = 0;
digits2array (n, ar, &idx); /* convert n to array */
printf ("\n array:\n\n");
for (i = 0; i < idx; i++) /* output results */
printf (" ar[%2d] : %lld\n", i, ar[i]);
return 0;
}
/* converts x to array of digits in a (reverse order) */
long long digits2array_rev (long long x, long long *a, size_t *i)
{
if (x < 10) {
a[(*i)++] = x;
return x;
}
a[(*i)++] = x % 10;
return digits2array_rev (x / 10, a, i);
}
/* helper function to reverse results of digits2array_rev */
void digits2array (long long x, long long *a, size_t *idx)
{
long long tmp[MAXDIG] = {0};
int i = 0;
digits2array_rev (x, tmp, idx); /* fill array with digits (reversed) */
for (i = 0; i < *idx; i++) /* reverse to correct order */
a[*idx - 1 - i] = tmp[i];
}
64/32-bit Version w/Preprocessor Directives
You can accomplish the same thing for x86, while preserving the original types for x86_64 through the use of preprocessor directives. (understanding that there is actually no storage benefit -- long (8-bit on x86_64), long long (8-bit on x86)).
#include <stdio.h>
#include <stdlib.h>
#if defined(__LP64__) || defined(_LP64)
# define BUILD_64 1
#endif
#define MAXDIG 32
#ifdef BUILD_64
void digits2array (long x, long *a, size_t *idx);
int digits2array_rev (long x, long *a, size_t *i);
#else
void digits2array (long long x, long long *a, size_t *idx);
long long digits2array_rev (long long x, long long *a, size_t *i);
#endif
int main (void) {
#ifdef BUILD_64
long n = 25464878454;
long ar[MAXDIG] = {0};
#else
long long n = 25464878454;
long long ar[MAXDIG] = {0};
#endif
size_t idx = 0;
int i = 0;
digits2array (n, ar, &idx); /* convert n to array */
printf ("\n array:\n\n");
for (i = 0; i < idx; i++) /* output results */
#ifdef BUILD_64
printf (" ar[%2d] : %ld\n", i, ar[i]);
#else
printf (" ar[%2d] : %lld\n", i, ar[i]);
#endif
return 0;
}
/* converts x to array of digits in a (reverse order) */
#ifdef BUILD_64
int digits2array_rev (long x, long *a, size_t *i)
#else
long long digits2array_rev (long long x, long long *a, size_t *i)
#endif
{
if (x < 10) {
a[(*i)++] = x;
return x;
}
a[(*i)++] = x % 10;
return digits2array_rev (x / 10, a, i);
}
/* helper function to reverse results of digits2array_rev */
#ifdef BUILD_64
void digits2array (long x, long *a, size_t *idx)
{
long tmp[MAXDIG] = {0};
#else
void digits2array (long long x, long long *a, size_t *idx)
{
long long tmp[MAXDIG] = {0};
#endif
int i = 0;
digits2array_rev (x, tmp, idx); /* fill array with digits (reversed) */
for (i = 0; i < *idx; i++) /* reverse to correct order */
a[*idx - 1 - i] = tmp[i];
}
maxto take appropriate actions (i.e. like increment another counter to keep track of the number of times you reachmax?)unsigned long longthen you would have duplicated the rangediv_resultwhich islong long. Q2, you wantdiv_resultto be an array. That's the coding style of the operands innit?