Please help me understand how to solve this problem. I am totally lost and don't even know where to start.
Problem:
Create a class HugeInteger that uses a 40-element array of digits to store integers as large as 40 digits each. Provide member functions input, output, add and subtract. For comparing HugeInteger objects, provide functions isEqualTo, isNotEqualTo, isGreaterThan, isLessThan, isGreaterThanOrEqualTo and isLessThanOrEqualTo --- each of these is a "predicate" function that simply return true if the relationship is holds between the two HugeIntgers and returns false if the relationship does not hold. Also, provide a predicate function isZero. If you feel ambitious, provide member functions multiply, divide, and modulus.
RULES: Must use the .h file, unmodified, below. Provided .h file:
So my approach was to build the array integer by doing the following by building a for loop which cycles backward through an long int. I created two variables which, one would take the modulus of the long int and assign that number to array integer[i]. This gives me the last number off the long int. Next, I take the long int and /= 10 which knocks off the last digit. The for loop continues until the array is populated.
This is great. However, I dont't know how to implement this into the structure the teacher has set up for this assignment. I've tried to populate the private member array integer this way, however, I come up empty.
I'm totally blow away with the two constructors and how to use them to do this problem.
Here's how I tried to approach the problem by breaking down the long into individual elements in an array. I used a for loop and here is that for loop.
long longInt = 123456789; // value to put into array of shorts
array < short, 9 > integer = { }; // array of shorts
int theInt; // number to put into short element
for(int i = 8; i >=0; i--)
{
theInt = longInt % 10; //gets last digit off longInt
longInt /=10; //knocks last digit off longInt
integer[i] = theInt;// puts value of theInt into array of shorts
}
So now I have an array of shorts. I can convert the into chars and have a c-string, which is what I think they .h file is suggesting that I do, but I'm not sure how this solves the problem.
ok so you're righting an implementation for the first constructor - i.e. it populates itself from an int
The second constructor you need to be able to accept a string and end up with the same array you created from the first constructor (assuming you pass in the same number)
Once you've got these two constructors then think about how you can add them together, by iterating through BOTH arrays of ints and adding and carrying values as if you were doing it on paper.
Add together as in create two HugeInteger instances each populated using the constructors and then you need to call the add operator on one of them
HugeInteger hugeInteger1 = ... HugeInteger("12345"); // one constructed from a string HugeInteger hugeInteger2 = ... HugeInteger(new Long(98765)); // one constructed from an long
No the first constructor accepts a long but defaults to 0 if none provided The second constructor accepts a string.
So your first section of code would be the implementation of the first constructor. After calling new HugeInteger( with a long) it would have number1 as you've called it which is a HugeInteger with the internal array populated from the long that's you passed in.
The principle isn't really that hard, you just need to add numbers from the least significant to the most, while detecting and adding a carry to the following number. Subtraction is the same but reversed, you can check if your subtraction is less or equal to zero. Comparison can be done with checking of the length first (the number of digits), if they are equal then check every number from the most significant to the least, if you find any one is bigger - your job is done, you can stop. As for the array I suggest converting numbers to an array of characters, then subtracting 48 from each and every one and keeping them that way. 48 is 0 in ASCII, well you get the idea I hope, just turn them from ASCII to actual values, it will make your math simple.
Here's how I tried to approach the problem by breaking down the long into individual elements in an array. I used a for loop and here is that for loop.
long longInt = 123456789; // value to put into array of shorts
array < short, 9 > integer = { }; // array of shorts
int theInt; // number to put into short element
for(int i = 8; i >=0; i--)
{
theInt = longInt % 10; //gets last digit off longInt
longInt /=10; //knocks last digit off longInt
integer[i] = theInt;// puts value of theInt into array of shorts
}
So now I have an array of shorts. I can convert the into chars and have a c-string, which is what I think they .h file is suggesting that I do, but I'm not sure how this solves the problem.
Addition Algorithm: (assuming I've created two arrays each 9 elements long from two HugeInteger Objects):
And can you prove that it does what you expect it to do?
Can you instantiate a HugeInteger using this constructor (using say 12345) and prove that the array contains [1] [2] [3] [4] [5] in your main class?
You might need to implement your output method so that you can get the value stored in the array out of the class. As it stands the array only exists as a private variable inside the class so nothing outside the class can get to it
cout << hugeInteger1.output() - not sure how you would pass it back though
One thing that looks strange is that a copy constructor (the second one) is usually where you pass in the same object type - in this case a HugeInteger. The constructor commented as a copy constructor in the original h file accepts a string - like the input method().
It compiles and runs properly. Each case is tested in the tester.cpp file.
Now, I just have to do this part of the exerciser : "For comparing HugeInteger objects, provide functions isEqualTo, isNotEqualTo, isGreaterThan, isLessThan, isGreaterThanOrEqualTo and isLessThanOrEqualTo --- each of these is a "predicate" function that simply return true if the relationship is holds between the two HugeIntgers and returns false if the relationship does not hold."