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,
-
class large{
-
int no[SIZE]; //array is used to store the large number. Each digit of a the number is stored as an element..
-
// 123 is stored as no[3]={1,2,3}
-
public:
-
large operator *(large& x){ // overload the * operator
-
/*
-
34 x 46
-
-------
-
204 // these values are stored in the
-
136 // two dimensional array mat[][];
-
-------
-
1564 // this the value stored in "large ret"
-
*/
-
large ret;
-
int carry=0;
-
int mat[2*SIZE+1][2*SIZE]={0};
-
for(int i=SIZE-1;i>=0;i--){
-
for(int j=SIZE-1;j>=0;j--){
-
carry += no[i]*x.no[j];
-
if(carry < 10){
-
mat[i][j-(SIZE-1-i)]=carry;
-
carry=0;
-
}
-
else{
-
mat[i][j-(SIZE-1-i)]=carry%10;
-
carry=carry/10;
-
}
-
}
-
}
-
for( i=1;i<SIZE+1;i++){
-
for(int j=SIZE-1;j>=0;j--){
-
carry += mat[i][j]+mat[i-1][j];
-
if(carry < 10){
-
mat[i][j]=carry;
-
carry=0;
-
}
-
else{
-
mat[i][j]=carry%10;
-
carry=carry/10;
-
}
-
}
-
}
-
for( i=0;i<SIZE;i++)
-
ret.no[i]=mat[SIZE][i];
-
return ret;
-
}
-
-
large (){
-
for(int i=0;i<SIZE;i++)
-
no[i]=0;
-
}
-
-
large (string _no){
-
for(int i=0;i<SIZE;i++)
-
no[i]=0;
-
int index=SIZE-1;
-
for( i=_no.length()-1;i>=0;i--,index--){
-
no[index]=_no[i]-'0';
-
}
-
}
-
void print(){ //print the large number
-
int start=0;
-
for(int i=0;i<SIZE;i++)
-
if(no[i]!=0){
-
start=i;
-
break; // find the first non zero digit. store the index in start.
-
}
-
for( i=start;i<SIZE;i++) // print the number starting from start till the end of array.
-
cout<<no[i];
-
cout<<endl;
-
return;
-
}
-
};
-
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