SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Arrays
Exercise:
Programming Projects
Question:2 | ISBN:9780321531346 | Edition: 7

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 hexadecimal numeral d is the same as base 10 numeral 13 and the hexadecimal 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. 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.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

 

Program:

 

#include <iostream>
#include <string>
using namespace std;
const int MaxDigits = 10;
void inputHexa(char hexa[MaxDigits]);
void displayHexa(char hexa[MaxDigits]);
void sumHexa(char digits1[MaxDigits], char digits2[MaxDigits], char total[MaxDigits]);
int main()
{
    char ch;    
    do
    {
        char digits1[MaxDigits] = {'0'};
        char digits2[MaxDigits] = {'0'};
        char total[MaxDigits] = {'0'};
        cout << "Enter the first hexadecimal number(Maximum 10 digits):  ";
        inputHexa(digits1);
        cout << "Enter the second hexadecimal number(Maximun 10 digits): ";
        inputHexa(digits2);
        sumHexa(digits1, digits2, total);
        cout << "Sum of ";
        displayHexa(digits1);
        cout << " and ";
        displayHexa(digits2);
        cout << " is: ";
        displayHexa(total);
        cout << endl;
        cout << "\nEnter Y/y to repeat calculations again: ";
        cin.get(ch);
        cin.ignore();
        cout << endl;
    }while(ch == 'Y' || ch == 'y');
    system("pause");
    return 0;
} 
void inputHexa(char hexa[MaxDigits])
{
    char ch = '0';
    int n = 0;
    while((ch = cin.peek()) != '\n' && n < MaxDigits)
    {
        hexa[n] = ch;
        n++;

        cin.get(ch);
    } 
    cin.ignore();    
    for(int i = 0; i < n / 2; i++)
        {
        char tempCh = hexa[i];
        hexa[i] = hexa[n - 1 - i];
        hexa[n - 1 - i] = tempCh;
        } // end for
} 
void displayHexa(char hexa[MaxDigits])
{
    for(int i = MaxDigits - 1; i >= 0; i--)
        {
        if(hexa[i] != 0)
            cout << hexa[i];
        } 
}
void sumHexa(char digits1[MaxDigits], char digits2[MaxDigits], char total[MaxDigits])
{
    int number1, number2, sum;
    int c1 = 0, c2 = 0;
    char tempCh;
        for(int i = 0; i < MaxDigits; i++)
        {
            if(digits1[i] >= '0' && digits1[i] < '0' + 10)
        {
        number1 = digits1[i] - '0';
        }
            else
        {
tempCh = toupper(digits1[i]);

            if(tempCh >= 'A' && tempCh <= 'F')
            {
                number1 = tempCh - 'A' + 10;
            }
            else
            {
                
                cout << "Invalid input!" << endl;
                system("pause");
                exit(EXIT_FAILURE);
            } 
        } 

            if(digits2[i] >= '0' && digits2[i] < '0' + 10)
        {
        
            number2 = digits2[i] - '0';
        }
            else
        {
            
            tempCh = toupper(digits2[i]);

            
            if(tempCh >= 'A' && tempCh <= 'F')
            {
                
                number2 = tempCh - 'A' + 10;
            }
            else
            {
                
                cout << "Invalid input!" << endl;
                system("pause");
                exit(EXIT_FAILURE);
            } 
        } 

    
            c1 = c2;
            sum = (number1 + number2 + c1) % 16;

            c2 = (number1 + number2 + c1) / 16;

            if(sum >= 0 && sum < 10)
        {
            total[i] = char('0' + sum);
        }
            else if(sum >= 10 && sum <= 15)
        {
            total[i] = char('A' + sum - 10);
        } 
        } 
        if(c1 == 1 && c2 == 1)
        {

        cout << "---Addition Overflow---" << endl;

        
        for(int i = MaxDigits - 1; i >= 0; i--)
        {
            total[i] = -1;
        } 
    }
} 

Output:

 

0 0

Discussions

Post the discussion to improve the above solution.