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.
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 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...
Write a program in C (NOT C++ or C#) The program inputs 5 elements into each...
Write a program in C (NOT C++ or C#) The program inputs 5 elements into each of 2 integer arrays. Multiply corresponding array elements, that is, arrayOne[0] * arrayTwo[0], etc. Save the product into a third array called prodArray[ ]. Display the product array.
Write a program to perform the following two tasks: 1. The program will accept a string...
Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the rose". Display the result string. 2. Then...
Write a program to perform the following two tasks: 1. The program will accept a string...
Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the rose". Display the result string. 2. Then...
C++ program to perform each of the area calculations in separate functions. Your program will take...
C++ program to perform each of the area calculations in separate functions. Your program will take in the relevant information in the main (), call the correct function that makes the calculation, return the answer to the main () and then print the answer to the screen. The program will declare a variable called “choice” of type int that is initialized to 0. The program will loop while choice is not equal to 4. In the body of the loop...
Write a C program that asks the user to enter any two integernumbers, and each...
Write a C program that asks the user to enter any two integer numbers, and each number consists of four-digits. Your program should check whether the numbers are four digits or not and in case they are not a four digit number, the program should print a message and exit, otherwise it should do the following:Print a menu as follows:Select what you want to do with the number 1-3:1- Print Greatest Common Divisor (GCD) of the two numbers.2- Print sum...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT