Question: write a program in C that converts a binary oating point value entered by the user to decimal, where the decimal part is represented as
write a program in C that converts a binary oating point value entered by the user to decimal, where the decimal part is represented as a sum of powers of two, e.g., this program must convert 1100.1011 to 12 + 1/2 + 1/8 + 1/16.
(a) The last index of function returns as an int the index of the last occurrence of ch within in str, assuming in str is a valid null-terminated string.
(b) The sub string function assigns to out str the substring in in str that consists of characters in str[start]...in str[end - 1], such that out str is a valid null-terminated string.
both functions are completed.
USE BITWISE OPERATONS!
my code so far:
#include "stdio.h" #include "string.h"
#define MAX_BITS 32 #define MAX_LENGTH 49
int last_index_of(char in_str[], char ch); void sub_string(char in_str[], char out_str[], int start, int end);
int main() { int i = 0; char input[MAX_LENGTH + 1]; // + 1 for '\0' char bits[MAX_BITS + 1]; printf("Enter a floationg point value in binary: "); scanf("%s", input); input[strlen(input)] = '\0'; printf("the user entered %s ", input);
int lastIndex = last_index_of(input, '.'); printf("last index: %d ", lastIndex);
sub_string(input, bits, 0, lastIndex); printf("bits: %s ", bits);
}
int last_index_of(char *in_str, char ch) { char *ptr = strrchr(in_str, ch); return ptr - in_str; }
void sub_string(char in_str[], char out_str[], int start, int end) { int i,j; for(i = start, j = 0; i < end && in_str[i] != '\0';i++,j++){ out_str[j] = in_str[i]; } out_str[j] = '\0'; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
