Arrays

I am starting to work with arrays. I was given this problem. adding 20 digit arrays.

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
167
#include <iostream>
#include <string>
using namespace std;

void convert();
void addnum();
void print();
void printSum();
void addarrays();

int first[20] = { 0 };
int second[20] = { 0 };
int sum[21] = { 0 };
int lengthF;
int tempLenF;

int main()
{
	convert();
	addarrays();
	//print();
	printSum();
}

void convert()
{
	string num1;
	string num2;


	std::cout << "Please enter the first positive integer (20 digits at the most): " << endl;
	cin >> num1;

	if (num1.size() > 20)
	{
		cout << "Your First number has more than 20 digits. Pleases Try Again.";
		exit(0);
	}

	std::cout << "Please enter the second positive integer (20 digits at the most): " << endl;
	cin >> num2;

	if (num2.size()>20)
	{
		cout << "Your First number has more than 20 digits. Pleases Try Again.";
		exit(0);
	}

	int count = 0;

	for (int i = (int)num1.length() - 1; i >= 0; i--)
	{
		first[count] = num1[i] - '0';
		count++;
	}

	int count2 = 0;

	for (int i = (int)num2.length() - 1; i >= 0; i--)
	{
		second[count2] = num2[i] - '0';
		count2++;
	}


	if (num1.size()>num2.size())
		lengthF = num1.size();
	else if (num2.size()>num1.size())
		lengthF = num2.size();

}

void addarrays()
{
	int carryover = 0, temp = 0;

	for (int i = 0; i <= 20; i++)
	{
		temp = carryover + first[i] + second[i];

		if (temp > 10)
		{
			carryover = 1;
		


/*
void addnum()
{
//addition is the number carried on
int addition = 0;
int temp;
int i;
int sumcount = 20;
for (i = 0; i<lengthF; i++)


//Temp is starting from the right of the number and calulates the simple addition of 10
temp = first[i] + second[i] + addition;
if (temp>10)
{
addition = 1;
temp = temp - 10;
sum[sumcount--] = temp;
}
else
{
addition = 0;
sum[sumcount--] = temp;
}
if (i == lengthF - 1)
{
sum[sumcount] = addition;
}
}
*/	temp -= 10;
		}

		sum[i] = temp;
	}

}


void print()
{

	int counterSum = 0;
	for (int i = 0; i < 21; i++)
	{
		if (sum[i] != 0)
		{
			counterSum = i;
			break;
		}
	}

	cout << "Sum = ";

	for (int i = counterSum; i < 21; i++)

	{
		cout << sum[i];
	}

	cout << endl;
}

void printSum()
{
	bool ignorezeros = true;


	for (int i = 21; i >= 0; i--)
	{

		if (sum[i] == 0 && ignorezeros)
		{

		}
		else
		{
			ignorezeros = false;
			cout << sum[i];
		}
	}
}
Last edited on
I see no question.
Oh i meant to say that. It isnt out putting right. and i need help finding out the problem. Ex i enter 123 and 123 it should output 246 but it outputs 900000000000000246
Sum has 21 elements. First value that you do look at in printSum is sum[21]. That is an out of range error.
Topic archived. No new replies allowed.