Answered step by step
Verified Expert Solution
Question
1 Approved Answer
undefined 5. [20 pts] Write a C program that functions as a decimal-to-binary or binary-to-decimal converter for signed, two's complement numbers. The user is to
undefined
5. [20 pts] Write a C program that functions as a decimal-to-binary or binary-to-decimal converter for signed, two's complement numbers. The user is to be prompted to decide which conversion is to be performed. Then, the user is prompted to enter a valid integer. The conversion is then performed and displayed on the screen. All numbers are assumed to occupy exactly 32 bits (i.e., the C int type). If user enters a binary number with fewer than 32 bits, the most-significant bit (MSB) is used to indicate the sign and the number should be sign-extended to 32 bits. If the user enters a decimal number, a negative sign is used to indicate negative quantities, as usual. Proper structure, style, documentation, error checking, and robustness are expected. If the program does not work as required, no points are awarded. Each function must be documented as follows: Here are some functions you should probably write: int get Integer (const char *prompt, int minval, int maxVal) This function prints the string prompt to the screen then asks the user for an integer. The function then validates the integer against the number range [minval, maxVal]. If not in this range, the function prints an error message, prints the prompt again, and repeats. Otherwise, the validated integer is returned. void getBinaryString (const char *prompt, char *str) This function prints the string prompt to the screen then asks the user for a string. It then validates each character of the string to ensure it is a 1 or 0 (ASCII character). If so, the function stores the string in the str parameter. Otherwise, the function prints an error message and repeats. void signExtendString (const char *str, char *estr) This function takes the string in str, which comprises only 1 and 0 characters, and sign- extends it to 32 bits (i.e., 32 characters) in estr. If str already represents 32 bits, the string is simply copied to estr. int binaryStringToInt (const char *estr) This function interprets the 32-bit string of 1 and 0 characters in estr as a signed, two's complement integer. The integer value is returned. void print InBinary (int val) This function converts the parameter val to binary representation and prints it on the screen in 32 bits. Here is a sample run of the program Type 1 to convert from decimal ta binary or 2 to convert from binary to decimal: Orange That is not a valid integer. Try again. Cype 1 to convert from decimal to binary or 2 to convert from binary to decimal: 13 Hello?! Type 1 or 2. Do you need change-of-major form? ype 1 to convert from decimal to binary or 2 to convert from binary to decimal: 1 Enter decimal number: decimal number That is not a valid integer. Try again. Enter decimal number 123 The decimal number 123 has 32-bit binary representation of 00000000000000000000000001111011 3 Type 1 to convert from decimal to binary or 2 to convert from binary to decimal: 2 Enter a binary number: 123 This is not a valid binary number. Try again. Enter a binary number: 1101 The binary number 11111111 111111111101 has decimal value of 3 Type 1 to convert from decimal to binary or 2 to convert from binary to decimal: 2 Enter a binary number: 01101 The binary number 00000000000000000000000000001101 has decimal value of 13 5. [20 pts] Write a C program that functions as a decimal-to-binary or binary-to-decimal converter for signed, two's complement numbers. The user is to be prompted to decide which conversion is to be performed. Then, the user is prompted to enter a valid integer. The conversion is then performed and displayed on the screen. All numbers are assumed to occupy exactly 32 bits (i.e., the C int type). If user enters a binary number with fewer than 32 bits, the most-significant bit (MSB) is used to indicate the sign and the number should be sign-extended to 32 bits. If the user enters a decimal number, a negative sign is used to indicate negative quantities, as usual. Proper structure, style, documentation, error checking, and robustness are expected. If the program does not work as required, no points are awarded. Each function must be documented as follows: Here are some functions you should probably write: int get Integer (const char *prompt, int minval, int maxVal) This function prints the string prompt to the screen then asks the user for an integer. The function then validates the integer against the number range [minval, maxVal]. If not in this range, the function prints an error message, prints the prompt again, and repeats. Otherwise, the validated integer is returned. void getBinaryString (const char *prompt, char *str) This function prints the string prompt to the screen then asks the user for a string. It then validates each character of the string to ensure it is a 1 or 0 (ASCII character). If so, the function stores the string in the str parameter. Otherwise, the function prints an error message and repeats. void signExtendString (const char *str, char *estr) This function takes the string in str, which comprises only 1 and 0 characters, and sign- extends it to 32 bits (i.e., 32 characters) in estr. If str already represents 32 bits, the string is simply copied to estr. int binaryStringToInt (const char *estr) This function interprets the 32-bit string of 1 and 0 characters in estr as a signed, two's complement integer. The integer value is returned. void print InBinary (int val) This function converts the parameter val to binary representation and prints it on the screen in 32 bits. Here is a sample run of the program Type 1 to convert from decimal ta binary or 2 to convert from binary to decimal: Orange That is not a valid integer. Try again. Cype 1 to convert from decimal to binary or 2 to convert from binary to decimal: 13 Hello?! Type 1 or 2. Do you need change-of-major form? ype 1 to convert from decimal to binary or 2 to convert from binary to decimal: 1 Enter decimal number: decimal number That is not a valid integer. Try again. Enter decimal number 123 The decimal number 123 has 32-bit binary representation of 00000000000000000000000001111011 3 Type 1 to convert from decimal to binary or 2 to convert from binary to decimal: 2 Enter a binary number: 123 This is not a valid binary number. Try again. Enter a binary number: 1101 The binary number 11111111 111111111101 has decimal value of 3 Type 1 to convert from decimal to binary or 2 to convert from binary to decimal: 2 Enter a binary number: 01101 The binary number 00000000000000000000000000001101 has decimal value of 13Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started