...

/

Add, Subtract, and operator[]

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 12341234 (one thousand two hundred thirty-four but stored as 4 3 2 1) to 9898 (ninety-eight but stored as 8 9), we would carry out the following procedure:

Press + to interact
Sum of two integers
1 / 9
Sum of two integers

The result will be 13321332, 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
Add operation

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 ...