Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN C++ PLEASE: Lab: Hexadecimal addition Part 1: Please include an alogorithm on

ID: 3903308 • Letter: I

Question

IN C++ PLEASE:

Lab: Hexadecimal addition

Part 1:

Please include an alogorithm on how you would write this program

Write a program that reads in two hexadecimal numbers from a file, hex.txt, and prints out the sum of the two numbers in hexadecimal.

From Wikipedia:  “In mathematics and computer science, hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0–9 to represent values zero to nine, and A, B, C, D, E, F (or alternatively a–f) to represent values ten to fifteen. For example, the hexadecimal number 2AF3 is equal, in decimal, to:

(2 × 163) + (10 × 162) + (15 × 161) + (3 ×     160), or 10,995.”

For example, if the file contains:

45AF

12B3

...your program will the result in decimal:

The decimal sum of 45AF and 12B3 is 22626.

  To solve this problem:

  a)   Read the hexadecimal numbers as character arrays

b) Convert the character arrays to numbers (by calling a function that takes the character array as a parameter, and returns an integer)

c) Add the numbers to get a decimal sum

d) Convert the sum to hexadecimal (by calling a function that fills a character array)

Part 2:

Assume that your file has an unknown number of hexadecimals.  

Modify / Enhance /Change your program so that it prints the sum of all the numbers in the file.

Explanation / Answer

// C++ program to convert hexadecimal to decimal
#include<iostream.h>
#include<string.h>
//using namespace std;

// Function to convert hexadecimal to decimal
int hexadecimalToDecimal(char hexVal[])
{
int len = strlen(hexVal);

// Initializing base value to 1, i.e 16^0
int base = 1;

int dec_val = 0;

// Extracting characters as digits from last character
for (int i=len-1; i>=0; i--)
{
// if character lies in '0'-'9', converting
// it to integral 0-9 by subtracting 48 from
// ASCII value.
if (hexVal[i]>='0' && hexVal[i]<='9')
{
dec_val += (hexVal[i] - 48)*base;

// incrementing base by power
base = base * 16;
}

// if character lies in 'A'-'F' , converting
// it to integral 10 - 15 by subtracting 55
// from ASCII value
else if (hexVal[i]>='A' && hexVal[i]<='F')
{
dec_val += (hexVal[i] - 55)*base;

// incrementing base by power
base = base*16;
}
}

return dec_val;
}
void decToHexa(int n,char a[] , char b[])
{

// char array to store hexadecimal number
char hexaDeciNum[100];

// counter for hexadecimal number array
int i = 0;
while(n!=0)
{
// temporary variable to store remainder
int temp = 0;

// storing remainder in temp variable.
temp = n % 16;

// check if temp < 10
if(temp < 10)
{
hexaDeciNum[i] = temp + 48;
i++;
}
else
{
hexaDeciNum[i] = temp + 55;
i++;
}

n = n/16;
}
cout<<"sum of "<<a<<" + "<< b<<" is ";
// printing hexadecimal number array in reverse order
for(int j=i-1; j>=0; j--)
cout << hexaDeciNum[j];
}

// Driver program to test above function
void main()
{
char hexNum1[30],hexNum2[30];
int x,y,z;
cout<<"Enter 1st hexadecimal number"<<endl;
cin.getline(hexNum1,29);
cout<<"Enter 2nd hexadecimal number"<<endl;
cin.getline(hexNum2,29);

x=hexadecimalToDecimal(hexNum1);
y = hexadecimalToDecimal(hexNum2);
z=x+y;
decToHexa(z,hexNum1,hexNum2);

}