8

Think I have an integer array like this:

a[0]=60; a[1]=321; a[2]=5;

now I want to convert the whole of this array into an integer number, for example int b become 603215 after running the code.

How to do it?

2
  • 3
    I do have to say I've never thought about doing this, or seen anyone else trying to do it. Kudos for the originality, or picking a question that's hidden itself from me. Commented Jun 26, 2012 at 5:14
  • 1
    This is similar, but simpler than this question. My solution applies to this problem, but since you don't care about formatting then the stringstream solution seems simpler. Commented Jun 26, 2012 at 5:37

6 Answers 6

15

Use a std::stringstream:

#include <iostream>
#include <sstream>

int main() {
    std::stringstream ss;
    int arr[] = {60, 321, 5};

    for (unsigned i = 0; i < sizeof arr / sizeof arr [0]; ++i)
        ss << arr [i];

    int result;
    ss >> result;
    std::cout << result; //603215
}

Note that in C++11 that mildly ugly loop can be replaced with this:

for (int i : arr)
    ss << i;

Also, seeing as how there is a good possibility of overflow, the string form of the number can be accessed with ss.str(). To get around overflow, it might be easier working with that than trying to cram it into an integer. Negative values should be taken into consideration, too, as this will only work (and make sense) if the first value is negative.

Sign up to request clarification or add additional context in comments.

Comments

7
int a[] = {60, 321, 5};

int finalNumber = 0;
for (int i = 0; i < a.length; i++) {
    int num = a[i];
    if (num != 0) {
        while (num > 0) {
            finalNumber *= 10;
            num /= 10;
        }
        finalNumber += a[i];
    } else {
        finalNumber *= 10;
    }
}

finalNumber has a result: 603215

6 Comments

Just a couple of other things, the first being a Java->C++ one: 1. Arrays aren't classes in C++, and don't have a length member. It's typically better to use std::vector instead, as it offers the functionality. 2. a should be arr.
@Kalai Thank you too, Nice and Smart Answer :))
@Kalai your code has a problem, think a[0]=50; a[1]=1; a[2]=0; then the result should be 5010, but your code will skip 0 and will show 501.
@Stranger Thanks for your comment. You are having good analysing power. I have edited the code. Now, it will work.
@Kalai it is not working yet!!! use int a[] = {60, 321, 0}; as the first line and you will get no answer at all !!!
|
6

Concat all the numbers as a string and then convert that to number

#include <string>
int b = std::stoi("603215");

1 Comment

I know the question isn't too specific, but I would imagine the array elements can be arbitrary (to the point where it doesn't overflow the integer). Hardcoding a string value won't get around that. The function is a good alternative start (assuming you have C++11) to the stringstream, though.
3

This algorithm will work:

  1. Convert all the integer values of array into string using for loop.
  2. Append all the string values now to one string from index 0 to length of array.
  3. Change that string into an integer again.

1 Comment

I made your algorithm easier to follow when reading it. FFR, there's a numbered list button at the top of the editing space.
2

Iterate the array and convert the values into string. Then concatenate all of them and convert back to integer.

#include <string>

int a[] = {60, 321, 5};
std::string num = "";
for(auto val : a)
    num += a;
int b = std::stoi(num);

Comments

2
Array to Integer conversion

Number should be stored in opposite way

 for(int i=0;i<d;i++)
    {
        ans += num[i]*pow(10,i);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.