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

Subtract xed-width unsigned integers in any radix; N digits. Need to write out a

ID: 3747437 • Letter: S

Question

Subtract xed-width unsigned integers in any radix; N digits. Need to write out a program in C from this pseudocode. The code needs to accept hex numbers, subtract, and print out the answer in binary. There's an error in the pseudocode but please adhere as closely to it as possible.

Equation 2.5 refers to

Algorithm 3.4: Subtract fixed-width unsigned integers in any radix: N digits. (Refer to Equation 2.5) 1: borrow 0 2: for i = 0 to N-1 do 3 ify,x, then o i-th digit D subtract Y from X difference, (xi-yi) else if i=N-l then 5: orrow 1 Result will be wrong! difference,-(x-y) else 10 while x, 0 do 12 13 14 15 16 17 18 19 20 21 22: end for end while while j > i do end while difference, (radix + xi-yi) end if

Explanation / Answer

I have made

void printDifference(char hex1[] ,char hex2[]) as function which takes two char arrays as input
they contains hex representation of two numbers.They are converted into decimal numbers and difference is taken.The binary number of that difference is then printed.


//two char arrays are taken as input
//they contains hex representation of two numbers

//N = length of hex notation number

void printDifference(char hex1[] ,char hex2[]){
//firstHexEval and secondHexEval computes the decimal value of hex representation
int firstHexEval=0;
int secondHexEval=0;
int p=0;
for(i=N-1;i>=0;i--){
firstHexEval= firstHexEval + pow(16,p)*toInt((hex1[i]);
p++;
}

p=0;
for(i=N-1;i>=0;i--){
secondHexEval= secondHexEval + pow(16,p)*toInt((hex2[i]);
p++;
}

int diff= abs(firstHexEval-SecondHexEval );
printDifferenceInBinary(diff)
}

//print difference in binary
void printDifferenceInBinary(int n)
{
/* step 1 */
if (n > 1)
bin(n/2);

/* step 2 */
printf("%d", n % 2);
}

//helping function to return int value of character in hexadecimal notation
int toInt(char c){
switch(c){
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'A':
return 10;
case 'B':
return 11;
case 'C':
return 12;
case 'D':
return 13;
case 'E':
return 14;
case 'F':
return 15;   
}

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