Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Solved

Posted on 2009-03-30

i've been given a programming assignment to create a class called hugeint that can take up to 40 digit integers and do arithmatic operations on them.I'm having considerable difficulty with the problem and was hoping for a bit of help. My biggest issue right now is with the multiply operator. ill post what I have for the code so far, but i have to keep deleting and changing stuff as it does not work for large integers! Hope someone can point me in the right direction!

```
HugeInt HugeInt::operator *(HugeInt& rhs)
{
HugeInt temp;
int x(0);
int y(0);
int z(0);
int f(0);
int digitCount(1);
int timesAdded(1);
int amountOverTen(0);
char a;
char b;
char c;
char parsedChar;
long long addition(0);
long long parsedInt;
long long longInt;
int parsedRhsVector(0);
int parsedInputVector(0);
int i(0);
vector<long long> hugeVector;
vector<int> rhsVector;
vector<int> inputVector;
vector<long long> multiplyVector;
vector<int> otherMultiplyVector;
vector<int>::iterator inputCounter;
vector<int>::iterator rhsCounter;
vector<long long>::iterator multiplyIterator;
char Input [40];
char rhsInput[40];
for(i=40;i>=0;i--)
{
Input[i]=digits[i];
a=(Input[i]);
x=a-'0';
rhsInput[i]=rhs.digits[i];
b=rhsInput[i];
y=b-'0';
//z=x*y;
if(y>=0)
{
rhsVector.push_back(y);
}
if(x>=0)
{
inputVector.push_back(x);
}
}
rhsCounter=rhsVector.begin();
while(rhsCounter< rhsVector.end())
{
for(inputCounter=inputVector.begin(); inputCounter < inputVector.end(); inputCounter++)
{
parsedInputVector=*inputCounter;
parsedRhsVector=*rhsCounter;
z= (parsedInputVector * parsedRhsVector)+amountOverTen;
amountOverTen=0;
while(z>=10)
{
z=z-10;
amountOverTen++;
}
stream<< z;
if(inputCounter+1>= inputVector.end())
{
z=amountOverTen;
stream<<z;
}
}
amountOverTen=0;
inputCounter=inputVector.begin();
rhsCounter++;
}
for(multiplyIterator=multiplyVector.begin();multiplyIterator<multiplyVector.end();multiplyIterator++)
{
longInt=*multiplyIterator;
addition=addition+longInt;
}
stringstream stream;
string additionString;
stream<<addition;
additionString= stream.str();
for(i=0;i<additionString.length();i++)
{
parsedChar=additionString[i];
hugeVector.push_back(parsedChar);
}
vector<long long>:: iterator it;
i=0;
for ( it=hugeVector.begin() ; it < hugeVector.end(); it++ )
{
parsedInt=*it;
c=parsedInt;
temp.digits[i]=c;
i=i+1;
}
i=0;
for(i=0;i<40;i++)
{
if(temp.digits[i]<=0)
{
temp.digits[i]='\0';
}
}
return temp;
}
```

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

- Help others & share knowledge
- Earn cash & points
- Learn & ask questions

1 Comment

You should remember your basic school algorithms for multiplying numbers

123 * 98 =

1107

984

----------------

12054

and you will see that it would work with any count of digits.

So, you have to iterate on the digits of second HugeInt and multiply the first HugeInt operand by any digit number. You best add another operator* which takes a char and simply do

hugesums[i] = hugeop1 * hugeop1.getDigitAt(i);

where i is the current loop counter. Make i 0-based. The getDigitAt would return a char. e. g. '9' and I would count from the right, so that for i=0 you get the least significant digit.

To store the digits in HugeInt, don't use an array. A simple string member will do it.

hugesums in the above sample would be a

vector<HugeInt> hugesums(40, HugeInt(0));

With that the hugesums already is fully allocated and filled with huge integers of 0 value (the internal string is "0").

the operator*(HugeInt huge, char dig)

then would do a multiplication of dig with each digit of the huge operand, add the previous carry and add the remainder of mod10 to the result string. Then, save the new carry.

The total result is a string which could be turned to a HugeInt and be returned by value.

make another helper, which adds zero digits to the right of an HugeInt. You need it to multiply your partial sums by an arbitrary power of 10.

Finally use operator+ to calculate the total sum of the partial sums (hugesum) in aloop and the final result is waht you need as result of operator*.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

Course of the Month8 days, 11 hours left to enroll

Join the community of 500,000 technology professionals and ask your questions.