Lesson 4

Adding Extraordinarily Large Numbers in C++


Hello and welcome! Today, we'll delve deep into a captivating problem involving large numbers -- specifically, adding extraordinarily large numbers. As you may have noticed, traditional calculators and even some programming languages struggle when dealing with excessively large numbers. To handle such scenarios efficiently, we'll simulate this process manually using strings. By the end of this discussion, you'll be able to add together numbers that have thousands or even tens of thousands of digits. Intriguing, right? Let's get started!

Task Statement

In today's task, we'll venture into the realm of large numbers, in which we are given two exceedingly large positive integers. However, these aren't your average, everyday large numbers. They are so enormous they're represented as strings that can be up to 10,000 digits long!

Your mission, should you choose to accept it, is to write a C++ function that adds these two "string-numbers" together. The challenge is to perform the addition without converting these entire strings into integers.

At the end, your function should return the resulting sum, represented as a string. At first glance, this might seem daunting, but don't worry -- we'll break it down step by step, emulating the way we manually add numbers.

Solution Building: Step 1

Before we dive into the code, let's first discuss the strategy we're going to follow. Remember that every digit in a number carries value, and the position of the digit determines its influence on the total value of the number. This system is known as place-value notation.

The first step involves initializing our variables. We'll use two ints, i and j, to point to the current digit in num1 and num2, respectively. We'll also need a carry int variable to hold the carryovers from each addition operation. Lastly, we'll use a vector of chars, named result, to store our resultant number, where each digit from the addition is appended to the front.

1std::string addLargeNumbers(std::string num1, std::string num2) { 2 int i = num1.size() - 1; 3 int j = num2.size() - 1; 4 int carry = 0; 5 std::vector<char> result;
Solution Building: Step 2

After initializing our variables, it's time to move on to the next step. Here, we'll scan through num1 and num2 from right to left, moving from the least significant digit to the most significant one.

For each iteration, we extract the digits n1 from num1 and n2 from num2. If i or j is less than 0, that means we've processed all the digits in one of the numbers. Therefore, we treat any additional digits as 0.

1std::string addLargeNumbers(std::string num1, std::string num2) { 2 int i = num1.size() - 1; 3 int j = num2.size() - 1; 4 int carry = 0; 5 std::vector<char> result; 6 7 while(i >= 0 || j >= 0 || carry) { 8 int n1 = (i >= 0) ? num1[i] - '0' : 0; 9 int n2 = (j >= 0) ? num2[j] - '0' : 0; 10 i--; 11 j--;
Solution Building: Step 3

After obtaining digits n1 and n2, our next step is to add them. This addition also includes the carry, which accumulates any overflow from the addition of previous column digits. This sum results in a one- or two-digit number, where the tens place becomes a new carry and the units place is the result digit.

Subsequently, we add current to the result array and decrement both i and j before starting the next iteration. Finally, we reverse result and join the digits together to obtain our result.

1#include <algorithm> 2#include <iostream> 3#include <vector> 4 5std::string addLargeNumbers(std::string num1, std::string num2) { 6 int i = num1.size() - 1; 7 int j = num2.size() - 1; 8 int carry = 0; 9 std::vector<char> result; 10 11 while(i >= 0 || j >= 0 || carry) { 12 int n1 = (i >= 0) ? num1[i] - '0' : 0; 13 int n2 = (j >= 0) ? num2[j] - '0' : 0; 14 int current = n1 + n2 + carry; 15 carry = current / 10; 16 current = current % 10; 17 result.push_back('0' + current); 18 i--; 19 j--; 20 } 21 22 std::reverse(result.begin(),result.end()); 23 return std::string(result.begin(), result.end()); 24} 25 26int main() { 27 std::cout << addLargeNumbers("1454178195297", "8458263917502"); 28 return 0; 29}
Lesson Summary

Congratulations! You have successfully implemented a method to add very large numbers by mimicking the way we traditionally perform addition operations. Achieving this not only requires a robust understanding of place-value notation but also the ability to manipulate strings and arrays effectively. This task was likely challenging, but remember, every struggle leads to greater accomplishment. Now, with this powerful tool in your arsenal, you can confidently tackle problems involving large numbers. In the upcoming practice session, you can test your new skills with a range of similar challenges. Enjoy coding, and remember, learning is a journey, so take pleasure in the ride!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.