Question

I am to create three different versions of the following C program code below that implements...

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”

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char **argv) { const int BUF_LEN = 128; char str[BUF_LEN]; int i; char c; int is_binary; int d, n; /* Get the user to enter a string */ printf("Please enter a string made of 0s and 1s, finishing the entry by pressing Enter.\n"); for (i=0; i<BUF_LEN-1; i++) { scanf("%c", &c); if (c == '\n') { break; } str[i] = c; } str[i] = '\0'; /*...
Using the following code answer the next couple questions: #include<stdio.h> #include<stdlib.h> #include<string.h> /* Rewrite using a...
Using the following code answer the next couple questions: #include<stdio.h> #include<stdlib.h> #include<string.h> /* Rewrite using a pointer to char str[] */ void array_to_ptr () { int n=0, len; char str[ ] = "Hello World!"; len = strlen(str); for( n=0; n<len; n++) {     putc(str[n], stdout); } printf("\nlength = %d\n", n); } int contains (char * str, char c); int * makearray(int n); int main (void) { printf("Question #2 - array_to_ptr:\n"); array_to_ptr();   printf("\n------------------------------------\n\n"); printf("Question #3 - contains:\n"); printf("Test #1: "); if...
The following program implements a key-value data structure in which values are stored based on a...
The following program implements a key-value data structure in which values are stored based on a string keyword. Currently, the program allows new keys and values to be entered, but there is no function to retrieve the values given a search key. Complete the get_value function to retrieve a value given a search key. The function should print "INVALID KEY" if the key is not present. Do not remove any code OR add any code to main. You should only...
The code is in C programming language pls convert it into python. Thanks. Program --> #include...
The code is in C programming language pls convert it into python. Thanks. Program --> #include <stdio.h> #include <stdlib.h> void main() { //declare variables FILE *fileptr; char filename[15]; char charRead; char filedata[200],searchString[50]; int i=0,j=0,countNoOfWord=0,count=0; //enter the filename to be opened printf("Enter the filename to be opened \n"); scanf("%s", filename); /* open the file for reading */ fileptr = fopen(filename, "r"); //check file exit if (fileptr == NULL) { printf("Cannot open file \n"); exit(0); } charRead = fgetc(fileptr); //read the string...
Please provide answer in the format that I provided, thank you Write a program that prompts...
Please provide answer in the format that I provided, thank you Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must...
In assembly masm use the code below! Write a program that reverse a string using indirect...
In assembly masm use the code below! Write a program that reverse a string using indirect addressing (may not use the stack - push/pop). The string will be given by the user and be up to 64 characters long INCLUDE Irvine32.inc INCLUDE macros.inc MAX = 64 .data source BYTE MAX DUP('#'),0 destination BYTE LENGTHOF source DUP('*'),0 actual_length DWORD ? ask BYTE "Enter a String: ",0 .code main proc ; ask user for input mov edx, OFFSET ask call WriteString ;...
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
Consider the following code that is contained in the main function of a C program. What...
Consider the following code that is contained in the main function of a C program. What is wrong with the code? unsigned int myUnsignedInt = 100; printf("%c\n", myUnsignedInt);
Write a C/C++ program that performs the tasks described below. If there is just 1 command-line...
Write a C/C++ program that performs the tasks described below. If there is just 1 command-line argument and it is -hw you should simply print hello world and then exit(0). Otherwise, the program should provide a function named mm_alloc. This is the function prototype: void *mm_alloc(int num_bytes_to_allocate) mm_alloc function should obtain space for the user from a block which you obtain via mmap. You should obtain the block on the first invocation and the block size should be some number...
use c language only to solve this porblem -------------------------- Have the function StringChallenge(str) take the str...
use c language only to solve this porblem -------------------------- Have the function StringChallenge(str) take the str string parameter being passed and return the number of words the string contains (e.g. "Never eat shredded wheat or cake" would return 6). Words will be separated by single spaces. Examples Input: "Hello World" Output: 2 Input: "one 22 three" Output: 3 you have the following code what edits can you do #include <stdio.h> #include <string.h> void StringChallenge(char str[]) {      // code goes...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT