I am writing a program in C that will convert a floating point number to a base
ID: 3821352 • Letter: I
Question
I am writing a program in C that will convert a floating point number to a base two binary number. It then normalizes it. so 3.74 outputs 1.111E1 . I am not supposed to have trailing zeros in the binary representation. so It will only print out until the last one on the left hand side. so 140.1 will print 1.0001100000110011001101E7. Can someone help me with this? Thanks
#include<stdio.h>
int main()
{
int integral, binaryInt = 0, i = 1;
float binaryFract = 0, k =0.1f, fractional, temp, binaryTotal, f;
printf("Convert float to binary ");
printf(" Enter float value : ");
scanf("%f",&f);
integral = (int)f;
//Separating the fractional part of the value
fractional = f - (int)f;
//First lets convert integral part to Binary
while(integral>0)
{
binaryInt = binaryInt + integral % 2 * i;
i = i * 10;
integral = integral / 2;
}
//Loop for converting Fractional value to binary now
while(k>0.00000001)
{
temp = fractional *2;
binaryFract = binaryFract+((int)temp)*k;
fractional = temp - (int)temp;
k = k / 10;
}
//Combining the integral and fractional binary value.
binaryTotal = binaryInt + binaryFract;
printf(" binary equivalent = %lf ", binaryTotal);
}
Explanation / Answer
#include<stdio.h>
#include<math.h>
int main()
{
int k =0, integral, i = 1, pos=-9;
long binary = 0;
float fractional, temp, f;
double binaryTotal;
char str[50];
void removeTrailZero(char *a);
printf("Convert float to binary ");
printf(" Enter float value : ");
scanf("%f",&f);
integral = (int)f;
//Separating the fractional part of the value
fractional = f - (int)f;
//First lets convert integral part to Binary
while(integral>0)
{
binary = binary + integral % 2 * i;
if (integral % 2 == 1)
pos = k; //pos stores the location of first 1
i = i * 10;
k++;//k calculates the position it is calcuation
integral = integral / 2;
}
k = 0; //reset k
//Loop for converting Fractional value to binary now
while(k < 8 && fractional!=0)
{
temp = fractional *2;
binary = binary*10 +((int)temp);
k ++;
if ( -k > pos && ((int)temp) == 1)
pos = -k;
fractional = temp - (int)temp;
}
//1001.11101 = 100111101, pos = 3, k = 5
//0.001001 = 1001 pos = -3 k = 6
//Combining the integral and fractional binary value.
binaryTotal = (float)binary/pow(10,(pos+k));
sprintf(str,"%lf", binaryTotal); //convert binary total to string
removeTrailZero(str); //this function parses the string from right and keep on removing zeros
printf(" binary equivalent = %sE%d ", str,pos);
}
void removeTrailZero(char *a) { //this function parses the string from right and keep on removing zeros
int i, len = strlen(a);
for (i = len-1; i>2; i--)
if (a[i] == '0')
a[i] = '';
else if (a[i] == '1')
return;
}
Here you go champ. The code perfectly matches your specs (as far as am concerned). I changed certain logic and also commented them so that you can understand them. If you have any doubt on the logic of approach, please feel free to comment below. I shall be glad to help you with the logic of approach.
Happy Coding!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.