Question

In: Computer Science

Write a C++ program to perform the addition of two hexadecimal numerals each with up to...

Write a C++ program to perform the 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.

Use arrays to store hexadecimal numerals as arrays of characters.

Input Notes: The program reads two strings, each a sequence of hex digits (no lowercase, however) terminated by a "q". Example: 111q AAAq

Output Notes (Prompts and Labels): Each hex input string is prompted for by:
Enter a hexadecimal number of 10 or fewer hex digits
Type q to stop the entry of the hex digits

The output consists of a display of two 10-digit hex numbers being added as follows:
§0000xxxxxx
+0000000yyy
-----------------
§0000zzzzzz

where § signifies a SPACE. Note the mandatory presence of the leading zeros. Here the input values typed were "xxxxxx" and "yyy" and the result was "zzzzzz".

Solutions

Expert Solution

#include <iostream>
#include <string>

using namespace std;

string plus_hex(string hex1, string hex2)
{
if (hex1.length() < hex2.length())
hex1.swap(hex2);

  

/*Strat algorithm*/

int length1, length2;
length1 = hex1.length();
length2 = hex2.length();
int flag = 0; // carry
int get1, get2;
int sum;

while (length1>0)
{
//get first number
if (hex1[length1 - 1] >= 'A')
get1 = hex1[length1 - 1] - 55;
else
get1 = hex1[length1 - 1] - '0';

//get second number

if (length2 > 0)
{
if (hex2[length2 - 1] >= 'A')
get2 = hex2[length2 - 1] - 55;
else
get2 = hex2[length2 - 1] - '0';
}
else
get2 = 0;

//get the sum
sum = get1 + get2 + flag;

if (sum >= 16)
{
int left = sum % 16;
if (left >= 10)
hex1[length1 - 1] = 'A' + left % 10;
else
hex1[length1 - 1] = '0' + left;
flag = 1;
}
else
{
if (sum >= 10)
hex1[length1 - 1] = 'A' + sum % 10;
else
hex1[length1 - 1] = '0' + sum;
flag = 0;
}

length1--;
length2--;
}

if (flag == 1)
return "1" + hex1;
else
return hex1;

/*End of algorithm*/
}
int main(void)
{
string hex1, hex2,hex3;
int len;
  
   cout<<"Enter 1st hexadecimal number of 10 or fewer hex digits"<<endl;
   cout<<"Type q to stop the entry of the hex digits"<<endl;
   getline(cin,hex1,'q');
   cout<<"Your 1st hexadecimal number is"<<hex1<<endl;
  
   cout<<"Enter 2nd hexadecimal number of 10 or fewer hex digits"<<endl;
   cout<<"Type q to stop the entry of the hex digits"<<endl;
   getline(cin,hex2,'q');
   cout<<"Your 2nd hexadecimal number is"<<hex2<<endl;

hex3=plus_hex(hex1, hex2);
   cout<<"Addition of above two number is"<<hex3<<endl;
   len=hex3.length();
   //cout<<len;
   if(len>10)
       cout<<"Addition Overtflow";
return 0;
}

Output:

Thank you.


Related Solutions

Write a C++ program to perform two-4 bit binary number operations including addition and subtraction. The...
Write a C++ program to perform two-4 bit binary number operations including addition and subtraction. The user will type in two-4 bit binary numbers with the selection of one of the operations. Then, the program will calculate the result of the calculation. Display two-4 bit binary numbers and the result from the calculation.
Computer Architecture and Organization(c++) Write a C++ program that prompts the user to enter a hexadecimal...
Computer Architecture and Organization(c++) Write a C++ program that prompts the user to enter a hexadecimal value, multiplies it by ten, then displays the result in hexadecimal. Your main function should a) declare a char array b) call the readLn function to read from the keyboard, c) call a function to convert the input text string to an int, d) multiply the int by ten, e) call a function to convert the int to its corresponding hexadecimal text string, f)...
Write a program in C that takes as input a four-digit hexadecimal number and prints the...
Write a program in C that takes as input a four-digit hexadecimal number and prints the next 10 hexadecimal numbers. Define a hexadecimal number as int hexNum[4] Allow upper- or lowercase letters for input and use uppercase letters for the hexadecimal output. For example, 3C6f should be valid input and should produce output 3C6F, 3C70, 3C71, . . . .
Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are...
Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are not allowed to use library functions for conversion. The output should look exactly as follows: DECIMAL      BINARY                     HEXDECIMAL                      BCD 0                      0000 0000                   00                                            0000 0000 0000 1                      0000 0001                   01                                            0000 0000 0001 2                      0000 0010                   02                                            0000 0000 0010 .                       .                                   .                                               . .                       .                                   .                                               . 255                  1111 1111                   FF                                            0010 0101 0101
Write a Python program that will perform various calculations (addition, subtraction, multiplication, division, and average). The...
Write a Python program that will perform various calculations (addition, subtraction, multiplication, division, and average). The program will add, subtract, multiply, or divide 2 numbers and provide the average of multiple numbers inputted from the user. You need to define a function named performCalculation which takes 1 parameter. The parameter will be the operation being performed (+,-,*,/). This function will perform the given prompt from the user for 2 numbers then perform the expected operation depending on the parameter that’s...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
write a program to perform the following in C Your program should prompt the user to...
write a program to perform the following in C Your program should prompt the user to enter ten words, one at a time, which are to be stored in an array of strings. After all of the words have been entered, the list is to be reordered as necessary to place the words into alphabetical order, regardless of case. Once the list is in alphabetical order, the list should be output to the console in order. The program should execute...
write a C++ program that : 1. Perform a rot13 substitution 2. Perform a caesar encryption...
write a C++ program that : 1. Perform a rot13 substitution 2. Perform a caesar encryption given a dictionary 3. Perform a caesar decryption given a dictionary 4. Create a random caesar cipher dictionary If user prints: -r : Perform rot13 substitution -g : generate a random caesar cipher dictionary. -e: Encrypt using the caesar cipher -d : Decrypt using the caesar cipher The format for the caesar cipher dictionary is a file with 26 pairs of letters, one per...
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write Matrix Addition 2 D (dimensional) Array program in c++.
-In C Programming- Write a program to display the total rainfall for a year. In addition,...
-In C Programming- Write a program to display the total rainfall for a year. In addition, display the average monthly rainfall, and the months with the lowest and highest rainfall. Create an array to hold the rainfall values. Create a 2nd parallel array (as a constant) to hold the abbreviated names of the months. I created my arrays to be 1 element bigger than needed, and then disregarded element [0] (so that my months went from [1] = "Jan" to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT