Question

In: Computer Science

Create design document for a program that will allow the user: Convert a number to binary....

Create design document for a program that will allow the user:

  • Convert a number to binary.
  • Convert from binary to number.
  • Display the hexadecimal representation of a binary number.
  • Given an hexadecimal display its binary representation.
  • Convert a number to IEEE single precision and vice versa.
  • More to come.

PLEASE ADD PSEUDOCODE AND USE C PROGRAMMING

USE FUNCTIONS IF POSSIBLE

Solutions

Expert Solution

/*

program is written in c programming language for all the above operations using function

*/

#include<math.h>
#include <stdio.h>
void numtoBinary(int n);
void Binarytodec(int n);
void hextobin(char* hd);
void bintohex(int n);

//conversion from number to ieee single precision
int binary(int n, int i)
{
int k;
for (i--; i >= 0; i--)
{
k = n >> i;
if (k & 1)
printf("1");
else
printf("0");
}
}
typedef union
{
float f;
struct
{
unsigned int mantissa : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
} field;
} myfloat;

//conversion from ieee single precision to number

typedef union {
  
float f;
struct
{

unsigned int mantissa : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
  
} raw;
} myfloat1;

unsigned int convertToInt(int* arr, int low, int high)
{
unsigned f = 0, i;
for (i = high; i >= low; i--) {
f = f + arr[i] * pow(2, high - i);
}
return f;
}

//main method

int main()
{
int ch,a,i;
char str[100] ;
myfloat var;
myfloat1 var1;
unsigned int ieee[32];
printf("1.conversion from number to binary\n 2.conversion from binary to number\n3.conversion from hexadecimal to binary");
printf("\n 4.conversion from binary to hexadecimal\n 5.conversion from number to IEEE single pricision\n 6.conversion from IEEE single pricision to number");
printf("\n \n enetr a choice : ");
scanf("%d",&ch);
switch(ch)
{
   case 1:
  
printf("enter a decimal number");
scanf("%d",&a);
           numtoBinary(a);
           break;
   case 2:
           printf("enter a binary number");
scanf("%d",&a);
           Binarytodec(a);
           break;
   case 3:
     
   printf("enter a hexadecimal number");
scanf("%s",str);
           hextobin(str);
           break;
       case 4:
       printf("enter a binary number");
scanf("%d",&a);
           bintohex(a);
           break;
   case 5:
          
printf("Enter any float number: ");
scanf("%f",&var.f);
printf("%d ",var.field.sign);
binary(var.field.exponent, 8);
printf(" ");
binary(var.field.mantissa, 23);
printf("\n");
           break;
   case 6:
printf("enter 32 bit unsigned IEEE number");
for(i=0;i<32;i++)
{
scanf("%d",&ieee[i]);
}
unsigned f = convertToInt(ieee, 9, 31);
var1.raw.mantissa = f;
f = convertToInt(ieee, 1, 8);
var1.raw.exponent = f;

var1.raw.sign = ieee[0];
  
printf("The float value of the given IEEE-754 representation is : \n");
printf("%f", var1.f);
          
           break;
   default:
           printf("invalid choice");
           break;
}
return 0;
}

//conversion from decimal to binary
void numtoBinary(int n)
{
int bN[32];
printf("decimal number : %d",n);
int i = 0;
while (n > 0) {
  
bN[i] = n % 2;
n = n / 2;
i++;
}
printf("\n binary number :");
for (int j = i - 1; j >= 0; j--)
printf("%d",bN[j]);

}

