0

I have been trying to figure this out for now 4 hours. And I just can't find it, I give up. I have to create a class where I store the digits of large numbers in a vector. And I also have to create functions for them. One of them is for multiplying. I know this isn't how you multiply a large number, this is just a test function for it. But it doesnt work. It doesn't give errors, the program just waits for a while, then prints nothing (I print it in main). I have already created classes many times, and they worked, and their functions returned their types perfectly. The only way this is working for me is like this:

largeNum mul(int x){
        for(int i=0; i<_numArr.size(); i++){
            _numArr[i] = _numArr[i] * x;
        }
   return *this;  
}

But I dont want to overwrite my original vector. I want to give the result to a new largeNum type object's vector. I tried pointers with it in every way, and its just not working. Maybe because it's 3 AM, but I'm not getting anywhere. This is what i'm trying with:

class largeNum{
    public:
        std::vector<int> _numArr;

    public:
        largeNum(){
            for(int i=0; i< _numArr.size(); i++){
                _numArr[i]=0;
            }
        }


    largeNum mul(int x){
        largeNum res;
        for(int i=0; i<_numArr.size(); i++){
            res._numArr[i] = _numArr[i] * x;
        }
        return res;
    }

}



/* the printing part in main.cpp:

largeNum b = n1.mul(2);
for(int i=0; i < b._numArr.size(); i++){
        cout << b._numArr[i] << endl;
}
*/

Please help.

Vivi
  • 75
  • 7

2 Answers2

4

Neither the constructor nor mul() are resizing the target vector at all. So, because the vector is empty, the loop inside the constructor is a no-op, and the loop inside mul() has undefined behavior for accessing elements out-of-bounds.

Try something more like this:

class largeNum{
    public:
        std::vector<int> _numArr;

    public:
        largeNum() {
            // push values into the vector as needed...
            _numArr.resize(someSize, 0);
        }

        largeNum(const largeNum &src) : _numArr(src._numArr) {}

        largeNum mul(int x) const {
            largeNum res(*this);
            for(size_t i = 0; i < res._numArr.size(); ++i) {
                res._numArr[i] *= x;
            }
            return res;
        }
};

Alternatively:

class largeNum{
    public:
        std::vector<int> _numArr;

    public:
        largeNum() {}

        largeNum(const largeNum &src) : _numArr(src._numArr) {}

        largeNum mul(int x) const {
            largeNum res;
            res.reserve(_numArr.size());
            for(size_t i = 0; i < _numArr.size(); ++i) {
                res.push_back(_numArr[i] * x);
            }
            return res;
        }
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

You forget to push back a number to res:

largeNum mul(int x){
    largeNum res;
    for(int i=0; i<_numArr.size(); i++){
        if (res._numArr.size() == i) res._numArr.push_back(0);
        res._numArr[i] = _numArr[i] * x;
    }
    return res;
}
Hancel Lin
  • 345
  • 2
  • 10