Big integer calculator for Quotient function with vector

I would like to ask a question. I were coding Big integer calculator for Quotient function with vector in C++. First, in compquient Function.
1
2
3
4
5
6
7
8
9
for (k = quotientSize - 1; k >= 0; k--)
{
    while (lessEqual(buffer, remainder))
    {
        remainder = subtraction(remainder, buffer);
        quotient[k]++;
    }
    divideBy10(buffer);
}

I can't debug and how to fix here! I don't know what is the problem is? The Error message "C++ Libraries out of range!" when i type 4 and calculate 10 divide by 2.
quotient[k]++;

Do i miss something?? Here is the full 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Perform huge integer operations.
#include <iostream>
#include <vector>
#include <string>
using namespace std;

enum sign{ plus, minus };
int enterChoice(); // enable user to input menu choice
void inputTwoHugeInts(vector< int > &hugeInt1, vector< int > &hugeInt2);
bool isZero(vector< int > hugeInt);
bool less(vector< int > leftOperand, vector< int > rightOperand);
bool lessEqual(vector< int > leftOperand, vector< int > rightOperand);
vector< int > addition(vector< int > addend, vector< int > adder);
vector< int > subtraction(vector< int > minuend, vector< int > subtrahend);
vector< int > multiplication(vector< int > multiplicand, vector< int > multiplier);
vector< int > compQuotient(vector< int > dividend, vector< int > divisor);
vector< int > compRemainder(vector< int > dividend, vector< int > divisor);
void divideBy10(vector< int > &leftOperand);
void printArithmetic(vector< int > operand1, vector< int > operand2,
	vector< int > result, char op, sign resultSign);

int main()
{
	vector< int > leftOperand;
	vector< int > rightOperand;
	vector< int > result;

	int choice = enterChoice();

	switch (choice)
	{
	case 4:
		inputTwoHugeInts(leftOperand, rightOperand);
		if (isZero(rightOperand))
		{
			cout << "divide by zero!!\n\n";
			break;
		}
		result = compQuotient(leftOperand, rightOperand);
		printArithmetic(leftOperand, rightOperand, result, '/', sign::plus);
		break;
	default: // invalid value
		cout << "Program should never get here!";
	} // end switch

	system("pause");
}

// enable user to input menu choice
int enterChoice()
{
	// display available options
	cout << "Enter your choice\n"
		<< " 1 - test addition\n"
		<< " 2 - test subtracttion\n"
		<< " 3 - test multiplication\n"
		<< " 4 - test dividsion\n";

	int menuChoice;
	do {
		cout << "? ";
		cin >> menuChoice; // input menu selection from user
	} while ((menuChoice < 1) || (menuChoice > 4));
	cout << endl;

	return menuChoice;
} // end function enterChoice

// enable user to input two positive huge integers from user
void inputTwoHugeInts(vector< int > &hugeInt1, vector< int > &hugeInt2)
{
	string s1;
	string s2;
	cout << "Enter two positive huge integers:\n";
	cin >> s1 >> s2;
	cout << endl;

	unsigned hugeInt1Size = s1.size();
	for (int i = hugeInt1Size - 1; i >= 0; --i)
		hugeInt1.push_back(s1[i] - '0');

	unsigned hugeInt2Size = s2.size();
	for (int i = hugeInt2Size - 1; i >= 0; --i)
		hugeInt2.push_back(s2[i] - '0');
}

bool isZero(vector< int > hugeInt)
{
	unsigned size = hugeInt.size();
	if (size == 0)
		return true;
	else
	{
		for (int i = 0; i < size; ++i)
		if (hugeInt[i] != 0)
			return false;
		return true;
	}
}

vector< int > addition(vector< int > addend, vector< int > adder)
{
	int addendSize = addend.size();
	int adderSize = adder.size();
	int sumSize = (addendSize >= adderSize) ? addendSize + 1 : adderSize + 1;

	vector< int > sum(sumSize);

	for (int i = 0; i < addendSize; ++i)
		sum[i] = addend[i];

	for (int i = 0; i < adderSize; ++i)
		sum[i] += adder[i];

	for (int i = 0; i < sumSize - 1; ++i)
	if (sum[i] > 9)
	{
		sum[i] -= 10;
		++sum[i + 1];
	}

	if (sum[sumSize - 1] == 0)
		sum.resize(sumSize - 1);

	return sum;
}

void divideBy10(vector< int > &leftOperand)
{
	int size = leftOperand.size();
	for (int i = 1; i < size; ++i)
		leftOperand[i - 1] = leftOperand[i];

	leftOperand.resize(size - 1);
}

void printArithmetic(vector< int > leftOperand, vector< int > rightOperand,
	vector< int > result, char op, sign resultSign)
{
	for (int i = leftOperand.size() - 1; i >= 0; --i)
		cout << leftOperand[i];

	cout << " " << op << " ";

	for (int i = rightOperand.size() - 1; i >= 0; --i)
		cout << rightOperand[i];

	cout << " == ";

	if (resultSign == sign::minus)
		cout << '-';

	for (int i = result.size() - 1; i >= 0; --i)
		cout << result[i];
	cout << endl << endl;
}

