Comparison of classes

I'm having a lot of trouble with writing a class function that compares two big integer arrays. Someone please help me.

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
#include "HugeInteger.h"
#include <iostream>

using namespace std;

	HugeInteger::HugeInteger(void)
	{
		for (int i = MAXINT - 1; i >= 0; i--)
			this->myArray[i] = 0;
	}

	HugeInteger::~HugeInteger()
	{
	}

	void HugeInteger::input(int newArray[MAXINT])
	{
		for (int index = MAXINT - 1; index >= 0; index--)
			this->myArray[index] = newArray[index];
	}

	void HugeInteger::output()
	{
		for (int index = 0; index < MAXINT; index++)
			cout << this->myArray[index];
	}

	HugeInteger HugeInteger::add(HugeInteger other)
	{
		HugeInteger result;
		for (int i = 0; i < MAXINT; i++)
		{
			result.myArray[i] = this->myArray[i] + other.myArray[i];
		}

		return result;
	}

	HugeInteger HugeInteger::subtract(HugeInteger other)
	{
		HugeInteger result;
		for (int i = 0; i < MAXINT; i++)
		{
			result.myArray[i] = this->myArray[i] - other.myArray[i];
		}

		return result;
	}

	bool HugeInteger::testZero(void)
	{
		bool result = true;
		for (int index = MAXINT - 1; index >= 0; index-- )
			if (this->myArray[index] != 0)
				result = false;
			return result;
	}
__________________________________________
NEXT .CCP

#include <iostream>
#include "HugeInteger.h"
using namespace std;

int main()
{
	int first[MAXINT] = { 1, 2, 4, 5, 7, 8, 9, 6, 5, 6, 3, 2, 5, 4, 1, 5, 6, 9, 8, 7, 4, 5, 1, 2, 0, 3, 6, 0, 2, 5, 6, 9, 7, 4, 1, 2, 5, 9, 6, 0 };
	int second[MAXINT] = { 2, 3, 5, 0, 4, 6, 9, 8, 7, 5, 6, 3, 0, 1, 5, 4, 7, 2, 9, 8, 3, 7, 1, 5, 3, 4, 2, 6, 0, 1, 4, 5, 7, 9, 8, 6, 5, 1, 3, 0 };
	int zero[MAXINT] = { 0 };

	HugeInteger myHugeInteger1;
	HugeInteger myHugeInteger2;
	HugeInteger myHugeInteger0;
	
	// Add Objects
	HugeInteger myHugeInteger4;
	HugeInteger sum2;

	// Subtract Objects
	HugeInteger myHugeInteger5;
	HugeInteger difference2;

	myHugeInteger1.input(first);
	myHugeInteger2.input(second);
	myHugeInteger0.input(zero);

	cout << boolalpha << endl;

	cout << "This will test if all the elements in myHugeInteger1,\n";
	myHugeInteger1.output();
	cout << ", are equal to zero: " << endl;
	cout << myHugeInteger1.testZero() << endl << endl;

	cout << "\n\nThis will test if all the elements in myHugeInteger2,\n";
	myHugeInteger2.output();
	cout << ", are equal to zero: " << endl;
	cout << myHugeInteger2.testZero() << endl << endl;

	cout << "\n\nThe will test if all the elements in myHugeInteger0,\n";
	myHugeInteger0.output();
	cout << ", are equal to zero: " << endl;
	cout << myHugeInteger0.testZero() << endl << endl;

	// Add test case 1
	myHugeInteger4 = myHugeInteger0.add(myHugeInteger2);
	cout << "\nThe sum of\t";
	myHugeInteger0.output();
	cout << endl << " plus\t\t";
	myHugeInteger2.output();
	cout << endl << " equals\t\t";
	myHugeInteger4.output();
	cout << endl;
	
	// Add test case 2
	sum2 = myHugeInteger1.add(myHugeInteger2);
	cout << "\nThe sum of \t";
	myHugeInteger1.output();
	cout << endl << " plus\t\t";
	myHugeInteger2.output();
	cout << endl << " equals\t\t";
	sum2.output();
	cout << endl;

	// Subtract test case 1
	myHugeInteger5 = myHugeInteger0.subtract(myHugeInteger2);
	cout << "\nThe difference of \t";
	myHugeInteger0.output();
	cout << endl << " minus\t\t";
	myHugeInteger2.output();
	cout << endl << " equals\t\t";
	myHugeInteger5.output();
	cout << endl;

	// Subtract test case 2
	difference2 = myHugeInteger1.subtract(myHugeInteger2);
	cout << "\nThe difference of \t";
	myHugeInteger1.output();
	cout << endl << " minus\t\t";
	myHugeInteger2.output();
	cout << endl << " equals\t\t";
	difference2.output();
	cout << endl;

	/*

	//THIS IS WHAT I NEED

	// Equal to test case
	cout << "\nThis will test if all the elements in the first array,\t";
	myHugeInteger1.output();
	cout << endl << " are equal to all elements in the second array: \t\t << endl;
	myHugeInteger2.output();
	cout << endl << myHugeInteger1.testEqualTo() << endl << endl;
	
	*/
}
________________________________________
HEADER FILE

#ifndef HUGEINTEGER_H
#define HUGEINTEGER_H

#include <iostream>
using namespace std;

static const int MAXINT = 40;

//HungeInteger Class
class HugeInteger
{
	public:
		HugeInteger(void);
		~HugeInteger(void);

		void HugeInteger::input(int[MAXINT]);
		void HugeInteger::output(void);
		
		HugeInteger HugeInteger::add(HugeInteger other);
		HugeInteger HugeInteger::subtract(HugeInteger other);
		
		bool testZero(void);
		bool testEqualTo(void);

	private:
		int myArray[MAXINT];

};
#endif


Thanks all.
Last edited on
You already test whether each element equals zero. You need to test whether each element equals corresponding element in the other array.


However, what is the "HugeInteger" supposed to do? The name hints of something, but your existing methods do not.

Lets pretend that each element can hold only one digit. So lets add up (pseudo):
{ 1, 6 } + { 4, 7 }
Your lines 28-37 do it like this:
result.myArray[0] = 1 + 4
result.myArray[1] = 6 + 7

Fine?
result == { 5, 13 }
No, we accept only 1-digit values.  What to do? Discard overflow? { 5, 3 }
Somehow, 16 + 47 should not be 53.

(I might have misguessed the goal of the class.)
Some comments on your code:

You might want to consider storing the digits so that myArray[0] is the least significant digit. As you have it, myArray[0] is the most significant digit. This makes it difficult to do calculations since the HugeIntegers may have different numbers of digits. By storing least significant digit in myArray[0], the myArray index is the power of 10. This simplifies carrying and borrowing.

Line 28,39: You should pass other as a const reference. No reason to copy the entire object onto the stack.

Line 33: As keskiverto pointed out, you have a carry issue if the result is > 9. You need to store the remainder and carry. Don't forget that carry can in itself cause another carry, and another, etc.

Line 44: Likewise, you have a borrow issue here. What if the result of subtracting two digits is < 0?

Line 55: If you find a non-zero element, you should return false immediately. No need to iterate through the rest of the array.

Lines 175-179: Declarations don't use the class name. Most compilers will complain about this.

What happens if adding two most significant digits overflows?

What happens if subtracting a larger number from a smaller number? I see no provision for a sign.
Last edited on
Topic archived. No new replies allowed.