I am to create three different versions of the following C program code below that implements the conversion of unsigned binary numbers into decimal (Base 2 to Base 10 conversion).
Version 1: Complete the C program ”bin2dec ver1.c” that implements binary to decimal conversion. The maximum number of binary bits is 32. The program is made of the functions ”unsigned binary to decimal(const char *str)”and ”main”. The parameter ”str” passed to this function points to a C string comprising only the ASCII characters ”1” and ”0”. The function returns the unsigned integer represented by the C string when interpreted as a binary number. For example, if ”str” points to the string ”10110” then this function returns 22. It is assumed the input string contains no more than 32 bits hence the ”unsigned” type is sufficient to hold the maximum result.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
/*----------------------------------------------------------------------------*/ /* Title: bin2dec_ver1.c */ /*----------------------------------------------------------------------------*/ /* Binary to decimal conversion */ /* Version 1 */ /* To compile: */ /* $ gcc -o bin2dec_ver1 bin2dec_ver1.c */ /* $ chmod 755 bin2dec_ver1 */ /* */ /* To run: */ /* $ ./bin2dec_ver1 */ /* */ /*----------------------------------------------------------------------------*/
#include <stdio.h> #include <math.h> #include <ctype.h> #include <string.h> #include <stdlib.h> /* The function read a string of characters and perform the following: */ /* a) check the validity of the characters ("1" or "0" NB: Use their ASCII codes). Exit if incorrect values */ /* b) convert the correct representation of binary string (number) into decimal */ /* Useful functions: strlen and power */ unsigned binary_to_decimal(const char *str) { /* Declare useful variable */ /* Add declaration of variable */ /* Check if input is made of 0s(ASCII 48) and 1s (ASCII 49) */ /* Exit if incorrect value: use "exit(0)" /* Add your code Here */ /* Convert Binary to Decimal Repeat for each binary digit starting from the last array element */ /* Add your code */ } /* Main program to be used for testing */ int main(void) { char data[33]; unsigned value; printf("Enter the string\n"); gets(data); value = binary_to_decimal(data); printf("Decimal equivalent is: %u\n",value); return(0); } -----------------------------------------------------------------------------------------------
Next, create 2 more versions based off of the previous version that are described below:
Version 2: Modify the main function of the program (”bin2dec ver1.c”, version 1) to allow the user to continuously enter the binary strings for the conversion. The program shall end if the user enters ”q” or ”Q”. Name the file that contains these changes ”bin2dec ver2.c”
Version 3: Modify the main function of the program (”bin2dec ver1.c”, version 1) to allow the user to pass the binary string as a command line. For instance by typing ”bin2dec ver3 11111”, you should get the following message: ”The Decimal equivalent is: 31”. Name the file that contains these changes ”bin2dec ver3.c”
Version 1
#include <stdio.h>
#include <string.h>
#include <math.h>
unsigned binary_to_decimal(const char *str)
{
//variables
int n = 0;
//calculate the size of the string
int size = strlen(str);
//initialize count variable to zero
int count = 0;
//loop through the string
while ( *str != '\0' )
{
//if str char is 1
if ( *str == '1' )
{
//calculate the n
n = n + pow(2, size-count-1);
}
//incrment count
count++;
//increment string
str++;
}
return n;
}
/* Main program to be used for testing */
int main()
{
char data[33];
unsigned value;
printf("Enter the string\n");
gets(data);
value = binary_to_decimal(data);
printf("Decimal equivalent is: %u\n",value);
return(0);
}
----------------------------------------------------------------------
SEE OUTPUT
Version 2
#include <stdio.h>
#include <string.h>
#include <math.h>
unsigned binary_to_decimal(const char *str)
{
//variables
int n = 0;
//calculate the size of the string
int size = strlen(str);
//initialize count variable to zero
int count = 0;
//loop through the string
while ( *str != '\0' )
{
//if str char is 1
if ( *str == '1' )
{
//calculate the n
n = n + pow(2, size-count-1);
}
//incrment count
count++;
//increment string
str++;
}
return n;
}
/* Main program to be used for testing */
int main()
{
char data[33];
unsigned value;
printf("Enter the string\n");
gets(data);
while(strcmp(data,"q") != 0 && strcmp(data,"Q") != 0) {
value = binary_to_decimal(data);
printf("Decimal equivalent is: %u\n",value);
printf("Enter the string\n");
gets(data);
}
return(0);
}
----------------------------------------------------------------------
SEE OUTPUT
Version 3
#include <stdio.h>
#include <string.h>
#include <math.h>
unsigned binary_to_decimal(const char *str)
{
//variables
int n = 0;
//calculate the size of the string
int size = strlen(str);
//initialize count variable to zero
int count = 0;
//loop through the string
while ( *str != '\0' )
{
//if str char is 1
if ( *str == '1' )
{
//calculate the n
n = n + pow(2, size-count-1);
}
//incrment count
count++;
//increment string
str++;
}
return n;
}
/* Main program to be used for testing */
int main(int argc, char ** argv)
{
char data[33];
unsigned value;
value = binary_to_decimal(argv[1]);
printf("Decimal equivalent is: %u\n",value);
return(0);
}
----------------------------------------------------------------------
SEE OUTPUT
Thanks, PLEASE COMMENT IF There is any concern.
PLEASE
UPVOTE
Get Answers For Free
Most questions answered within 1 hours.