Solved

# Convert array of integers to single int

Posted on 2006-04-14
1,879 Views
Suppose I have an array of single-digit, base 10 integers, like:

int intarray = { 2 , 5, 7 };

Is there a way I can convert this into a single int, with a value of 257?
0
Question by:chsalvia

LVL 86

Accepted Solution

You mean like

int a2int(int* arr, size_t sz) {

int ret = 0;

for (int i = 0; i < sz; ++i) {

ret *= 10;

ret += arr[i];
}

return ret;
}

?
0

LVL 16

Expert Comment

Hi chsalvia,

Could you please post what you have tried and we can help you understand what you are doing wrong or help you out with the parts you find difficult.

Paul
0

LVL 13

Assisted Solution

You could do it.

Firstly, each array represents a power of ten.

So lets say you have an int array [1 2 3 4 5]

this means, your integer will be ( working backwards ):
(5 * 10 ^ 0) + (4 * 10 ^ 1) + (3 * 10 ^ 2) + (2 * 10 ^ 3) + (1 * 10 ^ 4)

so if you see the pattern you could write a simple for loop which iterates through the int array, multiply it by it's 10's coefficient, and add it to the total sum.

Good luck with it.
0

LVL 86

Expert Comment

;o)
0

## Featured Post

### Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.