Hi. I need some help with an arrays. I have an array:
, and i want to make the number from the numbers in the array, in this case 1 * 100 + 2 * 10 + 3 * 1 = 123. Cone somebody help me please?Code:array[3] = {1, 2, 3}
Thank you a lot.
Hi. I need some help with an arrays. I have an array:
, and i want to make the number from the numbers in the array, in this case 1 * 100 + 2 * 10 + 3 * 1 = 123. Cone somebody help me please?Code:array[3] = {1, 2, 3}
Thank you a lot.
You haven't said what data type in is the array.
Mainframe assembler programmer by trade. C coder when I can.
It's integer.
Easy if you know how decimal works,
Work with positional notation, and each position is a power of 10 (decimal).
For example 123 is
(right-to-left): 10^0 * 3 + 10^1 * 2 + 10^2 * 1
So, in sort traverse your array backwards, and add each element * 10^i (Where i is the iteration of your loop) together.
You'll need pow(): http://www.cplusplus.com/reference/c...cmath/pow.html
Maybe using string is a way
Code:int array[3] = {1, 2, 3}, num; char temp[4]; for(i = 0; i < 3; i++) temp[i] = array[i] + '0'; temp[i] = 0; num = atoi(temp);