'multiplication very large int in arrays

I have some exercise from my school. I have to write program in c++ which, miltiplication two large int. (max digits one of them is 100). I wrote some program, i don't know how i can extend it by bigger numbers and enter number from keyboard (I have some idea with enter numbers like string and convert it to int but, i dont know how make dynamic alloced array 2d with 0 inside.) It is simple code becouse i am begiiner of C++ and please help me in my skills area; I will be grateful for pointing errors in my code.

#include <iostream>


using namespace std;

int hide_zero(const int * t)
{
    int o;
    for(int k = 8; k>=0; k--)
    {
        if(t[k]!=0) {
            o = k;
            break;
        }
    }
    return o;
}

int main() {


    int arr1[4]={1,2,3,4}; //4321
    int arr2[4]={1,2,3,4}; //4321

    const int arr1_l = 4;
    const int arr2_l = 4; // += more rows.

    int tab[4][9]={0}; // I think rows should be = arr2_l;  and columns = min. arr1_l+ arr2_l

    int il;
    for(int i = 0; i < arr2_l; i++)
    {
        for(int j = 0; j < arr1_l; j++)
        {
            il = arr2[i]*arr1[j];
            if(il+tab[i][i+j]>=10)
            {
                tab[i][j+i+1] = (il+tab[i][i+j])/10;
                tab[i][i+j]=(tab[i][i+j] + il)%10;
            }
            else if(il+tab[i][i+j]<10)
            {
                tab[i][i+j]=tab[i][i+j] + il;
            }
        }
    }
    int sum[9]={0};

    for(int i=0; i < 9; i++)
    {
        for(int j = 0; j<4; j++)
        {
            sum[i]+=tab[j][i];
        }
        if(sum[i]>=10)
        {
            sum[i+1]= sum[i]/10;
            sum[i] = sum[i]%10;
        }
        if(sum[i]<10)
        {
            sum[i]=sum[i];
        }
    }

    for(int i=0; i<4; i ++)
    {
        for(int k = 8; k>=0; k--)
        {
            cout << tab[i][k] << " ";
        }
        cout << endl;
    }

    cout << endl;

    for(int k = hide_zero(sum); k>=0; k--)
    {
        cout <<sum[k];
    }
}

I tried extended it like this: create string a,b; enter a, b; convert like: in for arr1[0]=a[i]-48;

but i dont know how i should create array with 0 inside

arr[l_1][l_2] not work ;/



Solution 1:[1]

I'm not going to do the problem for you, but I'll tell you how I would do it. This is how I did the exact same problem about (literally) 45 years ago.

First, I would define a class called BigInt.

 class BigInt {
 public:
      BigInt();
      BigInt(const std::string &fromStr);
      
      BigInt multiply(const BigInt &rhv) const;

 protected:
      std::vector<unsigned int> vec;
      bool isNeg = false;
 };

 std::ostream &operator<<(std::ostream &ostr, const BigInt &);

That's just a start. I would store the value of the BigInt in an vector of ints.

You would need to implement converting a string into a BigInt (the second constructor) and the other required methods. The construct-from-string would just take each digit, starting at the 1s digit, and stuff them into the vector with push_back.

Then I would use paper and pencil and figure out how to do long multiplication manually. Once you do a simple problem on paper, you should be able to figure out how to do it in code.

This is about the most useful beginner level problem you can write, from a learning perspective. It makes you think like a computer while learning a variety of important concepts.

Good luck. Come on back when you get further.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joseph Larson