• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1883
  • Last Modified:

Convert array of integers to single int

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
chsalvia
Asked:
chsalvia
  • 2
2 Solutions
 
jkrCommented:
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
 
PaulCaswellCommented:
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
 
Kelvin_KingCommented:
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
 
jkrCommented:
;o)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now