[Solved] “copy” elements of array in order into an “int”? C [closed]


Does array[0] always represent the 10’s place and array[1] always represent the 1’s place? Will array[0] or array[1] ever have a value greater than 9 or less than 0? Will there ever be an array[2] or an array[3] to represent a 3 digit or a 4 digit number?

If the answer to the above questions are Yes, No, and No, then isn’t the answer simple arithmetic?

int result = (array[0] * 10) + array[1];

If the data isn’t pre range-checked, then you’ll need to add that step. Even if the data IS pre range-checked, you should consider adding that step anyway to make extra sure.

’10’ is also a magic number in the above example. It would probably be wise to not hard-code 10, but base it off of the size of the array. Consider the case where there IS an array[2] . . . array[n]. What then?

8

solved “copy” elements of array in order into an “int”? C [closed]