Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my multiply algorithm that is part of my big num class. The arr is a pointer to a class that I created for a Safe resizable array. The main() is obviously incomplete(just put it there to show how my bigint class works in relation to my multiply function) What I really need here is a fix for my algorithm here or give my advice on how to fix it. 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-i, result%10);


            carry=result/10;
        }
        shift++;
        temp1.add_pos(temp2);
    }
    this->assign(temp1);
}


                int main()
                {
                   a.assign (12345);
                   b.assign (12342357);
                   a.multiply (b);
                }
Posted
Updated 5-Apr-15 10:57am
v3

You have obviously copied the add-methode of your class and made it into a multiply-method. But that won't work. You are only multiplying each digit of A with the corresponding digit of arr. To do a multiplication you must however multiply each digit of A with each digit of arr! Just as you would do on paper.

Try it on paper first and then correct your algorithm correspondingly.
 
Share this answer
 
You could also take a look at the Boost Multiprecision Library[^]

Best regards
Espen Harlinn
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900