473,657 Members | 2,493 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multiplication of long integers

10 New Member
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
1 4668
whodgson
542 Contributor
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

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

Similar topics

3
3430
by: Mark Dickinson | last post by:
Can anyone either reproduce or explain the following apparently inconsistent behaviours of __add__ and __mul__? The class Gaussian provides a sub-bare-bones implementation of Gaussian integers (a Gaussian integer is a complex number x+yi for which both x and y are integers): class Gaussian(object): """class representing Gaussian integers"""
3
2041
by: I.V. Aprameya Rao | last post by:
hi i have been wondering, how does python store its very long integers and perform aritmetic on it. i needed to implement this myself and was thinking of storing the digits of an integer in a list. however this would be very slow for operations like division etc.
1
2819
by: akickdoe22 | last post by:
Please help me finish this program. i have completed the addition and the subtraction parts, but i am stuck on the multiplication and division. any suggestions, hints, code, anyhting. it's not a true large int calculator, the numbers entered are at max 100 intergers. CODE SO FAR: #include<iostream> //libraries limited to #include<string> using namespace std;
9
7998
by: Ralf Hildebrandt | last post by:
Hi all! First of all: I am a C-newbie. I have noticed a "strange" behavior with the standart integer multiplication. The code is: void main(void)
87
11195
by: Vijay Kumar R Zanvar | last post by:
Hi, Why multiplication of pointers is not allowed? Till now I only know this, but not the reason why! PS: As a rule, I searched the FAQ, but could not find an answer. -- Vijay Kumar R Zanvar
17
8010
by: Christopher Dyken | last post by:
Hi group, I'm trying to implement two routines to handle 32x32-bits and 64x64-bits signed integer multiplication on a 32 bits machine in C. It easy to find descriptions of non-signed multiplication, however, I haven't found any good descriptions for the signed counterpart. Does any of you have a good reference for this topic? Thanks in advance,
31
3115
by: Pesso | last post by:
What happens if you multiple two integers and the result overflows the MAX_INT in C? Is there a way to trap the condition when it happens?
2
1915
by: helPlease | last post by:
I want to multiply two very long integers (say of 20 digits).I am storing them in character array.Then what mechanism should i follow to mutiply two such numbers.If anybody has any suggestions please post.
1
4873
by: pjerald | last post by:
What is important to Understand about these two functions when multiplying integers and converting it to Long value. ONE WAY. long convertDaysToMilliseconds(int days) { return 1000*3600*24*days; } ANOTHER WAY long convertDaysToMilliseconds(int days) { return 1000L*3600*24*days; } OR static final long MILLISECONDS_PER_DAY = 24L*3600*1000;
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.