-1

So here I have two classes, on Safearray and one bigint calculator( the SafeArray is imperfect but it serves its purpose) what I really need help with is my multiply algorithm, when I compile nothing happens, I think the algorithm is mostly right(if not please help me fix it) but mostly I think the problem is returning and printing the final answer. Any help would be appreciated, Thanks.

    void multiply(const bigint &A)
    {
        bigint temp1; // bigint with value 0

        int carry = 0;
        int shift = 0;
        bigint temp2;
        for(int j = size-1; j >= 0; j--) //no member size in bigint
        {
            for(int i=size-1; i>=0; i--)
            {
                // bigint with value 0 and size: size + A.size
                int result = (arr->get(i)*A.arr->get(j)+carry);

                if(size - shift - (i - size-1) >= 0)
                    temp2.arr->set(size - shift - (i - size-1), result%10);
                else
                    break;

                carry=result/10;
            }
            shift++;
            temp1.add_pos(temp2);
        }
        this->assign(temp1);
    }
jack
  • 19
  • 1
  • 6
  • Looking at your code, it's not clear to mewhether you are storing the most significant digit in `arr[0]` or the least significant digit in `arr[0]`. If you store the least significant digit in `arr[0]`, some of your logic will be simplified, and easier to debug. – R Sahu Apr 05 '15 at 01:25

2 Answers2

1

some hint:

your resize did not do initialization of extending part

your size - shift - (i - size-1) part is definitely wrong. it increases while i decreases...

why not starting with simple case like a = 1 and b = 2, then using debugger trace line by line?

another useful track would be:

try to trace your code line by line comparing to your math by hand with the same question, then you can easily find out the logic error you are making.

0

Don't implement math library by yourself unless you do it as homework or for fun. Such thinks has been made several times by someone else who put much more effort on it. Download some ready math library with big int support.

Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148