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
|
// Function that receives two arrays of hex digits(first and second hexadecimal numbers), computes the sum, and outputs it to the screen.
void addTwoHexadecimalNumbers(const char addendOne[], const char addendTwo[], char total[], const unsigned int size)
{
char sumDigit, carryDigit = '0', sum_hold, carry_hold; // char variables used to hold the sum digit and carry digit
int i;
for (i = 10; i >= 0; i--)
{
if (carryDigit == '1')
{
carry_hold = carryDigit;
addTwoHexDigits(carry_hold, addendOne[i], sumDigit, carryDigit);;
sum_hold = sumDigit;
addTwoHexDigits(sum_hold, addendTwo[i], sumDigit, carryDigit);
total[i] = sumDigit;
}
else
{
addTwoHexDigits(addendOne[i], addendTwo[i], sumDigit, carryDigit);
total[i] = sumDigit;
}
}
if (carryDigit == '1') // if carry digit is equal to '1', then
{
cout << "The result is longer than 10 digits. Output: ";
for (i = 0; i < size; i++)
{
cout << "*";
}
}
else
{
cout << endl << "The sum of the two hexadecimal numbers is: ";
for (i = 0; i < size; i++)
{
cout << total[i];
}
}
return;
}
|