Convert Int array to an integer

Nov 23, 2013 at 2:38am
Is there anyway we can make an integer array to an integer

Thank you
Nov 23, 2013 at 2:46am
Yes. Can you be more specific about what you wish to do?
Nov 23, 2013 at 2:48am
Duoas


Of course

IF we have an integer array such as int A[3] = {1,2,3}

How do we make it int A = 123.

Thanks
Nov 23, 2013 at 2:53am
Nov 23, 2013 at 2:57am
Remember your basic math. This is kind of the opposite of logarithm -- each element is multiplied by a power of ten.

123 = 1×102 + 2×101 + 3×100

Another example:

7926 = 7×103 + 9×102 + 2×101 + 6×100

The simplest way for your array is an iterative approach:

1
2
3
4
result = 0
for each element in array, first to last:
  result *= 10
  result += element

Hope this helps.
Topic archived. No new replies allowed.