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

I need help writing a hexadecimal addition. Here is the question: Hexadecimal nu

ID: 3625278 • Letter: I

Question

I need help writing a hexadecimal addition. Here is the 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". 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 "Overflow error".
You should be able to change the maximum length of the hexadecimal numbers required by changing only one globally defined constant and recompiling. 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

please rate - thanks

#include<stdio.h>
#include <conio.h>
int add(char[],int,char[],int,char[],int);
int getvalue(char);
int checkvalid(char[]);
int main()
{const int size=10;
char num1[size],num2[size],ans[size+1],yorn;
int d1,d2;
int over,valid;

do{
   printf("Enter 1st number in hex: ");
   scanf("%s",&num1);
   do
     {valid=checkvalid(num1);
      if(!valid)
         {printf("Invalid hex digit ");
         printf("Enter 1st number in hex: ");
         scanf("%s",&num1);
         }
      }while(!valid);
    printf("Enter 2nd number in hex: ");
   scanf("%s",&num2);
   do
      {
      valid=checkvalid(num2);
      if(!valid)
         {printf("Invalid hex digit ");
         printf("Enter 2nd number in hex: ");
         scanf("%s",&num2);
         }
      }while(!valid);
  
   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)
       printf("%s + %s = overflow ",num1,num2);
   else
       printf("%s + %s = %s ",num1,num2,ans);
   while(getchar()!=' ');
   printf("again? (Y/N): ");
scanf("%c",&yorn);
}while(toupper(yorn)=='Y');  
return 0;
}
int checkvalid(char 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 0;
      }
return 1;
}
int getvalue(char h)
{int i;
char hex[17]={"0123456789ABCDEF"};
for(i=0;i<17;i++)
     if(h==hex[i])
          return i;
}
int 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 1;
return 0;
  
   
}