• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

How to make a C++ program that multiplies 32 digit numbers?

32bit numbers wouldn't have been that bad, but 32 digit...

I'm sure there's a "bignum" library out there, but in case you have to or want to program it on your own, here's the basics:

Create a class that holds a vector (array for those still pre-STL) of integers.
Create a way for two of said vectors of arbitrary size to be added.
Using those building blocks, implement a operator *.

I know it was assigned as a programming assignment last semester at my college -- I had to help 5 people with it.

It's not that bad, it just takes some time.

Good luck!
Andrew

 
That's really easy to do. Simply create character arrays of size 32 and then use pointers to step through each item in the array (starting from the back end of course) and multiply each entry with each corresponding entry from the other array (ie number).

You'll need to use the modulus operator to determine what the carried number is and it will make your job easier if you zero-pad each number you use.

I could probably do it in 10 minutes. 🙂
 
ok i'm getting problems with pulling the array into the function. I have the multiply function down but none of us (not-so-smart peoples) can figure this out. we're also having problems with converting char to int then back to char. actually we're having lots of problems. anyone know of an outline. any help would be greatly appreciated.
 
Casting char to int is simple, but remember that you'll get an ascii value, not the actual number. I think going the other way also has this problem. In any case, it's not too difficult to solve. Since you mention a 32 digit limit on your numbers, you shouldn't have to mess with any dynamic data structures. Just declare a (struct or class I'm assuming) with a 32 element array of ints. Then overload the * operator and work on the things digit by digit as if you were doing long division.
 
First off think of your class structure.

What you need:
2 32 character arrays (for the input) (a[32] b[32])
2 32 int arrays (ai[32] bi[32])
1 64 int array (for the output) (o[64])
1 temp int (temp)
the multiplication function
the output function (could be a part of the mult function)
a function for loading the numbers into the 32 character arrays (could be done without a function)

Ok, to begin with you'll need to load some values into your character arrays. I would suggest using a batch type file for this task - though it could be done interactively. Next you will want to multiply the numbers - the idea I have is the following: Take each character from the arrays a[32] and b[32] and convert to int using atoi, and put them in the corresponding slot in ai[32] or bi[32]. Next step through ai multiplying each element by all of the others in bi keeping note of the positions of the elements being multiplied (note this should be done low to high). The product of these individual multiplications will be stored in temp. Now, realize that temp will have a value of 0 - 81. To convert back to a single digit - divide by 10 to get the high int, modulus by 10 to get the low int. Now add these to the out array (which should have been initialized to zeros) keeping in mind the beginning array positions for zero padding. To take care of carrying numbers using the same technique used to split the product into single digits (hint: a div of 10 that results in 0 means no more carrying). Once you have done this for all of the numbers simply output the out array high element to low element and you are done.

Hope that helped, and good luck.
 
atoi shouldn't be necessary, because all you have to do is deal with the individual chars in the array.
Lets say you read things into: char a[32], and you want to get things into int b[32]

b=((int)a)-*offset*) where *offset* is what you need to convert the ascii number of the digits into the number.

If you use atoi to put the contents of char a[32] into an integer, lets say int c, then if the value of the number you've just read exceeds the maximum value for your data type (int, unsigned int, long int, whatever...) then you will have problems. I'm assuming that the whole point of the assignment is to work with numbers that have larger values than can be accomodated by the commonly used data types.

edit:
..
 
The offset is 48.
Like the other guys say, there is no need to use atoi.
And don't bother using classes. The problem is too small to be stuffing around with them.
 
Back
Top