Can u please put it as type style so i can copy it from here, i will appreciate
ID: 3600337 • Letter: C
Question
Can u please put it as type style so i can copy it from here, i will appreciate that. Not a pictures or screenshot
Can u please put it as type style so i can copy it from here, i will appreciate that. Not a pictures or screenshot Project 2: 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 mes sage "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
Explanation / Answer
Hi,
here is the full code with comments to help you understand.
#include <string>
#include <map>
#include <cctype>
#include <algorithm>
#include <iostream>
using namespace std;
int convert2Decimal(char item) {
static bool isFirst = true;
static map<char, int>map;
if(isFirst){
for(int i=0;i<16;++i)
map["0123456789ABCDEF"[i]]=i;
isFirst = false;
}
return map[::toupper(item)];
}
char convert2Hex(int num){
static bool isFirst = true;
static map<int, char>map;
if(isFirst){
for(int i=0;i<16;++i)
map[i]="0123456789ABCDEF"[i];
isFirst = false;
}
return 0 <= num && num <= 15 ? map[num] : -1;
}
void addition(char h1[10], char h2[10]) {
int carry = 0;
char result[10];
for (int i = 0; i < 9; i++) {// 9 : 10-1 (-1 for EOS)
int wk = convert2Decimal(h1[i]) + convert2Decimal(h2[i]) + carry;
if(wk < 16){
carry = 0;
} else {
carry = 1;
wk -= 16;
}
result[i] = convert2Hex(wk);
}
if(carry)
cout << "overflow in addition" << endl;
reverse(result, result+sizeof(result)-1);
cout << string(result) << endl;//000001AA9
}
int main(){
char hex1[10] = "01400A0A0";
char hex2[10] = "2300AAAB0";
char sum[10];
int flag=1;
while(flag==1)
{
addition(hex1, hex2);
cout<<"want to test one more 1:0?"<<endl;
cin>>flag;
}
return 0;
}
Thumbs up if this was helpful, otherwise let me know in comments
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.