String array to function for addition

I am working on a project that adds and multiplies two very large integers (up to 200 digits for addition and 100 digits for multiplication). I've been able to store the large integer as a string, then break that string up into elements of 4. Now I'm working on the addition function and I'm having trouble with passing each string individually into the function and spitting it back into main for output to screen. Any help would be greatly appreciated.
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <string>
using namespace std;

int addition( int a, int b );
int multiplication( int a, int b );

int main()
{
	string number1 = "12345678901234567";
	string number2 = "09876543210987654432";
	string NumberNew1[50], NumberNew2[50];
	int x = number1.length()-4, y = 4, a = number2.length()-4;

	if (number1.length()%4 == 0)
	{
		for (int i = 0; i < number1.length()/4; i++)
		{			
			NumberNew1[i] = number1.substr(x, y);
			x -=4;
		}
	}

	else if(number1.length()%4 == 1)
	{
		for (int i = 0; i < number1.length()/4 + 1; i++)
		{			
			NumberNew1[i] = number1.substr(x, y);
			x -=4;
			if (x < 0)
			{
				x = 0;
				y = 1;
			}
		}
	}

	else if(number1.length()%4 == 2)
	{
		for (int i = 0; i < number1.length()/4 + 1; i++)
		{			
			NumberNew1[i] = number1.substr(x, y);
			x -=4;
			if (x < 0)
			{
				x = 0;
				y = 2;
			}
		}
	}

	else if(number1.length()%4 == 3)
	{
		for (int i = 0; i < number1.length()/4 + 1; i++)
		{			
			NumberNew1[i] = number1.substr(x, y);
			x -=4;
			if (x < 0)
			{
				x = 0;
				y = 3;
			}
		}
	}
	if (number2.length()%4 == 0)
	{
		for (int i = 0; i < number2.length()/4; i++)
		{			
			NumberNew2[i] = number2.substr(a, y);
			x -=4;
		}
	}

	else if(number2.length()%4 == 1)
	{
		for (int i = 0; i < number2.length()/4 + 1; i++)
		{			
			NumberNew2[i] = number2.substr(a, y);
			x -=4;
			if (x < 0)
			{
				x = 0;
				y = 1;
			}
		}
	}

	else if(number2.length()%4 == 2)
	{
		for (int i = 0; i < number2.length()/4 + 1; i++)
		{			
			NumberNew2[i] = number2.substr(a, y);
			x -=4;
			if (x < 0)
			{
				x = 0;
				y = 2;
			}
		}
	}

	else if(number2.length()%4 == 3)
	{
		for (int i = 0; i < number2.length()/4 + 1; i++)
		{			
			NumberNew2[i] = number2.substr(a, y);
			x -=4;
			if (x < 0)
			{
				x = 0;
				y = 3;
			}
		}
	}
	//finds longer string
	int longerLength = number1.length();
	if (number1.length() < number2.length())
		longerLength = number2.length();
	//loops number elements into  
	for ( int i = 0; i < longerLength / 4 + 1; i++)
		add(NumberNew1[i], NumberNew2[i]);
}



string add( string num1[], string num2[] )
{
	int i = atoi(num1[]);
	int j = atoi(num2[]);
	int carry = 0;

	i += j;

	if (i > 10000)
	{
		i = i - 10000;
		carry ++;
	}
	numResult[] = i;
}

int multiplication( int a, int b )
{

}

Look at the comments I have added.

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <string>
// Need these header files
#include <sstream>
#include <stdlib.h> // Need this for function atoi()

using namespace std;

int addition( int a, int b );
int multiplication( int a, int b );
// Need to declare the add() function
string add ( string, string );

