I am working on a programming assignment for class and having a little trouble. The biggest trouble I am having is working with arrays and doing the hex calculations, then making the conversion back to decimal.

I have included the assignment description below:

Write a C++ program to perform addtion of two hexadecimal numerals each with up to 10 digits. If the result of the hexadecimal numerals is more than 10 digits long, then simply give the output message "Addition Overflow" and not the result of 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 want's to end the program.

Thanks for any help!

I am working on a programming assignment for class and having a little trouble. The biggest trouble I am having is working with arrays and doing the hex calculations, then making the conversion back to decimal.

I have included the assignment description below:

Write a C++ program to perform addtion of two hexadecimal numerals each with up to 10 digits. If the result of the hexadecimal numerals is more than 10 digits long, then simply give the output message "Addition Overflow" and not the result of 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 want's to end the program.

Thanks for any help!

Here are some of the things you need to do:

1. Allocate array storage for three hexadecimal numbers (first addend, second addend, sum).

2. Prompt for two numbers as input, or exit.

3. Data validation? Do you need to check the addends to make sure they are "legal" - 10 or fewer characters, 'A' - 'F' , '0' - '9' ?

4. Add them together, including carrying and checking whether there is overflow.

5. Display the sum as a hexadecimal.

6. Convert from hexadecimal to decimal.

7. Display the sum as a decimal.

You can make use of string streams to do the conversion:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
	stringstream ss;
	int integer;
	string s;
	
	integer = 500;
	
	/* Convert an integer to a hexadecimal string */
	hex(ss);
	ss << integer;
	ss >> s;
	
	cout << "Integer: " << integer << endl;
	cout << "Hexadeximal: " << s << endl << endl;
	
	/* Convert a hexadecimal string to an integer */
	dec(ss);
	ss << s;
	ss >> integer;
	
	cout << "Hexadeximal: " << s << endl;
	cout << "Integer: " << integer << endl;

	return 0;
}

It's only an example and you'll have to adjust it to your needs ...
To make the sum of two hexadecimals you could do the following:
> Convert them both to decimal base ...
> Add those two integers ...
> Convert them back to hexadecimal base ...

> If you want to know if there was a so-called 'overflow' you only have to save the result in a string, and use the length() -method to check the number of characters, if it's higher than 10 you print out a message there was an 'OVERFLOW' ...

> You can use the width manipulator to set the output width (= of the result of the addition) to 10 characters ...

Use Google to find some information about stringstreams ...

Hope this helps !

Oh, sorry I made a mistake on line 24: you have to replace dec(ss); by ss.clear(); ...

Thanks for the help,

I am most confused about the actual conversion from Hex to decimal at the moment.

Thanks for the help,

I am most confused about the actual conversion from Hex to decimal at the moment.

1. Declare an integer and initialize it to 0.
2. Go through the hex string one character at a time.
2a. Isolate the hex digit and and convert it to an integer in the range of 0 to 15.
2b. Figure out the "place" of that hex integer (i.e. 1, 16, 256, 4096, etc.).
2c. Multiply 2a times 2b.
2d. Add 2c to the integer declared in step 1.
2e. Repeat 2a through 2d for the next hex digit.

commented: Obvious, yet very concise step-by-step information. +1

Hex number: AD which is 16^1 * 10 + 16 ^ 0 * 13 which is 160 + 13 = 173,

Hex number: 53 which is 16^1 * 5 + 16^0 * 3 = 83

So the sum of 173 and 83 is 256, so your hex result should be the same--

Hex result: AD + 53 = 100 (in Hex)

1-carried from (1 + A + 5)
1 - carried from (D + 3)
AD
+53
___
100


[content removed - irrelevant]
[more content removed - irrelevant]


Considering the worst case scenerio (high single-digits)--

FF
FF

then what?

16 ^ 1 * 15 + 16 ^ 0 * 15 = 255
16 ^ 1 * 15 + 16 ^ 0 * 15 = 255

255 + 255 = 510 = is the sum result of FF + FF

1 - carried (31 - 16 is 15. 16 subtracts into 31 once so we carry 1)
1 - carried (30 - 16 is 14. 16 subtracts into 30 once so we carry 1)
FF
+FF
___
E - the leftover '14' from 14
F - the leftover '15' from 14 + the carried 1
1 - the leftover 1 from the carry


I omitted some information because Vernon said it in a better way than I would have.

By the way, since we're doing only sums and not multiplication you should consider the fact that your carry value should never be higher than 1 unless you're summing more than 2 hex-values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.