I have no idea how to write a code which would take numbers from the array and equal them to an integer number without 0
.
For example, I have an array A[size]= {12,68,45,20,10}
and I need to get the answer as n= 12684521
. Any ideas or hints how to make it?
It is possible to do it without using strings or stringstreams. Here's the code:
int integer(int* arr, size_t length)
{
int result = 0;
for (size_t i = 0; i < length; i++)
{
int t1 = arr[i], t2 = 0;
while (t1 != 0)
{
if(t1 % 10 != 0)
{
t2 *= 10;
t2 += t1 % 10;
}
t1 /= 10;
}
while (t2 != 0)
{
if(t2 % 10 != 0)
{
result *= 10;
result += t2 % 10;
}
t2 /= 10;
}
}
return result;
}
The quirky thing is to make two inner while loops because operations in one of those loops mirror the numbers (e.g. if arr[i]
is 68 then t2
becomes 86 and then 6 and 8 are appended to result
).