Question

In: Computer Science

A C program that accepts a single command line argument and converts it in to binary...

A C program that accepts a single command line argument and converts it in to binary with array length of 16 bits. The array should contain the binary of the int argument. the program should also convert negative numbers. Side note the command line arg is a valid signed int.

Solutions

Expert Solution

Code for this program is provided below alongwith the output screenshot. This program takes command line argument and converts the number from decimal to binary 16 bit . If a negative number is given through argument , it represents the number in two'complement .Two's complement representation is widely used in todays systems for representing negative numbers. code is explained through code comments as well as a brief explanation is written below for the program and functions used in it.

#############################################################################

EXPLANATION-->>>

Program takes negative as well as positive numbers and convert them to repective binary representaion . For negative numbers , we convert the binary representation in two's complement and the result is the representation in binary.

For example: consider a number 87, its binary will be 1010111 and in 16 bit we will add 0 in right most bits so the number 87 in binary 16 bit will be 0000000001010111

Now -87 will be represented in two's complement. first we will find binary of 87 that is 0000000001010111

Now we inverse this binary and add 1 , the result will be two's complement or the binary 16 bit of negative 87 :- : 1111111110101000

+1

11111111101001001

11111111101001001 is binary representation of -87

atoi() function

This function is used in program for converting string two number. As the command line argument will be in string format we will convert it to integer by using this function

abs() function

This function is used for taking absolute value of a number. means abs(17)=17 and abs(-17)=17.

All the necessary explanation is in comments in code

##########################################################################

CODE--->>>

#include <stdio.h>
#include <stdlib.h>
//main function taking arguments in arg[] array
int main(int argc,char* arg[])
{
    if(argc==1) //if the command line argument is not provided
        printf("\n There is no Command Line Argument");
   else //if the argument is provided
   {
       int number1,number2; //int variables
       int binary[16]; //binary array to represent individual bits of number
       int j=15; //variable to act as iterator for array

       number1=atoi(arg[1]); //converting command line argument to integer
 //converting to absolute value of number1 (meaning if the number1 is negative change it to positive
       number2=abs(number1);
//loop to convert decimal number to binary
       while(number2/2 >0 )
       {
           binary[j--]=number2%2;
           number2=number2/2;
       }
       binary[j--]=number2;
       if(j>=0) //putting zero in all the left places of 16 bit number
       {
           for(int i=j;i>=0;i--)
            binary[i]=0;
       }

     if(number1>=0) //if the number is positive print the binary representation
      {
        printf("Binary Representation of %d is :\n ",number1);

        for(int i=0;i<16;i++)
        printf("%d",binary[i]);
      }
//if the number is negative than we need to convert the binary into two's complement
      else
      {
         //loop to inverse each bit
         for(int i=0;i<16;i++)
          {
              if(binary[i]==0)
                binary[i]=1;
              else
                binary[i]=0;
          }

          int carry=1; //variable for carry
          //Adding 1 to inverse binary to get two's complement
          for(int i=15;i>=0;i--)
          {
              int sum=0;
              sum=binary[i]+carry;
              if(sum==2) //for 1+1 because 1+1=0 with carry of 1
              {
                  carry=1;
                  binary[i]=0;
              }
              else //for 1+1 or 0+0
              {
                  binary[i]=sum;
                  carry=0;
              }
          }
        //print this two's complement because negative numbers are represented in twos complement
        printf("\nBinary Representation of %d is :\n ",number1);
        for(int i=0;i<16;i++)
        printf("%d",binary[i]);
      }
    }
    return 0;
}

#######################################################################

OUTPUT-->>


Related Solutions

Write a C program that accepts a port number as a command line argument, and starts...
Write a C program that accepts a port number as a command line argument, and starts an HTTP server. This server should constantly accept() connections, read requests of the form: GET /path HTTP/1.1\r\n\r\n read the file indicated by /path, and send it over the "connect" file descriptor returned by the call to accept().
Write a C or C++ program A6p2.c(pp) that accepts one command line argument which is an integer n between 2 and 4 inclusi...
Write a C or C++ program A6p2.c(pp) that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 49 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12];). Use pthread to create n threads to square all 60 array elements. You should divide this update task among the n threads as evenly as possible. Print the array both before and after the...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename If the user did not supply both arguments, the program should display an error message and exit. The program opens the given filename. Each line that contains the given string is displayed. Use the strstr function to search each line for the string. You may assume no line is longer than 255 characters. The matching lines are displayed to standard output (normally the screen).
Write a C program called test that takes one command line argument, an integer N. When...
Write a C program called test that takes one command line argument, an integer N. When we run test: ./test N the program will do this: the parent process forks N child processes each child process prints its process ID, exits the parent process waits for all child processes to exit, then exits
3. Write a java method that accepts a binary number and converts it to decimal then...
3. Write a java method that accepts a binary number and converts it to decimal then display the result. For Example: (110)2 = (6)10 (2 2 *1)+ (21 *1) + (20*0) = 6 Additional task: write a method that accepts a decimal and converts it to binary. i need to solve it as soon as and i will upvote you directly
The program should be able to do the following: In Java accepts one command line parameter....
The program should be able to do the following: In Java accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated by a single whitespace. reads the integers from the text file in part a into an array of integers. sort the integers...
Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are...
Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are not allowed to use library functions for conversion. The output should look exactly as follows: DECIMAL      BINARY                     HEXDECIMAL                      BCD 0                      0000 0000                   00                                            0000 0000 0000 1                      0000 0001                   01                                            0000 0000 0001 2                      0000 0010                   02                                            0000 0000 0010 .                       .                                   .                                               . .                       .                                   .                                               . 255                  1111 1111                   FF                                            0010 0101 0101
Python Write a program that takes a text filename as command line argument, and prints number...
Python Write a program that takes a text filename as command line argument, and prints number of times each letter occurred in this file.
Introduction Write in C++ at the Linux command line a program that is the same as...
Introduction Write in C++ at the Linux command line a program that is the same as the previous collection app project but now uses a class to store the items and also can save the items to a file that can be read back into the array by the user when the program is re-started. You can use your project 1 submission as a starting point or you can do something new as long as it meets the listed requirements....
Write a program that accepts a number of minutes and converts it both to hours and...
Write a program that accepts a number of minutes and converts it both to hours and days. For example, 6000 minutes is 100.0 hours or 4.166666666666667 days. (I currently have what is below) import java.util.Scanner; public class MinutesConversion { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int numOfMinutes = sc.nextInt(); double hour = numOfMinutes/60.00; double days = (hour/24); System.out.println(numOfMinutes + " minutes is " + hour + " hours or " + days + " days.");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT