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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189`` ``````#include "HugeInteger.h" #include 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 #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 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.)

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.