bool less(vector< int > leftOperand, vector< int > rightOperand)
{
	bool i = 0;
	int j;
	int leftOperandSize = leftOperand.size();
	int rightOperandSize = rightOperand.size();
	if (leftOperandSize < rightOperandSize)
	{
		i = 1;
	}
	if (leftOperandSize == rightOperandSize)
	{
		for (j = leftOperandSize - 1; j >= 0; j--)
		{
			if (leftOperand[j] != rightOperand[j])
			{
				if (leftOperand[j] < rightOperand[j])
				{
					i = 1;
				}
				break;
			}
		}
	}
	return i;
}

bool lessEqual(vector< int > leftOperand, vector< int > rightOperand)
{
	bool i = 1;
	int j;
	int leftOperandSize = leftOperand.size();
	int rightOperandSize = rightOperand.size();
	if (leftOperandSize > rightOperandSize)
	{
		i = 0;
	}
	if (leftOperandSize == rightOperandSize)
	{
		for (j = leftOperandSize - 1; j >= 0; j--)
		{
			if (leftOperand[j] != rightOperand[j])
			{
				if (leftOperand[j] > rightOperand[j])
				{
					i = 0;
				}
				break;
			}
		}
	}
	return i;
}

vector< int > subtraction(vector< int > minuend, vector< int > subtrahend)/////////////////////////////////
{
	int minuendSize = minuend.size();
	int subtrahendSize = subtrahend.size();
	int differenceSize = (minuendSize >= subtrahendSize) ? minuendSize + 1 : subtrahendSize + 1;

	vector< int > difference(differenceSize);

	for (int i = 0; i < minuendSize; ++i)
		difference[i] = minuend[i];

	for (int i = 0; i < subtrahendSize; ++i)
		difference[i] -= subtrahend[i];

	for (int i = 0; i < differenceSize - 1; ++i)
	if (difference[i] < 0)
	{
		difference[i] += 10;
		difference[i + 1]--;
	}

	while (differenceSize > 1)
	{
		if (difference[differenceSize - 1] == 0)
		{
			difference.resize(differenceSize - 1);
			differenceSize--;
		}
		else
		{
			break;
		}
	}

	return difference;
}

vector< int > compQuotient(vector< int > dividend, vector< int > divisor)
{

	int	k;

	int dividendSize = dividend.size();
	int divisorSize = divisor.size();

	int bufferSize = dividendSize;
	vector< int > buffer(bufferSize);

	int quotientSize = dividendSize - divisorSize + 1;
	vector< int > quotient(quotientSize);

	int remainderSize = dividendSize;
	vector< int > remainder(remainderSize);

	for (int i = 0; i < dividendSize - 1; i++)
	{
		remainder[i] = dividend[i];
	}

	for (int i = divisorSize - 1; i >= 0; i--)
	{
		buffer[dividendSize - divisorSize + i] = divisor[i];
	}

	if (::less(dividend, buffer))
	{
		divideBy10(buffer);
		quotient.resize(quotientSize - 1);
	}

	for (k = quotientSize - 1; k >= 0; k--)
	{
		while (lessEqual(buffer, remainder))
		{
			remainder = subtraction(remainder, buffer);
			quotient[k]++;
		}
		divideBy10(buffer);
	}
	return quotient;
}





vector< int > compRemainder(vector< int > dividend, vector< int > divisor)
{

	int	k;

	int dividendSize = dividend.size();
	int divisorSize = divisor.size();

	int bufferSize = dividendSize;
	vector< int > buffer(bufferSize);

	int quotientSize = dividendSize - divisorSize + 1;
	vector< int > quotient(quotientSize);

	int remainderSize = dividendSize;
	vector< int > remainder(remainderSize);

	for (int i = 0; i < dividendSize - 1; i++)
	{
		remainder[i] = dividend[i];
	}

	for (int i = divisorSize - 1; i >= 0; i--)
	{
		buffer[dividendSize - divisorSize + i] = divisor[i];
	}

	if (::less(dividend, buffer))
	{
		divideBy10(buffer);
		quotient.resize(quotientSize - 1);
	}

	for (k = quotientSize - 1; k >= 0; k--)
	{
		while (lessEqual(buffer, remainder))
		{
			remainder = subtraction(remainder, buffer);
			quotient[k]++;
		}
		divideBy10(buffer);
	}
	return remainder;
}
Last edited on
closed account (48T7M4Gy)
What I suggest you do is adopt a form of unit testing. Instead of wading through 341 lines of stuff and menus, extract the relevant bits and produce a new small main with test data.

Running convoluted and irrelevant code is a recipe for a long day and night of fault finding. This way will take 5 mins to set up.

Oh, and include line numbers with the error messages you get please. :)

Enter your choice
 1 - test addition
 2 - test subtracttion
 3 - test multiplication
 4 - test dividsion
? 2

Program should never get here! 
Exit code: 0 (normal program termination)


Enter your choice
 1 - test addition
 2 - test subtracttion
 3 - test multiplication
 4 - test dividsion
? 4

Enter two positive huge integers:
234
56

 
Exit code: 0 (normal program termination)

Last edited on
I can't debug and how to fix here!


Hi,

Why can't you debug? One should always be able to do this - the world will come to an end when we can't debug :+)

Are you using an IDE? What specifically is wrong with the debugger? Or what problem are you having with it? Is the Watch List window showing?

Sorry for the rapid fire questions, Just trying to sort out what is wrong :+)

As kemort says, always show the entire error verbatim.
Topic archived. No new replies allowed.