Write your question here.
Stuck working on an assignment for adding big integers
Here are the instructions for the assignment
Adding BIG Integers
In C++ an int is in the range 0 to 65535. But what if we need to add larger integers? Say we want
compute the sum 2345566777844567+ 9999988777765768009998. You task in this assignment
is to make this happen.
Write a function
string add(string a, string b)
where a and b are strings representing integers and the function add(a,b) returns a string
representing their sum. Strings a and b can have a maximum of 100 characters.
Write a main program that loops asking the user for the two numbers a and b. The program
should terminate if the user enters “done” what asked for “number” a.
#include <iostream>
using namespace std;
string add(string small, string big) {
string result = "";
int carry = 0;
// compare the length of the passed in variables "small" and "big"
// if "small" has longer length than big, swap "small" and "big"
// write a loop to concat '0's to the beginning of "small" to make two strings equal length.
// loop backward from the last index to index 0 of either "big" or "small"
/* Since string is char array, use big[i] and small[i] to represent each char (each digit).
If you do arithmetic calculation of char, e.g. if you do '0' + '1', you are actually doing 48 + 49 (their ASCII values). You have to convert first. See notes. */
// int value = sum of converted big[i] and converted small[i] and carry;
// if value < 10,
// concat** the value to result.
// set carry to 0
// else,
// concat** the right digit of value only to the result.
// set carry to 1
// Finally (outside the loop), if carry == 1 for the left-most index, add '1' to the beginning of result.
// **Note: You cannot concat int to string, so don't forget to convert int back to char when you concat.
return result;
}
int main() {
// add this three lines to your main to double check if your function works first
cout << add("2345566777844567", "9999988777765768009998") << endl; // 9999991123332545854565
cout << add("9999988777765768009998", "9999988777765768009998") << endl; // 19999977555531536019996
cout << add("2345566777844567", "745566777844567") << endl; // 3091133555689134
// write the required loop descripted in the problem statement.
cout << "End of Program." << endl;
return 0;
}
so far i have the following code below. I just dont understand how to go about doing the arithmetic part. Can anyone please explain
code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <iostream>
using namespace std;
string add(string small, string big) {
string result = "";
int carry = 0;
if (small.length() > big.length()) {
string temp = small;
small = big;
big = temp;
}
int diff = big.length() - small.length();
for (int i = 0; i < diff; i++) {
small = '0' + small;
}
for (int i = big.length() - 1; i >= 0; i--) {
int value = (small[i] - 48) + (big[i] - 48) + carry;
}
}
int main() {
// add this three lines to your main to double check if your function works first
cout << add("2345566777844567", "9999988777765768009998") << endl; // 9999991123332545854565
cout << add("9999988777765768009998", "9999988777765768009998") << endl; // 19999977555531536019996
cout << add("2345566777844567", "745566777844567") << endl; // 3091133555689134
// declare variables and write your loop
string a;
string b;
while (true) {
cout << "pick a large integer" << endl;
cin >> a;
if (a == "done") return 0;
//limits string max of 100 chars
a = a.substr(0, 100);
cout << "pick a second large integer" << endl;
cin >> b;
//limits string max of 100 chars
b = b.substr(0, 100);
cout << "added together equals " << endl;
//prints result
cout << add(a, b) << endl;
cout << "End of Program." << endl;
return 0;
}
}
|