Using Arrays and learning Hexadecimal adder

I am to make a hexadecimal adder here is my question:

I need help writing a hexadecimal adder. This is the question: Hexadecimal numerals are (integers written in base 16.) The 16 digits used are '0' through '9' plus 'a' for the "digit 10", 'b' for the "digit 11", 'c' for the "digit 12", 'd' for the "digit 13", 'e' for the "digit 14", and 'f ' for the "digit 15". For example, the headecimal numeral d is the asme as base 10 numeral 13 and the dexadecimal numeral 1d is the same as the base 10 numeral 29. Write a C++ program to perform addition of two hexadecimal numerals each with up to 10 digits. If the result of the addition is more than 10 digits long, then simply give the output message "Addition Overflow" and not the result of the addition. If the digits that you are adding is greater than the 'f' dont do the addition Use arrays to store hexadecimal numerals as arrays of characters. Include a loop to repeat this calculation for new numbers until the user says she or he wants to end the program. I don't understand how use use arrays to if you can please heeeelp. the arrays that we learned so far is like this example:

int a[] {0,0,0,0,0,0,0}

But the intstructor said that we need to use a function or more and you have to inialize the size of the array with a global named constant please help.
What he meant was
1
2
const int NUM_LENGTH = 10;
char MyHexNumber[NUM_LENGTH] = {'0', '0', '0', '0', '0', '0', '0', 'f', '7', '3'};

And there should also be a function that adds two such hex arrays.
I suggest you first convert hex numbers '0' to 'f' to dec numbers 0 to 15, then add each element with its equivalent in the other array
MyHexResult[i] = HexNum1[i] + HexNum2[i];
Then iterate through the loop and if some value is greater than 'f' then add one to the next element
1
2
MyHexResult[i+1] += MyHexResult[i]/16;
MyHexResult[i] %= 16;


Good luck
Last edited on
Im not the greatest one a for loop can you show me an example of a for loop that would be in one of those functions. So that way I would kinda have something to look at and see how you put it together. I know how to use functions i just found arrays a little hard right now its nice to know how to do some of it so i can learn. Besides the books aren't the greatest at teaching. They just seem to jump back and forth. If you can please help i would greatly thank you.
All of you for loops in this program should look like this:
1
2
3
for(int i = 0; i < NUM_LENGTH; i++){
    //do your stuff
}

and heres how the function should look if you do it my way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Add(char hex1[], char hex2[]){
    char hex_result[NUM_LENGTH];
    loop{
        convert each element of hex1 to int from 0 to 15
        and do the same with hex2
    }
    loop{
        do the first line I posted before.
    }
    loop{
        do the second piece of code I posted
    }
    loop{
        convert each element of hex_result to a char form '0' to 'f'
    }
}
Topic archived. No new replies allowed.