//conversion from binary to decimal
void Binarytodec(int n)
{
  
int dec = 0, i = 0, rem;
printf("binary number : %d",n);
while (n != 0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
  
printf("\n decimal number :%d",dec);
}

//conversion from hexadecimal to binary
void hextobin(char* hd)
{
  
long int i = 0;
  
while (hd[i]) {
  
switch (hd[i]) {
case '0':
printf("0000");
break;
case '1':
printf("0001");
break;
case '2':
printf("0010");
break;
case '3':
printf("0011");
break;
case '4':
printf("0100");
break;
case '5':
printf("0101");
break;
case '6':
printf("0110");
break;
case '7':
printf("0111");
break;
case '8':
printf("1000");
break;
case '9':
printf("1001");
break;
case 'A':
case 'a':
printf("1010");
break;
case 'B':
case 'b':
printf("1011");
break;
case 'C':
case 'c':
printf("1100");
break;
case 'D':
case 'd':
printf("1101");
break;
case 'E':
case 'e':
printf("1110");
break;
case 'F':
case 'f':
printf("1111");
break;
default:
printf("\nInvalid hexadecimal digit %c",
hd[i]);
}
i++;
}
}

//conversion from binary to hexadecimal
void bintohex(int bval)
{
long int hexval = 0, i = 1, rem;


while (bval != 0)
{
rem = bval % 10;
hexval = hexval + rem * i;
i = i * 2;
bval = bval / 10;
}
printf("hexadecimal number: %lX", hexval);
}


Related Solutions

Design a program that asks the user for a number and the program computes the factorial...
Design a program that asks the user for a number and the program computes the factorial of that number and displays the result . Implement with two different modules - one that uses a for loop and one that uses a while loop Grading Rubrick Program Compiles Cleanly  syntax errors25 pts-5 per errorProgram runs without runtime errors ( validation)run-time errors 25 pts-5 per errorProgram give correct answersLogic errors30 pts-5 per errorProgram is repeatableNot repeatable5 pts-5 ptsProgram is well modularizedBarely Modularized10 pts-...
Create a menu-based program that will allow the user to calculate the area for a few...
Create a menu-based program that will allow the user to calculate the area for a few different shapes: square, rectangle, parallelogram, and circle. Refer to the Sample Output in this document to see what menu options you should use. Create PI as a global constant variable. Main Function use do-while to repeat program until user chooses option 5 call the displayMenu function get user choice & validate with while loop depending on user’s choice, get the data required to calculate...
create a program that will allow the user to enter a start value from 1 to...
create a program that will allow the user to enter a start value from 1 to 5, a stop value from 10 to 12 and a multiplier from 1 to 4. the program must display a multiplication table from the values entered. for example if the user enters: start 2, stop 10 and multiplier 3, the table should appear as follows: 3*2=6 3*3=9 3*4=12 . . . 3*10=30
Create a menu-based program that will allow the user to calculate the area for a few...
Create a menu-based program that will allow the user to calculate the area for a few different shapes: square, rectangle, parallelogram, and circle. Refer to the Sample Output in this document to see what menu options you should use. Create PI as a global constant variable. Main Function use do-while to repeat program until user chooses option 5 call the displayMenu function get user choice & validate with while loop depending on user’s choice, get the data required to calculate...
Using Python, create a program that will act as a number convertor. This program will convert...
Using Python, create a program that will act as a number convertor. This program will convert an input decimal integer into binary and hexadecimal formats. a) Define a main function named numberconvertor(). Inside the main function, write all the logic of your code. [5% marks] b) Looping indefinitely (Hint: use infinite while loop), the program should give the user the 3 options given below and allow the user to select one among them. [15% marks] 1. converting a decimal number...
Design a Decoder Circuit that can convert a 4-bit Binary Number to a Hexadecimal Output.
Design a Decoder Circuit that can convert a 4-bit Binary Number to a Hexadecimal Output.
We need to create basic program that will simply display a menu and allow the user...
We need to create basic program that will simply display a menu and allow the user to select one of the choices. The menu should be displayed such that each option will be numbered, as well as have one capital letter so as to indicate that either the number or the designated letter can be entered to make their choice. Once the choice is made, the sub-menu for that selection should be displayed. Colors with an odd number of letters...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary 2. Binary to Decimal 3. Hexadecimal to Decimal 4. Decimal to Hexadecimal 5. Binary to Hexadecimal 6. Hexadecimal to Binary The user will type in the input number as following: Binary number : up to 8 bits Hexadecimal number: up to 2 bytes Decimal number: Less than 256 As a result, print out the output after the conversion with their input numbers. The program...
Convert the decimal number, 315.56 into binary form?
Convert the decimal number, 315.56 into binary form?
create a program that asks user math questions and keeps track of answers... Python Allow the...
create a program that asks user math questions and keeps track of answers... Python Allow the user to decide whether or not to keep playing after each math challenge. Ensure the user’s answer to each math problem is greater than or equal to zero. Keep track of how many math problems have been asked and how many have been answered correctly. When finished, inform the user how they did by displaying the total number of math problems, the number they...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT