Question:
how can i write this program in c++ please?
soso
2009-10-09 05:00:07 UTC
write Program to add two large integers with up to 300 digits.one approach is to treat each number as a list . each of whose Elements is Block of digits of that number for example the integer 179,534,672,198 might be stored with block[0]=198, block[1]=672 ,block[2]=534.block[3]=179 then add-two integers (lists) elements by Elements carrying from one Element to next when necessary
that's my Q
i need help
Three answers:
ScottVR
2009-10-09 05:38:26 UTC
I'm assuming you aren't looking for someone to do your homework for you and just want some help. The concept you are exploring is commonly known as "bignum", or "arbitrary precision arithmetic", bignum being the data type, and AP being the types of operators/methods you'll create to add, subtract, etc your bignum numbers.



You mention C++; are you supposed to implement a class to do this? Are you to give the class methods such as add(), mul(), etc or have you explored operator overloading in your course yet?



If you'll search out in your book or a search engine the terms I mentioned in the beginning of my answer you will find all the information you need to get this done. Some hints or pointers when you find yourself scratching your head: remember that when storing the string of digits as an array of ints that their position in the string relates to its "place" (in decimal, that would be powers of 10 - the "ones place", the "tens place", etc.) Then, when you have your function that initializes the array with the digits passed to it's constructor method, you'll need to keep the same thing in mind for each "column" (position in the array/"place") that you are adding, subtracting, or other arithmetic operator. You will likely find the modulus operator handy when testing for carries, remainders and the like.



And if you really feel like thinking and learning, perhaps after you've figured out how to do this using base 10, maybe you'd find that breaking it up into blocks of more than one digit (as you do in the example in your question) and treating each block of a certain size as a number in a different number system (base 100? base 1000?) Of course, you'd still print the results as base 10 digits using the normal 0-9 printable ascii characters, but deal with them in the other base number system when performing arithmetic operations on the blocks.

Simplest route I think is to keep it all base 10, one character (digit) per integer array element, but it's a cool idea that might get you bonus points!
?
2009-10-09 05:21:57 UTC
An alternate approach is just treat each large number as an array of digits (you can use a char for each digit). Then do the addition the same way you would do it "long hand" with a pencil and paper.



If you want to do it a more efficient way though then you need around 1000 bits to represent a 300 digit decimal number. Let's round that up to 64 bytes. Assuming sizeof(int) is 4 then you can have an array of 16 ints to represent each number.
2016-05-21 09:39:14 UTC
Visual Studio is a good one. You can use any text editor to write programs. Just remember you need a compiler after the program is written.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...