I have to write a program to perform addition of two hexadecimal number each with up to 10 Digits. If the result of the addition is more than 10 Digits long, then it needs to output "Addition Overflow".
Hi All,
After you do that(i mean after converting them to int) take sum of them and then again convert it to hex, now, to know is the number more than 10 digits.
you can cast it to string and then check that what is the length of string , But there is more easy ways ,But now i have headache and can not display them.
Have Nice Life.
Just a quick comment on style and approach. Function convertToDec is misnamed, as it converts from a hex string to an integer. The integer isn't a decimal value (internally the computer works in binary).
Within the function, the pow() function is using a sledgehammer to crack a walnut. Bear in mind that internally the function will usually calculate the logarithm, multiply by the power, then calculate the antilogarithm (with possible rounding errors). That's an awful lot of work when you could achieve the same goal with a single multiplication by 16 on each pass through the loop.
The rest of the code as has already been said, will need to add the two integers (one line of code) and then convert back to hex afterwards - with a check for overflow beyond 10 hex digits.
(I was going to point out that the integer may be up to 41 bits long, but you've already got that covered with the use of long long int).
Edit - just noticed this : cin >> a[SIZE];
That looks horribly wrong, first it is reading a single character rather than a string, and secondly it is storing the result outside the boundaries of the array.
Valid subscripts are in the range 0 to SIZE-1. a[SIZE] is one position past the end of the array.
Also if the user enters an invalid hex character, at line 61 there is hex[i] = -1; which looks like it silently ignores the bad data. I think it should either stop processing the string at that point, and/or output an error message. Perhaps the strings should first be validated and the user prompted to try again, before proceeding with the conversion to an integer.
Well, i wrote quite a lot in my previous post. A one-line response doesn't help us to help you. Perhaps you could explain specifically what you need help with.
- Currently I need help with how to add these numbers after converting to int and then checking if it is 10 digits long to Output the warning.
Edit:
This hint was given to us, but I still can't put my hands on it.
1 2 3 4 5 6 7 8 9
Add2 numbers and deter
mine the remainder. Remainder exists and is carried
over if number exceeds 15, or F.
18 = x + y; // x and y values from above example
18 % 16 = 2;
18 / 16 = 1; // 1 is our remainder
12 = (1 * 10) + 2
This method converts 18 in decimal to 12 in
hexadecimal
Like hexToInt , but the reverse.
Here's a sample implemention.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
bool intToHex(char *outstr,longlong in)//outstr must be atleast 11 bytes (or 'char's long)
{
outstr[10]=0;//null terminator
constchar ciphers[] = "0123456789abcdef";
for(int i=SIZE-1;i>=0;--i)
{ //here, we convert using the long division method.
outstr[i]=ciphers[in%16];//<- Add the remainder
in/=16; //<- set 'in' to the quotient
}
if(in)
returntrue;//overflow
elsereturnfalse;
}
4. Output.
Hope that helps.
P.S. Your hexToDec() function can be revamped (or reimplemented a little).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
longlongint hexToInt(char a[],int size)
{
//assert(size <= SIZE)
constchar ciphers[] = "0123456789abcdef";
longlong sum = 0,m=1;
for(int i = size-1 ; i>= 0 ; --i)
{
/*
strchr() returns the pointer to the first occurrence of a[i].
We subtract that pointer from the base pointer to get the index and thus the value of the given cipher.
*/
int hexdigit = strchr(ciphers,a[i])-ciphers;
sum += hexdigit*m;
m*=16;
}
return sum;
}