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

Write a program that reads in two hexadecimal numbers from a file, hex.dat, and

ID: 3628736 • Letter: W

Question

Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out the sum of the two numbers in hexadecimal. (As noted in class, first do this without using a file and by reading using the cin>> command)

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 output (if you output the result in decimal):



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

Explanation / Answer

please rate - thanks

#include<iostream>
#include <fstream>
using namespace std;
bool add(char[],int,char[],int,char[],int);
int getvalue(char);
bool checkvalid(string);
int main()
{const int size=10;
char num1[size],num2[size],ans[size+1],yorn;
int d1,d2;
bool over,valid;
ifstream in;
in.open("hex.dat");       
if(in.fail())           
   { cout<<"input file did not open please check it ";
   system("pause");
   return 1;
   }
      in>>num1;
while(in)
   {
   in>>num2;
   valid=checkvalid(num1);
      if(!valid)
         {cout<<num1<<" Invalid hex digit ";
         cout<<"program aborting: ";
         system("pause");
         return 0;
         }
      valid=checkvalid(num2);
      if(!valid)
         {cout<<num2<<" Invalid hex digit ";
          cout<<"program aborting: ";
         system("pause");
         return 0;
          }
   for(d1=0;num1[d1]!='';d1++)
         num1[d1]=toupper(num1[d1]);
   for(d2=0;num2[d2]!='';d2++)
          num2[d2]=toupper(num2[d2]);  
   if(d2<d1)
       over=add(num1,d1,num2,d2,ans,size);
   else
       over=add(num2,d2,num1,d1,ans,size);
   if(over)
       cout<<num1<<" + "<<num2<<" = "<<"overflow ";
   else
       cout<<num1<<" + "<<num2<<" = "<<ans<<endl;
in>>num1;
}
in.close();
system("pause");
return 0;
}
bool checkvalid(string n)
{int i,j;
char hex[17]={"0123456789ABCDEF"};
for(i=0;n[i]!='';i++)
     {for(j=0;j<16;j++)
         if(toupper(n[i])==hex[j])
               j=20;
     if(j<20)
         return false;
      }
return true;
}
int getvalue(char h)
{int i;
char hex[17]={"0123456789ABCDEF"};
for(i=0;i<17;i++)
     if(h==hex[i])
          return i;
}
bool add(char num2[],int m,char num1[],int l,char ans[],int size)
{char hex[17]={"0123456789ABCDEF"};
int i,j,c=0,a,digits=0;
for(i=0;i<size+1;i++)
    ans[i]='';
l--;
m--;
while(l>=0)
{a=getvalue(num2[m])+getvalue(num1[l])+c;
   c=a/16;
   ans[m]=hex[a%16];
   m--;
   l--;
   digits++;
   }
while(m>=0)
    {a=getvalue(num2[m])+c;
     c=a/16;
     ans[m]=hex[a%16];
     m--;
     digits++;
     }
if(c!=0)
{ for(i=digits;i>=0;i--)
      ans[i]=ans[i-1];
   ans[0]=hex[c];
}
if(digits>size-1)
    return true;
return false;
  
   
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote