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?