int main()
{
	string number1 = "12345678901234567";
	string number2 = "09876543210987654432";
	string NumberNew1[50], NumberNew2[50];
	int x = number1.length()-4, y = 4, a = number2.length()-4;

	if (number1.length()%4 == 0)
	{
		for (int i = 0; i < number1.length()/4; i++)
		{
			NumberNew1[i] = number1.substr(x, y);
			x -=4;
		}
	}

	else if(number1.length()%4 == 1)
	{
		for (int i = 0; i < number1.length()/4 + 1; i++)
		{
			NumberNew1[i] = number1.substr(x, y);
			x -=4;
			if (x < 0)
			{
				x = 0;
				y = 1;
			}
		}
	}

	else if(number1.length()%4 == 2)
	{
		for (int i = 0; i < number1.length()/4 + 1; i++)
		{
			NumberNew1[i] = number1.substr(x, y);
			x -=4;
			if (x < 0)
			{
				x = 0;
				y = 2;
			}
		}
	}

	else if(number1.length()%4 == 3)
	{
		for (int i = 0; i < number1.length()/4 + 1; i++)
		{
			NumberNew1[i] = number1.substr(x, y);
			x -=4;
			if (x < 0)
			{
				x = 0;
				y = 3;
			}
		}
	}
	if (number2.length()%4 == 0)
	{
		for (int i = 0; i < number2.length()/4; i++)
		{
			NumberNew2[i] = number2.substr(a, y);
			x -=4;
		}
	}

	else if(number2.length()%4 == 1)
	{
		for (int i = 0; i < number2.length()/4 + 1; i++)
		{
			NumberNew2[i] = number2.substr(a, y);
			x -=4;
			if (x < 0)
			{
				x = 0;
				y = 1;
			}
		}
	}

	else if(number2.length()%4 == 2)
	{
		for (int i = 0; i < number2.length()/4 + 1; i++)
		{
			NumberNew2[i] = number2.substr(a, y);
			x -=4;
			if (x < 0)
			{
				x = 0;
				y = 2;
			}
		}
	}

	else if(number2.length()%4 == 3)
	{
		for (int i = 0; i < number2.length()/4 + 1; i++)
		{
			NumberNew2[i] = number2.substr(a, y);
			x -=4;
			if (x < 0)
			{
				x = 0;
				y = 3;
			}
		}
	}
	//finds longer string
	int longerLength = number1.length();
	if (number1.length() < number2.length())
		longerLength = number2.length();
	//loops number elements into
	for ( int i = 0; i < longerLength / 4 + 1; i++)
    // To cath the return value and output
		cout << add(NumberNew1[i], NumberNew2[i]) << endl;
}


// You are passing a value and not an array
string add( string num1, string num2 )
{
  // atoi() requires that you pass a sequence of characters
	int i = atoi(num1.c_str());
	int j = atoi(num2.c_str());
	int carry = 0;

  // You forgot to declare numResult. I'm assuming
  // this is a string and note that it is not an array
	string numResult;

	i += j;

	if (i > 10000)
	{
		i = i - 10000;
		carry ++;
	}

	//numResult = i;

	// This converts an Int back to string
  stringstream out;
  out << i;
  numResult = out.str();

  // Return the number as a string
	return numResult;
}

int multiplication( int a, int b )
{

}
Thank you for helping, the code is running now. However the resulting output is incorrect. I suspect it has to do with having to carry over a number from the previous arithmetic. I changed the add function slightly to output the result as an int. I can't wrap my head around how I'll add the carry integer onto the next set of elements.

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
int add( string num1, string num2 )
{
  // atoi() requires that you pass a sequence of characters
	int i = atoi(num1.c_str());
	int j = atoi(num2.c_str());
	int carry = 0;

  // You forgot to declare numResult. I'm assuming
  // this is a string and note that it is not an array
	int numResult;

	i += j;

	if (i > 10000)
	{
		i = i - 10000;
		carry ++;
	}

	numResult = i;

	// This converts an Int back to string
  //stringstream out;
  //out << i;
  //numResult = out.str();

  // Return the number as a string
	return numResult;
}

current output:
45711276793234950

wanted output:
9888888889888888999


Topic archived. No new replies allowed.