You will write a C program to convert a base 10 real number into a binary equiva
ID: 3666641 • Letter: Y
Question
You will write a C program to convert a base 10 real number into a binary equivalent and the hexadecimal equivalent. The number (base 10) is typed in and the code will output the binary equivalent with correct number of bits. Examples of running the code below calls are given below: > 12.25 1100.01 (bin) C.4 (hex) > 56.625 111000.101 38.A (hex) Your program should produce an error message if input arguments are out of range (negative numbers, infinity etc.) produce an error if no arguments are given on the command line; not use any use the computer's built-in IEEE 754 conversion capabilities rather the computation should be done by your code (no special libraries). Your program does not need to handle floating point input that are not number (NaN or plusminus infinity). Describe any limitations that may exist for your program. Full marks will only be given to programs capable of handling the complete range of inputs, however, a proper explanation of any limitations your program may have will minimize any deductions. Only properly commented code will receive full marks. To test your code, select appropriate test cases to show that it works throughout the possible input range. Choosing a test set is a non-trivial problem and should be done with care, concentrating on number boundaries (maximum, minimum) and other code specific areas which may result in problems for your code. Include a printout of the results for all of your tests. The "C" source code should contain a header which has the following information: Full name Student number Course number Date Put all your functions and main routine into a single C language file, and only use standard C libraries (e.g. stdio.h, stdlib.h).Explanation / Answer
#include<stdio.h>
#include<conio.h>
void main()
{
int i,num,*bin,*oct,bin_len,oct_len,hex_len;
char *hex,ch;
clrscr();
printf(" Enter the number to be converted :");
scanf("%d",&num);
printf(" ");
//binary
bin_len=binary(bin,num);
printf(" Binary : ");
for(i=0;i<bin_len;i++)
printf("%d",bin[i]);
printf(" ");
//hexadecimal
hex_len=hexadecimal(hex,num);
printf(" Hexadecimal : ");
for(i=0;i<hex_len;i++)
printf("%c",hex[i]);
printf(" ");
getch();
}
int binary(int *bin,int num)
{
int rem=0,i=0;
do
{
rem=num%2;
num=num/2;
bin[i]=rem;
i++;
}while(num>0);
array_reverse_i(bin,i);
return i;
}
int hexadecimal(char *hex,int num)
{
int rem=0,i=0;
do
{
rem=num%16;
num=num/16;
switch (rem)
{
case 10 : hex[i]='A';
i++;
break;
case 11 : hex[i]='B';
i++;
break;
case 12 : hex[i]='C';
i++;
break;
case 13 : hex[i]='D';
i++;
break;
case 14 : hex[i]='E';
i++;
break;
case 15 : hex[i]='F';
i++;
break;
default :hex[i]=(char)(rem+48);
i++;
}
}while(num>0);
array_reverse_c(hex,i);
return i;
}
array_reverse_i(int *arr,int len)
{
int i,temp;
for(i=0;i<len/2;i++)
{
temp=arr[i];
arr[i]=arr[len-1-i];
arr[len-1-i]=temp;
}
return 0;
}
array_reverse_c(char *arr,int len)
{
int i;
char temp;
for(i=0;i<len/2;i++)
{
temp=arr[i];
arr[i]=arr[len-1-i];
arr[len-1-i]=temp;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.