0

I created a function that converts numbers to arrays in C++. Due to this creation, I also created the opposite which converts arrays back to numbers:

int convertToNumber(vector<int> nums) {

    int length{ nums.size() };
    int Pow{ length - 1 };
    int factor{ pow(10, Pow) };
    int tempNum{};
    int result_num{};

    for (auto n : nums) {

        tempNum = n;
        tempNum *= factor;
        result_num += tempNum;
        Pow--;

    }

    return result_num;
}

Here is the issue. When I run this using the following code in main:

int main() {

  vector<int> nums {2, 3, 5, 6, 2, 6};

  int number = convertToNumber(nums);

  cout << number << endl;

}

I get 2,400,000 when I should be getting 235,626. I searched for a long time could not find the logic error within the code. Does somebody know what's going on?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • note, with `int convertToNumber(vector nums)` you are making a copy of the vector, which is not so efficient. Instead pass by const reference `int convertToNumber(vector const& nums)`. And also note that `int` is quite big for a sinlge digit. `uint8_t` might be better. – JHBonarius Apr 21 '21 at 15:02

2 Answers2

3

With every iteration of your loop, you are updating Pow - but not factor; and it is factor that's used in your loop, not Pow.

Remember that when you assign an expression to a variable, that expression is only evaluated once, and the result of this single evaluation is what the variable is set to. In other words, the pow() function is not called each time you use factor; it's only called once with the initial value of Pow.

Note the code you provided would not compile. You must specify the namespace before identifiers such as vector and pow - they will not be understood by the compiler otherwise; and you will also need to include the appropriate headers.

If, by chance, you had a using namespace std hiding somewhere in your program - well, don't use that:

Why is "using namespace std;" considered bad practice?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • I am always making stupid mistakes in my program so I am thankful that you pointed that out. Also, I did use `#include ` and `#include `. `` has a math header nested somewhere in it so I didn't have to explicitly include `` or ``. I also used `using namespace std` but I didn't know that was bad practice. I will view the link you posted. – HyperText Markup Man Apr 21 '21 at 15:32
  • @HyperTextMarkupMan: I assumed you had those headers somewhere... But when asking on StackOverflow, your code examples should be such that we can simply copy-paste and compile, and not have to do extra work to fill in the missing bits. This is expected both as a courtesy and as a means to ensure you haven't introduced any "copy-and-paste errors" (something which happens a lot). Obviously you get more leeway since you're new :-) – einpoklum Apr 21 '21 at 20:38
  • Ok! Noted. I will try in the future to do that! It would probably make it so much easier on you guys! – HyperText Markup Man Apr 24 '21 at 15:48
2

KIS (keep it simple)

int convertToNumber(std::vector<int> const& nums) {
    int result_num{0};
    for (auto n : nums) {
        result_num *= 10;
        result_num += n;
    }
    return result_num;
}

or even use an algorithm

int convertToNumber(std::vector<int> const& nums) {
    return std::accumulate(cbegin(nums), cend(nums), 0,
        [](int a, int n) { return a*10+n; });
}
JHBonarius
  • 10,824
  • 3
  • 22
  • 41