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

Stumped on this particular question. Have no idea how to start it. Can you guide

ID: 3731709 • Letter: S

Question

Stumped on this particular question. Have no idea how to start it. Can you guide me in the right direction?

Tests:

Results of test:

Thank you.

The function has this signature: def fraction_to_binary (fraction_string, significant_binary_digits) Convert a string of decimal digits up to 32 digits long representing a positive decimal fractional value into its equivalent binary digit string. For this function to terminate the algorithm accepts the num the first "1" in the result or any digits following the first "1". The last significant binary digit must be rounded in the final result. So the algorithm will print more digits than the specified number

Explanation / Answer

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

void fraction_tobinary(char fraction[],int signbinary_digits)

{

int i,j=0,flag=0,k,n,res=0;

char *binary;

binary=(char*)malloc(signbinary_digits+1);

for(i=0;i<signbinary_digits;i++)

binary[i]='0';

n=strlen(fraction);

k=signbinary_digits;

while(k)

{

for(i=n-1;fraction[i]!='.';i--)

{

res=res+(fraction[i]-48)*2;

fraction[i]=res%10+48;

res=res/10;

}

binary[j++]=res+48;

if(res==1)

flag=1;

if(flag==1)

k--;

res=0;

}

binary[j]='';

printf(" %s",binary);

printf(" 0.");

printf("%s",binary);

free(binary);

}

int main()

{

char str_decimal[4][10]={"0.5","0.25","0.75","0.333333"};

char str[20];

int sigfntbit[4]={1,4,6,10},i,j,k;

for(i=0;i<4;i++)

{

printf(" decimal=");

printf("%s",str_decimal[i]);

for(j=0;j<4;j++)

{

strcpy(str,str_decimal[i]);

printf(" Significant bit=%d",sigfntbit[j]);

fraction_tobinary(str,sigfntbit[j]);

}

}

  

  

return 0;

}