By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,246 Members | 1,515 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,246 IT Pros & Developers. It's quick & easy.

multiplication of long integers

P: 10
Hi,

Im doing a multiplication of two large integers which are store in arrays(such as 2345 is in array as 2 in a[0], 3 in a[1] and so on)like this im storing integers in two arrays.The logic i have used is explained with code below,

Expand|Select|Wrap|Line Numbers
  1. class large{
  2.     int no[SIZE];    //array is used to store the large number. Each digit of a the number is stored as an element..
  3.                     // 123 is stored as no[3]={1,2,3}
  4. public:
  5.        large operator *(large& x){ // overload the * operator
  6. /*
  7.         34 x 46
  8.         -------
  9.           204            // these values are stored in the
  10.          136            // two dimensional array mat[][];
  11.         -------
  12.          1564   // this the value stored in "large ret"
  13. */                                
  14.         large ret;                
  15.         int carry=0;
  16.         int mat[2*SIZE+1][2*SIZE]={0};
  17.         for(int i=SIZE-1;i>=0;i--){
  18.             for(int j=SIZE-1;j>=0;j--){
  19.                 carry += no[i]*x.no[j];
  20.                 if(carry < 10){
  21.                     mat[i][j-(SIZE-1-i)]=carry;
  22.                     carry=0;
  23.                 }
  24.                 else{
  25.                 mat[i][j-(SIZE-1-i)]=carry%10;
  26.                     carry=carry/10;
  27.                 }
  28.             }
  29.         }
  30.         for( i=1;i<SIZE+1;i++){
  31.             for(int j=SIZE-1;j>=0;j--){
  32.                 carry += mat[i][j]+mat[i-1][j];
  33.                 if(carry < 10){
  34.                     mat[i][j]=carry;
  35.                     carry=0;
  36.                 }
  37.                 else{
  38.                     mat[i][j]=carry%10;
  39.                     carry=carry/10;
  40.                 }
  41.             }
  42.         }
  43.         for( i=0;i<SIZE;i++)
  44.             ret.no[i]=mat[SIZE][i];
  45.         return ret;
  46.     }
  47.  
  48.     large (){
  49.         for(int i=0;i<SIZE;i++)
  50.             no[i]=0;
  51.     }
  52.  
  53.     large (string _no){
  54.         for(int i=0;i<SIZE;i++)
  55.             no[i]=0;
  56.         int index=SIZE-1;
  57.         for( i=_no.length()-1;i>=0;i--,index--){
  58.             no[index]=_no[i]-'0';
  59.         }
  60.     }
  61.     void print(){        //print the large number
  62.         int start=0;
  63.         for(int i=0;i<SIZE;i++)
  64.         if(no[i]!=0){
  65.             start=i;
  66.             break;        // find the first non zero digit. store the index in start.
  67.         }
  68.         for( i=start;i<SIZE;i++) // print the number starting from start till the end of array.
  69.             cout<<no[i];
  70.         cout<<endl;
  71.         return;
  72.     }
  73. };
  74.  
After multiplying the integers i store it in matrix form my problem is in storing in the matrix .If i do according to above program im unable to that..so plz anyone help me to sort out this problem.

thank u
Sep 29 '09 #1
Share this Question
Share on Google+
1 Reply


100+
P: 542
Why don`t you store the results in a vector and then work out how to read from that vector into the desired container form to present as a matrix?
Oct 4 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.