Adding Big Integers string char conversion

Feb 8, 2020 at 10:40pm
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;
	}
}
Feb 9, 2020 at 12:28am
Your biggest issue is that you never return anything from your add function, so you couldn't possibly get the right results. You also aren't doing anything with your "value" you calculate.

Instead of starting with those huge numbers, start off with small examples.

First, simply try adding two 1-digit numbers together:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
using namespace std;

string add(string small, string big) {
    return "";	
}

int main() {
	cout << add("2", "3") << endl;
}


Then, try 1-digit numbers, but this time you have account for the carry digit:
add("7", "5");

etc. Work your way up. Add in a loop while accounting for the carry digit. It looks like you're getting close; I suggest just going through your logic, line by line, with simple inputs. When does your carry digit calculation need to be updated?

Perhaps try to do the steps necessary by hand on paper, and carefully try to translate that into code.
Last edited on Feb 9, 2020 at 12:32am
Feb 9, 2020 at 2:05am
FYI this is very wrong.
in C++ an int is in the range 0 to 65535. First that is an unsigned something; int is signed and is -x to y in range. Second, the size of 'int' depends on the compiler. Whoever is teaching you is being very careless saying things like that.

Feb 9, 2020 at 4:20am
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
string add(string small, string big)
{
    int carry = 0;
    
    string temp;
    if (small.length() >= big.length())
    {
        temp = big;
        big = small;
        small = temp;
    }
    
    int diff = big.length() - small.length();
    
    for (int i = 0; i < diff; i++)
    {
        small = '0' + small;
    }
    
    string result(big.length(), blah blah); // <--
    carry = 0;
    int temp_add = 0; // THIS IS AN INTEGER
    for (int i = big.length() - 1; i >= 0; i--)
    {
        temp_add = (small[i] - '0') + (big[i] - '0') + carry;  // <--
        result[i] = blah blah;
        carry = blah blah;
    }
    
    return result; // <--
}
Feb 9, 2020 at 10:10pm
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#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;


		char v = (char)value;
		if (value < 10) {
			v = value + 48;
			result = v + result;
			carry = 0;

		}
		else {
			value = value % 10;
			carry = 1;
			v = value + 48;
			result = v + result;
			
		}
	}
		if (carry == 1) {
			result = '1' + result;
		}
	
		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

	// 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;
	}
}


this is what i got and it works. i appreciate the help!
Topic archived. No new replies allowed.