Add, Subtract, and operator[]
Learn how we can add or subtract two huge integers.
In a previous lesson, we encountered a challenge when adding two integers with different lengths. The traditional approach of directly adding the corresponding digits from the two numbers becomes complicated due to mismatched indexing. To address this issue, we adopted a new approach by reversing the order in which we store the digits in an array. By doing so, we achieved a more intuitive indexing system where the unit digit is located at index 0, the 10’s digit is at index 1, and the highest significant digit is at the last index of the array. This reversal of digit order simplifies the addition process and allows for easier handling of numbers with varying lengths.
For example, if we want to add (one thousand two hundred thirty-four but stored as 4 3 2 1) to (ninety-eight but stored as 8 9), we would carry out the following procedure:
The result will be , stored in reverse order as 2331, as shown in the last slide above. Let’s implement this idea now.
Note: The 100th and 1000th digit of the number 98 is zero.
Your implementation
Write your code for the four upcoming functions here:
3 400 500 600
Let’s move toward the addition and see how we can add two numbers.
Addition
We add two declarations of operator[]
to enable the addition operation between two integers. These operators take an index as a parameter and are responsible for returning the value of the corresponding ...