Hello, I'm fairly new to programming and taking the introductory course for C++, and I'm having problems with a particular problem.
The problem is that I need to add two hexadecimals together, which i store in two arrays (a[] and b[]) of type char up to size of 10, and output the result of their addition as another array of type char(called c[]). If the addition of arrays results in a hexadecimal greater than 10 digits, I'm asked to output "Addition Overflow" and do nothing more.
My problem is inside the function I wrote that adds both arrays. Prior to this function call, I wrote a function that fills up the array with input from the user, and after that I filled the remaining elements of the array with '\0' (empty).
Before I post the code, here is the algorithm I designed for the function that adds the hexadecimals:
1. declare Initialize a variable called sum to 0, which is the result of adding the characters at index 'i' of arrays a[] and b[].
2. Use the isdigit and isalpha functions to check whether the character at index i (for both arrays) is a digit or a letter of the alphabet. Depending on which case it is, I use a subtraction operation to get the numerical value of the character and store that in sum. I tested this function separately in a driver program and it seems to have worked fine, so I don't believe this is the problem.
3. Once I have the values of sum, I used if-else statements to check their numerical value and assign the correct character belonging to it, to the array c at index j, which was declared and initialized to 0 before the loop started, and is incremented by one at the end of the loop.
If the number happens to be greater than 15, and less than 32, then I wrote code to subtract 16 from the total of sum, and repeat the same if-else statements to assign the character values to c[j].
The reason I do that separately is because if there is a value greater than 15, there is a "carry over" in the addition, so next time the loop iterates, sum will start at "1" instead of 0.
4. The loop will end when either the maximum size has been reached, or when both array values at index i are '\0' (in which case it will break).
My problem: When I compile and run the program, the output for the addition seems to be garbage values each time. I'm not entirely sure as to why that is, other than my biggest lead being something wrong in the loop specifically (as i have tested every other function in the program separately and they seemed to have worked fine.)
If anybody could give me some tips, advice, or point out the issue, I would be incredibly thankful. I just want to be pointed in the right direction so I can solve this problem.
Thanks!
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
void hexadecimal_addition(char a[], char b[], char c[], int size)
{
int sum = 0;
int j = 0;
for(int i = size - 1, j = 0; (i >= 0); i--)
{
if(a[i] == '\0' && b[i] == '\0')
break;
if(isdigit(a[i]))
sum += (a[i] - '0');
if(isalpha(a[i]))
sum += (a[i] - 'A') + 10;
if(a[i] == '\0')
sum += 0;
if(isdigit(b[i]))
sum += (b[i] - '0');
if(isalpha(b[i]))
sum += (b[i] - 'A') + 10;
if(b[i] == '\0')
sum += 0;
//now the array will be filled
if(sum < 16)
{
if(sum == 0)
c[j] = '0';
if(sum == 1)
c[j] = '1';
if(sum == 2)
c[j] = '2';
if(sum == 3)
c[j] = '3';
if(sum == 4)
c[j] = '4';
if(sum == 5)
c[j] = '5';
if(sum == 6)
c[j] = '6';
if(sum == 7)
c[j] = '7';
if(sum == 8)
c[j] = '8';
if(sum == 9)
c[j] = '9';
if(sum == 10)
c[j] = 'A';
if(sum == 11)
c[j] = 'B';
if(sum == 12)
c[j] = 'C';
if(sum == 13)
c[j] = 'D';
if(sum == 14)
c[j] = 'E';
if(sum == 15)
c[j] = 'F';
sum = 0;
}
else if(15 < sum && sum < 32)
{
sum -= 16;
if(sum == 0)
c[j] = '0';
if(sum == 1)
c[j] = '1';
if(sum == 2)
c[j] = '2';
if(sum == 3)
c[j] = '3';
if(sum == 4)
c[j] = '4';
if(sum == 5)
c[j] = '5';
if(sum == 6)
c[j] = '6';
if(sum == 7)
c[j] = '7';
if(sum == 8)
c[j] = '8';
if(sum == 9)
c[j] = '9';
if(sum == 10)
c[j] = 'A';
if(sum == 11)
c[j] = 'B';
if(sum == 12)
c[j] = 'C';
if(sum == 13)
c[j] = 'D';
if(sum == 14)
c[j] = 'E';
if(sum == 15)
c[j] = 'F';
sum = 1;
}
else // somehow got a wrong number in sum
cout << "Error in sum\n";
j++;
}
cout << "Hexadecimal result: ";
for(int i = 0; i < size; i++)
cout << c[i];
cout << endl;
if(sum == 1)
{
cout << "Addition Overflow\n";
exit(1);
}
return;
}
|
I know the code is a little messy, but I'm still fairly new and only using the tools that we've seen in class so far. But if any of you have suggestions on how to make the code nicer, feel free to point it out as well, if you want to.